-
Notifications
You must be signed in to change notification settings - Fork 423
/
Copy pathassertionTestUtils.js
103 lines (93 loc) · 3.43 KB
/
assertionTestUtils.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
// @flow
// Copyright (c) 2018-present, GM Cruise LLC
//
// This source code is licensed under the Apache License, Version 2.0,
// found in the LICENSE file in the root directory of this source tree.
// You may not use this file except in compliance with the License.
/* eslint-disable react/display-name */
import React, { useLayoutEffect, useState } from "react";
import { addConsoleErrorListener, removeConsoleErrorListener } from "./wrapConsoleError";
export async function timeout(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
type Story = (setTestData: (any) => void, state: any) => React$Element<any>;
type Assertion = (getTestData: () => any, setState: (any) => void) => Promise<void>;
type AssertionTest = {|
story: Story,
assertions: Assertion,
|};
/*
* This function takes a storybook story and some assertions about it and runs those assertions in the browser as the
* storybook test is running. It enables treating our storybook stories as tests.
*
* This function has an integration with the storycap package that we use to generate screenshots:
* it adds a `window.waitFor` function that returns a promise that only resolves once the assertions have run.
*
* Usage:
* storiesOf("SomeModule", module).add(
* "Some test",
* assertionTest({
* story: (setTestData) => { .. },
* assertions: async (getTestData) => { ... },
* })
* );
*/
export function assertionTest({ story, assertions }: AssertionTest): () => React$Element<any> {
let testData: any = null;
function setTestData(data: any) {
// Set the testData on window for debugging purposes.
const windowObject = window.parent ? window.parent : window;
// eslint-disable-next-line no-console
console.log("Set the following test data. See window.testData to access it for debugging purposes.", data);
windowObject.testData = data;
testData = data;
}
function getTestData(): any {
return testData;
}
function Component() {
const [error, setError] = useState<any>();
const [state, setState] = useState(null);
useLayoutEffect(() => {
const assertionPromise = async () => {
await timeout(100);
try {
await assertions(getTestData, setState);
} catch (error) {
console.warn(error);
throw error;
}
};
let errorCallback;
let consoleErrorCallback;
const errorPromise = new Promise((resolve, reject) => {
errorCallback = (event) => {
console.warn(error);
reject(event);
};
window.addEventListener("error", errorCallback);
});
const consoleErrorPromise = new Promise((resolve, reject) => {
consoleErrorCallback = (...data) => reject(new Error(data));
addConsoleErrorListener(consoleErrorCallback);
});
const resultPromise = Promise.race([assertionPromise(), errorPromise, consoleErrorPromise])
.catch((err) => setError(err))
.then(() => {
window.removeEventListener("error", errorCallback);
removeConsoleErrorListener(consoleErrorCallback);
// eslint-disable-next-line no-console
console.log("TEST FINISHED");
});
window.waitFor = () => resultPromise;
}, []);
return error ? (
<div style={{ height: "100%", width: "100%", overflow: "scroll" }}>
<pre>{error.stack}</pre>
</div>
) : (
story(setTestData, state)
);
}
return () => <Component />;
}