Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Improve API for getSimulator #120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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('/');
Expand All @@ -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('/');
Expand Down Expand Up @@ -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({
Expand All @@ -105,7 +101,7 @@ test('use simulator with fixture and plugin dependencies', async t => {
return 'yay!';
},
});
getSimulator(app, testPlugin);
getSimulator(getTestFixture, testPlugin);

t.end();
});
Expand Down
55 changes: 32 additions & 23 deletions src/__tests__/index.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ export type Simulator = {
request: $Call<ExtractFusionAppReturnType, typeof request>,
render: $Call<ExtractFusionAppReturnType, typeof render>,
};
export function getSimulator(
app: FusionApp,
testPlugin?: FusionPlugin<*, *>
export function getSimulator<TDeps, TService>(
appCreator: () => FusionApp,
testPlugin?: FusionPlugin<TDeps, TService>
): Simulator {
const app = appCreator();

if (testPlugin) {
app.register(testPlugin);
}
Expand Down