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();
});
test('service - cannot track the same types more than once at a time', t => {
const perfService = getService(
createTestFixture,
NodePerformanceEmitterPlugin
);
t.throws(() => perfService.start(), 'already running trackers cannot start');
// Able to start now that we've stopped
t.doesNotThrow(() => perfService.stop(), 'service can be stopped');
t.doesNotThrow(
() => perfService.start(),
'service can run if the trackers are not active'
);
t.doesNotThrow(() => perfService.stop(), 'service can be stopped');
t.doesNotThrow(
() => perfService.stop(),
// @noflow
import {test} from 'fusion-test-utils';
import classPropFixture from '../class-props';
test('Class properties work /w flow annotation', async assert => {
const result = new classPropFixture();
assert.equal(
result.classProp(),
true,
'class props work and evaluates to true'
);
});
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('/');
};
tape('non-ssr routes', async t => {
const reducer = (state, action) => ({test: action.payload || 1});
const plugin = getService(appCreator(reducer), Redux);
let mockCtx = {
body: null,
memoized: new Map(),
};
t.plan(1);
if (!Redux.middleware) {
t.end();
return;
}
// $FlowFixMe
await Redux.middleware(null, plugin)((mockCtx: any), () => Promise.resolve());
t.notok(plugin.from((mockCtx: any)).store);
t.end();
});
tape('browser middleware', async t => {
const element = React.createElement('div');
const ctx: any = {element, template: {body: []}, memoized: new Map()};
const service = getService(appCreator(), Plugin);
try {
await (Plugin.middleware &&
// $FlowFixMe
Plugin.middleware(null, service)((ctx: any), () => Promise.resolve()));
} catch (e) {
t.ifError(e);
}
t.end();
});
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();
}
},
})
);
});