forked from happo/happo-plugin-storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.es6.js
230 lines (212 loc) · 6.28 KB
/
register.es6.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import storybookClient from '@storybook/core/client';
const time = window.happoTime || {
originalDateNow: Date.now,
originalSetTimeout: window.setTimeout.bind(window),
};
const ASYNC_TIMEOUT = 100;
const WAIT_FOR_TIMEOUT = 2000;
let examples;
let currentIndex = 0;
let defaultDelay;
async function waitForSomeContent(elem, start = time.originalDateNow()) {
const html = elem.innerHTML.trim();
const duration = time.originalDateNow() - start;
if (html === '' && duration < ASYNC_TIMEOUT) {
return new Promise(resolve =>
time.originalSetTimeout(
() => resolve(waitForSomeContent(elem, start)),
10,
),
);
}
return html;
}
async function waitForWaitFor(waitFor, start = time.originalDateNow()) {
const duration = time.originalDateNow() - start;
if (!waitFor() && duration < WAIT_FOR_TIMEOUT) {
return new Promise(resolve =>
time.originalSetTimeout(
() => resolve(waitForWaitFor(waitFor, start)),
50,
),
);
}
}
async function getExamples() {
const storyStore = window.__STORYBOOK_CLIENT_API__._storyStore;
if (storyStore.extract) {
if (storyStore.cacheAllCSFFiles) {
await storyStore.cacheAllCSFFiles();
}
return Object.values(storyStore.extract())
.map(({ id, kind, story, parameters }) => {
if (parameters.happo === false) {
return;
}
let delay = defaultDelay;
let waitForContent;
let waitFor;
let beforeScreenshot;
let afterScreenshot;
let targets;
if (typeof parameters.happo === 'object' && parameters.happo !== null) {
delay = parameters.happo.delay || defaultDelay;
waitForContent = parameters.happo.waitForContent;
waitFor = parameters.happo.waitFor;
beforeScreenshot = parameters.happo.beforeScreenshot;
afterScreenshot = parameters.happo.afterScreenshot;
targets = parameters.happo.targets;
}
return {
component: kind,
variant: story,
storyId: id,
delay,
waitForContent,
waitFor,
beforeScreenshot,
afterScreenshot,
targets,
};
})
.filter(Boolean);
}
const result = [];
for (let story of window.__STORYBOOK_CLIENT_API__.getStorybook()) {
const component = story.kind;
for (let example of story.stories) {
const { name: variant } = example;
let delay = defaultDelay;
let waitForContent;
let waitFor;
let targets;
let beforeScreenshot;
let afterScreenshot;
if (storyStore.getStoryAndParameters) {
const { parameters } = storyStore.getStoryAndParameters(
story.kind,
variant,
);
if (parameters.happo === false) {
continue;
}
if (typeof parameters.happo === 'object' && parameters.happo !== null) {
delay = parameters.happo.delay || defaultDelay;
waitForContent = parameters.happo.waitForContent;
waitFor = parameters.happo.waitFor;
beforeScreenshot = parameters.happo.beforeScreenshot;
afterScreenshot = parameters.happo.afterScreenshot;
targets = parameters.happo.targets;
}
}
const storyId = (storybookClient.toId || (() => undefined))(
story.kind,
variant,
);
result.push({
component,
variant,
delay,
storyId,
waitForContent,
waitFor,
beforeScreenshot,
afterScreenshot,
targets,
});
}
}
return result;
}
function filterExamples(all) {
if (initConfig.chunk) {
const examplesPerChunk = Math.ceil(
all.length / initConfig.chunk.total,
);
const startIndex = initConfig.chunk.index * examplesPerChunk;
const endIndex = startIndex + examplesPerChunk;
all = all.slice(startIndex, endIndex);
}
if (initConfig.targetName) {
all = all.filter(e => {
if (!e.targets || !Array.isArray(e.targets)) {
// This story hasn't been filtered for specific targets
return true;
}
return e.targets.includes(initConfig.targetName);
});
}
return all;
}
let initConfig = {};
window.happo = {};
window.happo.init = (config) => {
initConfig = config;
};
window.happo.nextExample = async () => {
if (!examples) {
examples = filterExamples(await getExamples());
}
if (currentIndex >= examples.length) {
return;
}
const {
component,
variant,
storyId,
delay,
waitForContent,
waitFor,
beforeScreenshot,
} = examples[currentIndex];
try {
const docsRootElement = document.getElementById('docs-root');
if (docsRootElement) {
docsRootElement.setAttribute('data-happo-ignore', 'true');
}
const rootElement = document.getElementById('root');
rootElement.setAttribute('data-happo-ignore', 'true');
const { afterScreenshot } = examples[currentIndex - 1] || {};
if (typeof afterScreenshot === 'function') {
try {
await afterScreenshot({ rootElement });
} catch (e) {
console.error('Failed to invoke afterScreenshot hook', e);
}
}
window.__STORYBOOK_ADDONS_CHANNEL__.emit('setCurrentStory', {
kind: component,
story: variant,
storyId,
});
await new Promise(resolve => time.originalSetTimeout(resolve, 0));
await waitForSomeContent(rootElement);
if (/sb-show-errordisplay/.test(document.body.className)) {
// It's possible that the error is from unmounting the previous story. We
// can try re-rendering in this case.
window.__STORYBOOK_ADDONS_CHANNEL__.emit('forceReRender');
await waitForSomeContent(rootElement);
}
if (beforeScreenshot && typeof beforeScreenshot === 'function') {
try {
await beforeScreenshot({ rootElement });
} catch (e) {
console.error('Failed to invoke beforeScreenshot hook', e);
}
}
await new Promise(resolve => time.originalSetTimeout(resolve, delay));
if (waitFor) {
await waitForWaitFor(waitFor);
}
return { component, variant, waitForContent };
} catch (e) {
console.warn(e);
return { component, variant };
} finally {
currentIndex++;
}
};
export const setDefaultDelay = delay => {
defaultDelay = delay;
};
export const isHappoRun = () => window.__IS_HAPPO_RUN;