This repository has been archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
116 lines (100 loc) · 3.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/** Copyright (c) 2018 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import assert from 'assert';
import FusionApp, {createToken, createPlugin} from 'fusion-core';
import type {FusionPlugin, Token} from 'fusion-core';
/* Note: as the Jest type definitions are declared globally and not as part of
* a module, we must import the relevant types directly from the libdef file here
* to avoid the invariant that all consumers must add the jest libdef to their
* .flowconfig libs.
*/
import type {JestTestName, JestObjectType} from './flow/jest_v22.x.x.js';
import {render, request} from './simulate';
export {createRequestContext, createRenderContext} from './mock-context';
declare var __BROWSER__: boolean;
type ExtractFusionAppReturnType = <R>((FusionApp) => R) => R;
export type Simulator = {
request: $Call<ExtractFusionAppReturnType, typeof request>,
render: $Call<ExtractFusionAppReturnType, typeof render>,
getService<T>(token: Token<T>): T,
};
export function getSimulator(
app: FusionApp,
testPlugin?: FusionPlugin<*, *>
): Simulator {
if (testPlugin) {
app.register(testPlugin);
}
app.resolve();
return {
request: request(app),
render: render(app),
// $FlowFixMe
getService: token => app.getService(token),
};
}
export function getService<TDeps, TService>(
appCreator: () => FusionApp,
plugin: FusionPlugin<TDeps, TService>
): TService {
const app = appCreator();
const token: Token<TService> = createToken('service-helper');
let extractedService = null;
app.register(token, plugin);
app.register(
createPlugin({
deps: {service: token},
provides: ({service}) => {
extractedService = service;
},
})
);
app.resolve();
if (!extractedService) {
throw new Error('Provided plugin does not export a service');
}
return extractedService;
}
// Export test runner functions from jest
type ExtractArgsReturnType<TArguments, TReturn> = <R>(
(implementation?: (...args: TArguments) => TReturn) => R
) => R;
type JestFnType = $PropertyType<JestObjectType, 'fn'>;
// eslint-disable-next-line flowtype/generic-spacing
type MockFunctionType<TArgs, TReturn> = (
...args: TArgs
) => $Call<ExtractArgsReturnType<TArgs, TReturn>, JestFnType>;
type MatchSnapshotType = (tree: mixed, snapshotName: ?string) => void;
type CallableAssertType = (
assert: typeof assert & {matchSnapshot: MatchSnapshotType}
) => void | Promise<void>;
type TestType = (name: JestTestName, assert: CallableAssertType) => void;
// eslint-disable-next-line import/no-mutable-exports
let mockFunction: MockFunctionType<*, *>, test: any;
// $FlowFixMe
if (typeof it !== 'undefined') {
// Surface snapshot testing
// $FlowFixMe
assert.matchSnapshot = (tree, snapshotName) =>
// $FlowFixMe
expect(tree).toMatchSnapshot(snapshotName);
/* eslint-env node, jest */
test = (description, callback, ...rest) =>
it(description, () => callback(assert), ...rest);
// $FlowFixMe
mockFunction = (...args) => jest.fn(...args);
} else {
const notSupported = () => {
throw new Error('Can’t import test() when not using the test-app target.');
};
test = notSupported;
mockFunction = notSupported;
}
const mockFunctionExport = ((mockFunction: any): MockFunctionType<*, *>);
const testExport = ((test: any): TestType);
export {mockFunctionExport as mockFunction, testExport as test};