From b41e125a973d85ef6704bb8e8e5df06187cc6a57 Mon Sep 17 00:00:00 2001 From: Alexander Smith Date: Thu, 17 May 2018 16:59:35 -0700 Subject: [PATCH 1/2] Improve API for getSimulator --- src/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 8d3571f9..cb14281e 100644 --- a/src/index.js +++ b/src/index.js @@ -27,10 +27,12 @@ export type Simulator = { request: $Call, render: $Call, }; -export function getSimulator( - app: FusionApp, - testPlugin?: FusionPlugin<*, *> +export function getSimulator( + appCreator: () => FusionApp, + testPlugin?: FusionPlugin ): Simulator { + const app = appCreator(); + if (testPlugin) { app.register(testPlugin); } From 95021f1b331b41fb495ae6716cba4c87e5759e87 Mon Sep 17 00:00:00 2001 From: Alexander Smith Date: Thu, 17 May 2018 17:02:21 -0700 Subject: [PATCH 2/2] Update unit tests --- src/__tests__/index.js | 12 +++----- src/__tests__/index.node.js | 55 +++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 20683ea3..3bcd728c 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -18,8 +18,7 @@ test('simulate render request', async t => { const renderFn = () => { flags.render = true; }; - const app = new App(element, renderFn); - var testApp = getSimulator(app); + var testApp = getSimulator(() => new App(element, renderFn)); const ctx = await testApp.render('/'); t.ok(flags.render, 'triggered ssr'); t.ok(ctx.element, 'sets ctx.element'); @@ -31,8 +30,7 @@ test('simulate multi-render requests', async t => { const renderFn = () => { counter.renderCount++; }; - const app = new App('hello', renderFn); - var testApp = getSimulator(app); + var testApp = getSimulator(() => new App('hello', renderFn)); for (var i = 1; i <= 5; i++) { await testApp.render('/'); @@ -48,8 +46,7 @@ test('simulate non-render request', async t => { const renderFn = () => { flags.render = true; }; - const app = new App(element, renderFn); - const testApp = getSimulator(app); + const testApp = getSimulator(() => new App(element, renderFn)); if (__BROWSER__) { try { testApp.request('/'); @@ -86,7 +83,6 @@ test('use simulator with fixture and plugin dependencies', async t => { app.register(msgProviderPluginToken, msgProviderPlugin); return app; } - const app = getTestFixture(); t.plan(3); let testPlugin: FusionPlugin<*, *> = createPlugin({ @@ -105,7 +101,7 @@ test('use simulator with fixture and plugin dependencies', async t => { return 'yay!'; }, }); - getSimulator(app, testPlugin); + getSimulator(getTestFixture, testPlugin); t.end(); }); diff --git a/src/__tests__/index.node.js b/src/__tests__/index.node.js index ce580e0d..c2e099cf 100644 --- a/src/__tests__/index.node.js +++ b/src/__tests__/index.node.js @@ -12,49 +12,58 @@ import App from 'fusion-core'; import {getSimulator} from '../index.js'; test('status is 404 if ctx.body is never updated', async t => { - const app = new App('el', el => el); - const ctx = await getSimulator(app).request('/'); + const ctx = await getSimulator(() => new App('el', el => el)).request('/'); t.equals(ctx.status, 404, 'status defaults to 404'); t.end(); }); test('status is 200 if ctx.body is updated in request', async t => { - const app = new App('el', el => el); - app.middleware((ctx, next) => { - ctx.body = {ok: 1}; - return next(); - }); - const ctx = await getSimulator(app).request('/'); + const appCreator = () => { + const app = new App('el', el => el); + app.middleware((ctx, next) => { + ctx.body = {ok: 1}; + return next(); + }); + return app; + }; + const ctx = await getSimulator(appCreator).request('/'); t.equals(ctx.status, 200, 'status defaults to 200'); t.end(); }); test('status is set if ctx.status is updated in request', async t => { - const app = new App('el', () => 'hello'); - app.middleware((ctx, next) => { - ctx.status = 500; - ctx.body = {error: 'error'}; - return next(); - }); - const ctx = await getSimulator(app).render('/'); + const appCreator = () => { + const app = new App('el', () => 'hello'); + app.middleware((ctx, next) => { + ctx.status = 500; + ctx.body = {error: 'error'}; + return next(); + }); + return app; + }; + const ctx = await getSimulator(appCreator).render('/'); t.equals(ctx.status, 500, 'status is set'); t.end(); }); test('status is 200 if ctx.body is updated in render', async t => { - const app = new App('el', () => 'hello'); - const ctx = await getSimulator(app).render('/'); + const ctx = await getSimulator(() => new App('el', () => 'hello')).render( + '/' + ); t.equals(ctx.status, 200, 'status defaults to 200'); t.end(); }); test('status is set if ctx.status is updated in render', async t => { - const app = new App('el', () => 'hello'); - app.middleware((ctx, next) => { - ctx.status = 500; - return next(); - }); - const ctx = await getSimulator(app).render('/'); + const appCreator = () => { + const app = new App('el', () => 'hello'); + app.middleware((ctx, next) => { + ctx.status = 500; + return next(); + }); + return app; + }; + const ctx = await getSimulator(appCreator).render('/'); t.equals(ctx.status, 500, 'status is set'); t.end(); });