Skip to content

Commit 88b66af

Browse files
author
Jack Pope
committed
Use createRoot in ReactTestUtilsActUnmockedScheduler-test
1 parent 8bb6ee1 commit 88b66af

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

packages/react-dom/src/__tests__/ReactTestUtilsActUnmockedScheduler-test.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
// sanity tests to make sure act() works without a mocked scheduler
1111

1212
let React;
13-
let ReactDOM;
13+
let ReactDOMClient;
1414
let act;
1515
let container;
1616
let yields;
17+
let prevActGlobal;
1718

1819
function clearLog() {
1920
try {
@@ -23,48 +24,43 @@ function clearLog() {
2324
}
2425
}
2526

26-
function render(el, dom) {
27-
ReactDOM.render(el, dom);
28-
}
29-
30-
function unmount(dom) {
31-
ReactDOM.unmountComponentAtNode(dom);
32-
}
33-
3427
beforeEach(() => {
28+
prevActGlobal = global.IS_REACT_ACT_ENVIRONMENT;
29+
global.IS_REACT_ACT_ENVIRONMENT = true;
3530
jest.resetModules();
3631
jest.unmock('scheduler');
3732
yields = [];
3833
React = require('react');
39-
ReactDOM = require('react-dom');
34+
ReactDOMClient = require('react-dom/client');
4035
act = React.unstable_act;
4136
container = document.createElement('div');
4237
document.body.appendChild(container);
4338
});
4439

4540
afterEach(() => {
46-
unmount(container);
41+
global.IS_REACT_ACT_ENVIRONMENT = prevActGlobal;
4742
document.body.removeChild(container);
4843
});
4944

5045
// @gate __DEV__
51-
it('can use act to flush effects', () => {
46+
it('can use act to flush effects', async () => {
5247
function App() {
5348
React.useEffect(() => {
5449
yields.push(100);
5550
});
5651
return null;
5752
}
5853

59-
act(() => {
60-
render(<App />, container);
54+
const root = ReactDOMClient.createRoot(container);
55+
await act(() => {
56+
root.render(<App />);
6157
});
6258

6359
expect(clearLog()).toEqual([100]);
6460
});
6561

6662
// @gate __DEV__
67-
it('flushes effects on every call', () => {
63+
it('flushes effects on every call', async () => {
6864
function App() {
6965
const [ctr, setCtr] = React.useState(0);
7066
React.useEffect(() => {
@@ -77,8 +73,9 @@ it('flushes effects on every call', () => {
7773
);
7874
}
7975

80-
act(() => {
81-
render(<App />, container);
76+
const root = ReactDOMClient.createRoot(container);
77+
await act(() => {
78+
root.render(<App />);
8279
});
8380

8481
expect(clearLog()).toEqual([0]);
@@ -103,7 +100,7 @@ it('flushes effects on every call', () => {
103100
});
104101

105102
// @gate __DEV__
106-
it("should keep flushing effects until they're done", () => {
103+
it("should keep flushing effects until they're done", async () => {
107104
function App() {
108105
const [ctr, setCtr] = React.useState(0);
109106
React.useEffect(() => {
@@ -114,25 +111,27 @@ it("should keep flushing effects until they're done", () => {
114111
return ctr;
115112
}
116113

117-
act(() => {
118-
render(<App />, container);
114+
const root = ReactDOMClient.createRoot(container);
115+
await act(() => {
116+
root.render(<App />);
119117
});
120118

121119
expect(container.innerHTML).toEqual('5');
122120
});
123121

124122
// @gate __DEV__
125-
it('should flush effects only on exiting the outermost act', () => {
123+
it('should flush effects only on exiting the outermost act', async () => {
126124
function App() {
127125
React.useEffect(() => {
128126
yields.push(0);
129127
});
130128
return null;
131129
}
130+
const root = ReactDOMClient.createRoot(container);
132131
// let's nest a couple of act() calls
133-
act(() => {
134-
act(() => {
135-
render(<App />, container);
132+
await act(async () => {
133+
await act(() => {
134+
root.render(<App />);
136135
});
137136
// the effect wouldn't have yielded yet because
138137
// we're still inside an act() scope
@@ -150,7 +149,9 @@ it('can handle cascading promises', async () => {
150149
const [state, setState] = React.useState(0);
151150
async function ticker() {
152151
await null;
153-
setState(x => x + 1);
152+
await act(() => {
153+
setState(x => x + 1);
154+
});
154155
}
155156
React.useEffect(() => {
156157
yields.push(state);
@@ -159,8 +160,9 @@ it('can handle cascading promises', async () => {
159160
return state;
160161
}
161162

162-
await act(async () => {
163-
render(<App />, container);
163+
const root = ReactDOMClient.createRoot(container);
164+
await act(() => {
165+
root.render(<App />);
164166
});
165167
// all 5 ticks present and accounted for
166168
expect(clearLog()).toEqual([0, 1, 2, 3, 4]);

0 commit comments

Comments
 (0)