-
Notifications
You must be signed in to change notification settings - Fork 5
/
storybook-facade.js
214 lines (189 loc) · 5.08 KB
/
storybook-facade.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { storiesOf, action, linkTo } from '@storybook/react';
// import withReadme from 'storybook-readme/with-readme'
import { isElement } from 'react-dom/test-utils';
import * as knobs from '@storybook/addon-knobs';
import {
specs,
beforeEach,
afterEach,
before, // not supported in jest, use beforeAll
after, // not supported in jest, use afterAll
xit,
xdescribe,
describe as describeReal,
it as itReal,
} from 'storybook-addon-specifications';
import expectReal from 'expect';
const storybook = {
stories: {},
action,
linkTo,
// withReadme,
knobs,
};
const describe = (name, tests) => {
storybook.stories = storiesOf(name, module);
tests();
};
const it = (name, test) => {
const stories = storybook.stories;
const add = stories.addWithInfo || stories.add;
let story;
// jest doesn't seem to handle "/" in testnames
// Slashes are used in storybook to build a hierachy of stories
// we simply take the last part as action-name
const testname = name.substring(name.lastIndexOf('/') + 1, name.length);
specs(() =>
describeReal(testname, () => {
if (isElement(test)) {
story = test;
} else {
story = test();
}
}),
);
if (story) {
add.call(stories, name, () => story);
}
};
const expect = received => {
const callExpect = (method, ...expectedArgs) => {
const expectedValue = expectedArgs[0] || '';
const receivedFormatted = formatReceived(received, method);
const name = `expects ${receivedFormatted} ${method} ${expectedValue}`;
itReal(name, () => {
expectReal(received)[method](...expectedArgs);
});
};
const methods = expectMethods.reduce((expectObject, method) => {
expectObject[method] = callExpect.bind(null, method);
return expectObject;
}, {});
methods.not = expectNotMethods.reduce((expectObject, method) => {
const notMethod = method.replace('to', 'toNot');
expectObject[method] = callExpect.bind(null, notMethod);
return expectObject;
}, {});
return methods;
};
const formatReceived = (received, method) => {
if (method === 'toMatchSnapshot') {
return 'component or value';
} else if (typeof received === 'function') {
return received.toString();
} else if (typeof received === 'object') {
return JSON.stringify(received, null, 1);
}
return received;
};
const expectMethods = [
'toBeAn',
'toBeFalsy',
'toBeFewerThan',
'toBeMoreThan',
'toBeTruthy',
'toContain',
'toContainKey',
'toContainKeys',
'withArgs',
'withContext',
'toBe',
'toBeA',
'toBeGreaterThan',
'toBeGreaterThanOrEqualTo',
'toBeLessThan',
'toBeLessThanOrEqualTo',
'toEqual',
'toExclude',
'toExcludeKey',
'toExcludeKeys',
'toExist',
'toHaveBeenCalled',
'toHaveBeenCalledWith',
'toInclude',
'toIncludeKey',
'toIncludeKeys',
'toMatch',
'toThrow',
'toMatchSnapshot',
'toBeCalled',
];
const expectNotMethods = [
'toNotBeAn',
'toNotContain',
'toNotContainKey',
'toNotContainKeys',
'toNotInclude',
'toNotIncludeKey',
'toNotIncludeKeys',
'toNotBe',
'toNotBeA',
'toNotEqual',
'toNotExist',
'toNotHaveBeenCalled',
'toNotMatch',
'toNotThrow',
];
expectReal.extend({
toMatchSnapshot() {
expectReal.assert(true, 'expected a snapshot', this.actual);
return this;
},
toBeCalled() {
expectReal.assert(true, 'expected to be called', this.actual);
return this;
},
});
// this should actually perform the `only` function at some point
const fdescribe = (name, tests) => describe(name, tests);
const fit = (name, test) => it(name, test);
const beforeAll = before;
const afterAll = after;
const jest = {
fn: implementation => {
implementation = implementation || function jestFn() {};
implementation.mock = {
calls: [[], [], [], [], []],
instances: [{}, {}, {}, {}],
mockClear() {},
mockReset() {},
mockImplementation: () => implementation,
mockImplementationOnce: () => implementation,
mockReturnThis: () => implementation,
mockReturnValue: () => implementation,
mockReturnValueOnce: () => implementation,
};
return implementation;
}, // eslint-disable-line
clearAllTimers() {},
disableAutomock() {},
enableAutomock() {},
isMockFunction(fn) {}, // eslint-disable-line
genMockFromModule(moduleName) {}, // eslint-disable-line
mock(moduleName, factory, options) {}, // eslint-disable-line
clearAllMocks() {},
resetAllMocks() {},
resetModules() {},
runAllTicks() {},
runAllTimers() {},
runTimersToTime(msToRun) {}, // eslint-disable-line
runOnlyPendingTimers() {}, // eslint-disable-line
setMock(moduleName, moduleExports) {}, // eslint-disable-line
unmock(moduleName) {}, // eslint-disable-line
useFakeTimers() {},
useRealTimers() {},
spyOn(object, methodName) {}, // eslint-disable-line
};
window.storybook = storybook;
window.jest = jest;
window.expect = expect;
window.describe = describe;
window.xdescribe = xdescribe;
window.fdescribe = fdescribe;
window.it = it;
window.xit = xit;
window.fit = fit;
window.beforeEach = beforeEach;
window.afterEach = afterEach;
window.beforeAll = beforeAll;
window.afterAll = afterAll;