Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('service - requires ctx', t => {
const app = createTestFixture();
let wasResolved = false;
getSimulator(
app,
createPlugin({
deps: {rpcFactory: MockPluginToken},
provides: ({rpcFactory}) => {
// $FlowFixMe
t.throws(() => rpcFactory());
wasResolved = true;
},
})
);
t.true(wasResolved, 'service was resolved');
t.end();
});
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();
});
if (__BROWSER__) {
root = document.createElement('div');
root.setAttribute('id', 'root2');
if (document.body) {
document.body.appendChild(root);
}
app = new App(Root, el => render(el, root));
} else {
app = new App(Root);
}
app.register(HelmetPlugin);
app.middleware((ctx, next) => {
ctx.nonce = 'test-nonce';
return next();
});
const sim = getSimulator(app);
const ctx = await sim.render('/');
if (__NODE__) {
const fixtureFile = './src/__fixtures__/ssr1.html';
// Uncomment to regenerate fixture
// fs.writeFileSync(fixtureFile, ctx.body);
t.equal(ctx.body, fs.readFileSync(fixtureFile).toString());
} else if (__BROWSER__) {
// need to wait until next tick for dom changes
await new Promise(resolve => setTimeout(resolve, 10));
t.equal(document.title, "My Title's ");
const baseEl = document.querySelector('base');
if (!baseEl) {
throw new Error('Could not find base element');
}
t.equal(baseEl.getAttribute('href'), 'http://mysite.com/');
// $FlowFixMe
t.equal(typeof payload.args[0].error.stack, 'string');
// $FlowFixMe
t.equal(typeof payload.args[0].error.message, 'string');
called = true;
});
const mockEmitterPlugin = createPlugin({
provides: () => mockEmitter,
});
app.register(UniversalEventsToken, mockEmitterPlugin);
app.middleware({logger: LoggerToken}, ({logger}) => {
logger.error(new Error('fail'));
return (ctx, next) => next();
});
getSimulator(app);
t.equals(called, true, 'called');
t.end();
});
t.fail();
t.end();
return;
}
const store = await reduxScoped.initStore();
// $FlowFixMe
t.equals(store.mock, true, '[Final store] ctx provided by ctxEnhancer');
return next();
};
},
});
app.register(testPlugin);
const simulator = getSimulator(app);
await simulator.render('/');
};
test('mock with missing handler', async t => {
const app = createTestFixture();
t.plan(1);
getSimulator(
app,
createPlugin({
deps: {rpcFactory: MockPluginToken},
provides: async deps => {
const rpc = deps.rpcFactory.from(mockCtx);
try {
await rpc.request('test');
} catch (e) {
t.equal(e.message, 'Missing RPC handler for test');
} finally {
t.end();
}
},
})
);
});
t.equals(hello, 'world', 'meta is ok');
called = true;
}
}
const app = new App('element', el => el);
app.register(UniversalEventsToken, UniversalEvents);
app.register(LoggerToken, plugin);
app.register(UniversalLoggerConfigToken, {transports: [new Transport()]});
app.middleware({events: UniversalEventsToken}, ({events}) => {
events.emit('universal-log', {
level: 'info',
args: ['test', {hello: 'world'}],
});
return (ctx, next) => next();
});
getSimulator(app);
t.equals(called, true, 'called');
t.end();
});
test('context#useService', async t => {
const TestToken = createToken('test');
const TestPlugin = createPlugin({provides: () => 3});
let didRender = false;
function TestComponent() {
const provides = useService(TestToken);
const ctx = React.useContext(FusionContext);
didRender = true;
t.equal(provides, 3, 'gets registered service');
t.equal(ctx.request.url, '/', 'gets Fusion context');
return React.createElement('div', null, 'hello');
}
const element = React.createElement(TestComponent);
const app = new App(element);
app.register(TestToken, TestPlugin);
const sim = getSimulator(app);
const ctx = await sim.render('/');
t.ok(typeof ctx.body === 'string' && ctx.body.includes('hello'), 'renders');
t.ok(didRender);
t.end();
});
function setup(app, pageData = {title: '/', page: '/'}) {
if (__BROWSER__) {
const el = document.createElement('script');
el.setAttribute('type', 'application/json');
el.setAttribute('id', '__ROUTER_DATA__');
const textNode = document.createTextNode(JSON.stringify(pageData));
el.appendChild(textNode);
document.body && document.body.appendChild(el);
const rootEl = document.createElement('div');
rootEl.setAttribute('id', 'root');
document.body && document.body.appendChild(rootEl);
}
const simulator = getSimulator(app);
return simulator;
}
tape('Downstream Error handling should work', async t => {
const app = new App('el', el => el);
app.middleware((ctx, next) => {
throw new Error('FAIL');
});
app.register(ApolloServer);
app.register(ApolloServerEndpointToken, '/graphql');
app.register(GraphQLSchemaToken, schema);
const simulator = getSimulator(app);
try {
await simulator.request('/graphql', {
body: query,
method: 'POST',
});
t.fail('Should not reach this');
} catch (e) {
t.ok(e, 'has error');
t.end();
}
});