Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
t.equal(payload.status, 'failure');
t.equal(typeof payload.timing, 'number');
t.equal(payload.error, 'failure data');
},
});
const app = createTestFixture();
// $FlowFixMe
app.register(FetchToken, mockFetchAsFailure);
// $FlowFixMe
app.register(UniversalEventsToken, mockEmitter);
let wasResolved = false;
getSimulator(
app,
createPlugin({
deps: {rpcFactory: MockPluginToken},
provides: deps => {
const rpc = deps.rpcFactory.from();
t.equals(typeof rpc.request, 'function', 'has method');
const testRequest = rpc.request('test');
t.ok(testRequest instanceof Promise, 'has right return type');
testRequest
.then(() => {
// $FlowFixMe
t.fail(() => new Error('should reject promise'));
})
.catch(e => {
t.equal(e, 'failure data', 'should pass failure data through');
});
wasResolved = true;
const testEnhancer = async (
t,
enhancer: StoreEnhancer<*, *, *> | FusionPlugin<*, StoreEnhancer<*, *, *>>
): Promise => {
const app = new App('el', el => el);
const mockReducer: Reducer<*, *> = s => s;
app.register(EnhancerToken, enhancer);
app.register(ReducerToken, mockReducer);
app.register(ReduxToken, Redux);
const testPlugin = createPlugin({
deps: {
redux: ReduxToken,
},
middleware({redux}) {
return async (ctx, next) => {
const reduxScoped = redux.from(ctx);
if (!reduxScoped.initStore) {
t.fail();
t.end();
return;
}
const store = await reduxScoped.initStore();
// $FlowFixMe
t.equals(store.mock, true, '[Final store] ctx provided by ctxEnhancer');
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
/* eslint-env node */
import {UniversalEventsToken} from 'fusion-plugin-universal-events';
import {createPlugin} from 'fusion-core';
import type {FusionPlugin} from 'fusion-core';
import type {BrowserPerfDepsType} from './flow';
const plugin: FusionPlugin =
// $FlowFixMe
__NODE__ &&
createPlugin({
deps: {emitter: UniversalEventsToken},
provides: deps => {
const emitter = deps.emitter;
const perfLoggerVersion = require('../package.json').version;
emitter.on('browser-performance-emitter:stats:browser-only', (e, ctx) => {
if (ctx) {
const scopedEmitter = emitter.from(ctx);
scopedEmitter.emit(
'browser-performance-emitter:stats',
mapPerfEvent(e)
);
} else {
emitter.emit(
'browser-performance-emitter:stats',
mapPerfEvent(e),
ctx
test('server plugin', async () => {
let store;
const StorageToken = createToken('Storage');
const StorageService = createPlugin({
provides() {
return data => (store = data);
},
});
const app = new App(' ', v => v);
let data = {};
app.register(StorageToken, StorageService);
app.register(
plugin(app, {
deps: {save: StorageToken},
store: {
storeSync(args) {
data = args;
},
async store(args, {save}) {
function TestComponent(props) {
didRender = true;
t.deepLooseEqual(props.test1, testProvides1, 'works with plain plugin');
t.deepLooseEqual(
props.test2,
testProvides2,
'works with legacy PluginProvider'
);
t.deepLooseEqual(props.mapped, testProvides3, 'maps service to props');
t.notok(props.ctx, 'does not pass ctx through by default');
return React.createElement('div', null, 'hello');
}
const testPlugin1 = createPlugin({provides: () => testProvides1});
const testPlugin2 = plugin.create(
'test2',
createPlugin({provides: () => testProvides2})
);
const testPlugin3 = createPlugin({provides: () => testProvides3});
const element = React.createElement(withTest(TestComponent));
const app = new App(element);
app.register(TestToken1, testPlugin1);
app.register(TestToken2, testPlugin2);
app.register(TestToken3, testPlugin3);
const sim = getSimulator(app);
const ctx = await sim.render('/');
t.ok(typeof ctx.body === 'string' && ctx.body.includes('hello'));
t.ok(didRender);
t.end();
});
function createTestFixture() {
const mockTimers = mockTimersFactory();
const mockEmitterPlugin = createPlugin({
provides: () => mockEmitterFactory(),
});
const app = new App('content', el => el);
app.register(NodePerformanceEmitterToken, NodePerformanceEmitterPlugin);
app.register(TimersToken, mockTimers);
app.register(UniversalEventsToken, mockEmitterPlugin);
return app;
}
const testProvides2 = {hello: 2};
const testProvides3 = {hello: 3};
let didRender = false;
function TestComponent(props) {
didRender = true;
t.deepLooseEqual(props.test1, testProvides1, 'works with plain plugin');
t.deepLooseEqual(
props.test2,
testProvides2,
'works with legacy PluginProvider'
);
t.deepLooseEqual(props.mapped, testProvides3, 'maps service to props');
t.notok(props.ctx, 'does not pass ctx through by default');
return React.createElement('div', null, 'hello');
}
const testPlugin1 = createPlugin({provides: () => testProvides1});
const testPlugin2 = plugin.create(
'test2',
createPlugin({provides: () => testProvides2})
);
const testPlugin3 = createPlugin({provides: () => testProvides3});
const element = React.createElement(withTest(TestComponent));
const app = new App(element);
app.register(TestToken1, testPlugin1);
app.register(TestToken2, testPlugin2);
app.register(TestToken3, testPlugin3);
const sim = getSimulator(app);
const ctx = await sim.render('/');
t.ok(typeof ctx.body === 'string' && ctx.body.includes('hello'));
t.ok(didRender);
t.end();
});
async function injectServices(t) {
const HelloToken = createToken('hola');
const HelloPlugin = createPlugin({
provides() {
return 'world';
},
});
const GoodbyeToken = createToken('adios');
const GoodbyePlugin = createPlugin({
provides() {
return 'moon';
},
});
function TestComponent({hi, bye}) {
t.equal(hi, 'world');
t.equal(bye, 'moon');
return (
<div>
{hi} {bye}
</div>
);
}
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import React from 'react';
import {createPlugin} from 'fusion-core';
import {HelmetProvider} from 'react-helmet-async';
import type {FusionPlugin} from 'fusion-core';
const plugin =
__BROWSER__ &&
createPlugin({
middleware: () => {
return (ctx, next) => {
ctx.element = (
{ctx.element}
);
return next();
};
},
});
export default ((plugin: any): FusionPlugin);
const pluginFactory: () => PluginType = () =>
createPlugin({
deps: {
fetch: FetchToken.optional,
hydrationState: HydrationStateToken.optional,
events: UniversalEventsToken.optional,
},
provides: ({fetch = window.fetch, hydrationState, events} = {}) => {
class I18n {
locale: string;
translations: TranslationsObjectType;
requestedKeys: Set;
constructor() {
const {localeCode, translations} =
hydrationState || loadTranslations();
this.requestedKeys = new Set();
this.translations = translations || {};