-
Notifications
You must be signed in to change notification settings - Fork 45
/
act-examples.test.js
163 lines (133 loc) · 3.46 KB
/
act-examples.test.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
const React = require("react");
const { useState, useEffect } = React;
const ReactDOM = require("react-dom");
const { act } = require("react-dom/test-utils");
// please see README.md for explanations
describe("act and jest, sitting in a tree", () => {
it("can flush effects", () => {
function App() {
let [ctr, setCtr] = useState(0);
useEffect(() => {
setCtr(1);
}, []);
return ctr;
}
const el = document.createElement("div");
act(() => {
ReactDOM.render(<App />, el);
});
expect(el.innerHTML).toBe("1");
});
it("can handle multiple events", () => {
function App() {
let [ctr, setCtr] = useState(0);
return <button onClick={() => setCtr(x => x + 1)}>{ctr}</button>;
}
const el = document.createElement("div");
// we attach the element to body to ensure events work
document.body.appendChild(el);
ReactDOM.render(<App />, el);
const button = el.childNodes[0];
act(() => {
for (let i = 0; i < 3; i++) {
button.dispatchEvent(new MouseEvent("click", { bubbles: true }));
}
});
expect(button.innerHTML).toBe("3");
});
it("can handle fake timers", () => {
jest.useFakeTimers();
function App() {
const [ctr, setCtr] = useState(0);
useEffect(() => {
setTimeout(() => setCtr(1), 1000);
}, []);
return ctr;
}
const el = document.createElement("div");
act(() => {
ReactDOM.render(<App />, el);
});
expect(el.innerHTML).toBe("0");
act(() => {
jest.runAllTimers();
});
expect(el.innerHTML).toBe("1");
jest.useRealTimers();
});
it("can handle real timers", async () => {
function sleep(period) {
return new Promise((resolve, reject) => {
setTimeout(resolve, period);
});
}
function App() {
const [ctr, setCtr] = useState(0);
useEffect(() => {
setTimeout(() => setCtr(1), 1000);
}, []);
return ctr;
}
const el = document.createElement("div");
act(() => {
ReactDOM.render(<App />, el);
});
expect(el.innerHTML).toBe("0");
await act(async () => {
await sleep(1200);
});
expect(el.innerHTML).toBe("1");
});
it("can handle promises", async () => {
let resolve;
function fetch() {
return new Promise(_resolve => {
resolve = _resolve;
});
}
function App() {
let [data, setData] = useState(null);
useEffect(() => {
fetch("/some/url").then(setData);
}, []);
return data;
}
const el = document.createElement("div");
await act(async () => {
ReactDOM.render(<App />, el);
});
expect(el.innerHTML).toBe("");
await act(async () => {
resolve(42);
});
expect(el.innerHTML).toBe("42");
});
it("can handle async/await", async () => {
let resolve;
function fetch() {
return new Promise(_resolve => {
resolve = _resolve;
});
}
function App() {
let [data, setData] = useState(null);
async function somethingAsync() {
let response = await fetch("/some/url");
setData(response);
}
useEffect(() => {
somethingAsync();
}, []);
return data;
}
const el = document.createElement("div");
act(() => {
ReactDOM.render(<App />, el);
});
expect(el.innerHTML).toBe("");
await act(async () => {
resolve(42);
});
expect(el.innerHTML).toBe("42");
});
});