Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('translate', async t => {
const data = {test: 'hello', interpolated: 'hi ${adjective} ${noun}'};
const app = new App('el', el => el);
app.register(I18nToken, I18n);
app.register(I18nLoaderToken, {
from: () => ({translations: data, locale: 'en_US'}),
});
// $FlowFixMe
app.register(UniversalEventsToken, {
from: () => ({
emit: (name, payload) => {
t.equals(
name,
'i18n-translate-miss',
'emits event when translate key missing'
);
t.equals(
payload.key,
'missing-translation',
test('/health request', async t => {
t.plan(2);
const app = new App('el', el => el);
app.register(SWTemplateFunctionToken, swTemplateFunction);
app.register(ServiceWorker);
const sim = getSimulator(app);
// Basic /health request
const ctx_1 = await sim.request('/sw.js');
t.equal(ctx_1.status, 200, 'sends 200 status on sw request');
t.ok(
String(ctx_1.body)
.trim()
.replace(/\n/g, '')
.startsWith(`import {getHandlers} from '../../index'`),
'sends correct response'
);
t.end();
await app.cleanup();
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');
// @flow
import App from 'fusion-core';
import {assetUrl} from 'fusion-core';
import * as jsonData from './static/test.json';
import serverAsset from './server-asset.js';
const hoistedUrl = assetUrl('./static/test.css');
if (typeof window !== 'undefined') {
window.__hoistedUrl__ = hoistedUrl;
}
export default (async function() {
const app = new App('element', el => el);
__NODE__ &&
app.middleware((ctx, next) => {
if (ctx.url.startsWith('/_static')) {
ctx.set('x-test', 'test');
} else if (ctx.url === '/test') {
ctx.body = assetUrl('./static/test.css');
} else if (ctx.url === '/dirname') {
ctx.body = __dirname;
} else if (ctx.url === '/filename') {
ctx.body = __filename;
// @noflow
import {assetUrl} from 'fusion-core';
export default assetUrl('./static/test-server-asset.txt');
// @noflow
import App from 'fusion-core';
import {assetUrl} from 'fusion-core';
import {key as jsonField} from './static/test.json';
import serverAsset from './server-asset.js';
const hoistedUrl = assetUrl('./static/test.css');
if (typeof window !== 'undefined') {
window.__hoistedUrl__ = hoistedUrl;
}
export default (async function() {
const app = new App('element', el => el);
__NODE__ &&
app.middleware((ctx, next) => {
if (ctx.url.startsWith('/_static')) {
ctx.set('x-test', 'test');
} else if (ctx.url === '/test') {
ctx.body = assetUrl('./static/test.css');
} else if (ctx.url === '/dirname') {
ctx.body = __dirname;
} else if (ctx.url === '/filename') {
ctx.body = __filename;
return;
}
const i18n = I18n.provides(deps);
if (!I18n.middleware) {
t.end();
return;
}
await I18n.middleware(deps, i18n)(ctx, () => Promise.resolve());
t.equals(ctx.template.body.length, 1, 'injects hydration code');
t.equals(
// $FlowFixMe
consumeSanitizedHTML(ctx.template.body[0]).match('hello')[0],
'hello'
);
t.equals(consumeSanitizedHTML(ctx.template.body[0]).match(''), null);
t.equals(ctx.template.htmlAttrs['lang'], 'en-US');
chunkTranslationMap.dispose('a.js', [0], Object.keys(data));
chunkTranslationMap.translations.clear();
t.end();
});
const Plugin = getService(appCreator(reducer), Redux);
t.plan(5);
if (!Redux.middleware) {
t.end();
return;
}
// $FlowFixMe
await Redux.middleware(null, Plugin)(ctx, () => Promise.resolve());
t.ok(Plugin.from(ctx).store);
t.notEquals(ctx.element, element, 'wraps provider');
t.equals(ctx.template.body.length, 1, 'pushes serialization to body');
// $FlowFixMe
t.equals(consumeSanitizedHTML(ctx.template.body[0]).match('test')[0], 'test');
t.equals(consumeSanitizedHTML(ctx.template.body[0]).match(''), null);
t.end();
});
const ctx: any = {element, template: {body: []}, memoized: new Map()};
const service = getService(appCreator(), Plugin);
t.plan(3);
if (!Plugin.middleware) {
t.end();
return;
}
// $FlowFixMe
await Plugin.middleware(null, service)(ctx, () => Promise.resolve());
t.equals(ctx.template.body.length, 1, 'pushes serialization to body');
t.equals(
// $FlowFixMe
consumeSanitizedHTML(ctx.template.body[0]).match('__plugin__value__')[0],
'__plugin__value__'
);
t.equals(consumeSanitizedHTML(ctx.template.body[0]).match(''), null);
t.end();
});