8000 Codemod tests to waitFor pattern (9/?) (#26309) · facebook/react@25685d8 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 25685d8

Browse files
authored
Codemod tests to waitFor pattern (9/?) (#26309)
This converts some of our test suite to use the `waitFor` test pattern, instead of the `expect(Scheduler).toFlushAndYield` pattern. Most of these changes are automated with jscodeshift, with some slight manual cleanup in certain cases. See #26285 for full context.
1 parent 64dde70 commit 25685d8

22 files changed

+233
-217
lines changed

packages/react-cache/src/__tests__/ReactCacheOld-test.internal.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ let TextResource;
2020
let textResourceShouldFail;
2121
let waitForAll;
2222
let assertLog;
23+
let waitForThrow;
2324

2425
describe('ReactCache', () => {
2526
beforeEach(() => {
@@ -38,6 +39,7 @@ describe('ReactCache', () => {
3839
const InternalTestUtils = require('internal-test-utils');
3940
waitForAll = InternalTestUtils.waitForAll;
4041
assertLog = InternalTestUtils.assertLog;
42+
waitForThrow = InternalTestUtils.waitForThrow;
4143

4244
TextResource = createResource(
4345
([text, ms = 0]) => {
@@ -150,12 +152,12 @@ describe('ReactCache', () => {
150152
jest.advanceTimersByTime(100);
151153
assertLog(['Promise rejected [Hi]']);
152154

153-
expect(Scheduler).toFlushAndThrow('Failed to load: Hi');
155+
await waitForThrow('Failed to load: Hi');
154156
assertLog(['Error! [Hi]', 'Error! [Hi]']);
155157

156158
// Should throw again on a subsequent read
157159
root.update(<App />);
158-
expect(Scheduler).toFlushAndThrow('Failed to load: Hi');
160+
await waitForThrow('Failed to load: Hi');
159161
assertLog(['Error! [Hi]', 'Error! [Hi]']);
160162
});
161163

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4398,7 +4398,7 @@ background-color: green;
43984398
</body>
43994399
</html>,
44004400
);
4401-
expect(Scheduler).toFlushWithoutYielding();
4401+
await waitForAll([]);
44024402
expect(getMeaningfulChildren(document)).toEqual(
44034403
<html>
44044404
<head>

packages/react-dom/src/__tests__/ReactDOMServerSelectiveHydration-test.internal.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,17 +1092,19 @@ describe('ReactDOMServerSelectiveHydration', () => {
10921092
const innerHTML = ReactDOMServer.renderToString(<InnerApp />);
10931093
innerContainer.innerHTML = innerHTML;
10941094

1095-
expect(OuterScheduler).toHaveYielded(['Outer']);
1096-
expect(InnerScheduler).toHaveYielded(['Inner']);
1095+
expect(OuterScheduler.unstable_clearYields()).toEqual(['Outer']);
1096+
expect(InnerScheduler.unstable_clearYields()).toEqual(['Inner']);
10971097

10981098
suspendOuter = true;
10991099
suspendInner = true;
11001100

11011101
OuterReactDOMClient.hydrateRoot(outerContainer, <OuterApp />);
11021102
InnerReactDOMClient.hydrateRoot(innerContainer, <InnerApp />);
11031103

1104-
expect(OuterScheduler).toFlushAndYield(['Suspend Outer']);
1105-
expect(InnerScheduler).toFlushAndYield(['Suspend Inner']);
1104+
OuterScheduler.unstable_flushAllWithoutAsserting();
1105+
InnerScheduler.unstable_flushAllWithoutAsserting();
1106+
expect(OuterScheduler.unstable_clearYields()).toEqual(['Suspend Outer']);
1107+
expect(InnerScheduler.unstable_clearYields()).toEqual(['Suspend Inner']);
11061108

11071109
innerDiv = document.querySelector('#inner');
11081110

@@ -1115,7 +1117,7 @@ describe('ReactDOMServerSelectiveHydration', () => {
11151117
InnerScheduler.unstable_flushAllWithoutAsserting();
11161118
});
11171119

1118-
expect(OuterScheduler).toHaveYielded(['Suspend Outer']);
1120+
expect(OuterScheduler.unstable_clearYields()).toEqual(['Suspend Outer']);
11191121
if (
11201122
gate(
11211123
flags =>
@@ -1124,10 +1126,12 @@ describe('ReactDOMServerSelectiveHydration', () => {
11241126
) {
11251127
// InnerApp doesn't see the event because OuterApp calls stopPropagation in
11261128
// capture phase since the event is blocked on suspended component
1127-
expect(InnerScheduler).toHaveYielded([]);
1129+
expect(InnerScheduler.unstable_clearYields()).toEqual([]);
11281130
} else {
11291131
// no stopPropagation
1130-
expect(InnerScheduler).toHaveYielded(['Suspend Inner']);
1132+
expect(InnerScheduler.unstable_clearYields()).toEqual([
1133+
'Suspend Inner',
1134+
]);
11311135
}
11321136

11331137
assertLog([]);
@@ -1149,15 +1153,15 @@ describe('ReactDOMServerSelectiveHydration', () => {
11491153
InnerScheduler.unstable_flushAllWithoutAsserting();
11501154
});
11511155

1152-
expect(OuterScheduler).toHaveYielded(['Suspend Outer']);
1156+
expect(OuterScheduler.unstable_clearYields()).toEqual(['Suspend Outer']);
11531157
// Inner App renders because it is unblocked
1154-
expect(InnerScheduler).toHaveYielded(['Inner']);
1158+
expect(InnerScheduler.unstable_clearYields()).toEqual(['Inner']);
11551159
// No event is replayed yet
11561160
assertLog([]);
11571161

11581162
dispatchMouseHoverEvent(innerDiv);
1159-
expect(OuterScheduler).toHaveYielded([]);
1160-
expect(InnerScheduler).toHaveYielded([]);
1163+
expect(OuterScheduler.unstable_clearYields()).toEqual([]);
1164+
expect(InnerScheduler.unstable_clearYields()).toEqual([]);
11611165
// No event is replayed yet
11621166
assertLog([]);
11631167

@@ -1172,9 +1176,9 @@ describe('ReactDOMServerSelectiveHydration', () => {
11721176

11731177
// Nothing happens to inner app yet.
11741178
// Its blocked on the outer app replaying the event
1175-
expect(InnerScheduler).toHaveYielded([]);
1179+
expect(InnerScheduler.unstable_clearYields()).toEqual([]);
11761180
// Outer hydrates and schedules Replay
1177-
expect(OuterScheduler).toHaveYielded(['Outer']);
1181+
expect(OuterScheduler.unstable_clearYields()).toEqual(['Outer']);
11781182
// No event is replayed yet
11791183
assertLog([]);
11801184

@@ -1203,9 +1207,9 @@ describe('ReactDOMServerSelectiveHydration', () => {
12031207
});
12041208

12051209
// Outer resolves and scheduled replay
1206-
expect(OuterScheduler).toHaveYielded(['Outer']);
1210+
expect(OuterScheduler.unstable_clearYields()).toEqual(['Outer']);
12071211
// Inner App is still blocked
1208-
expect(InnerScheduler).toHaveYielded([]);
1212+
expect(InnerScheduler.unstable_clearYields()).toEqual([]);
12091213

12101214
// Replay outer event
12111215
await act(async () => {
@@ -1217,12 +1221,12 @@ describe('ReactDOMServerSelectiveHydration', () => {
12171221
// Inner is still blocked so when Outer replays the event in capture phase
12181222
// inner ends up caling stopPropagation
12191223
assertLog([]);
1220-
expect(OuterScheduler).toHaveYielded([]);
1221-
expect(InnerScheduler).toHaveYielded(['Suspend Inner']);
1224+
expect(OuterScheduler.unstable_clearYields()).toEqual([]);
1225+
expect(InnerScheduler.unstable_clearYields()).toEqual(['Suspend Inner']);
12221226

12231227
dispatchMouseHoverEvent(innerDiv);
1224-
expect(OuterScheduler).toHaveYielded([]);
1225-
expect(InnerScheduler).toHaveYielded([]);
1228+
expect(OuterScheduler.unstable_clearYields()).toEqual([]);
1229+
expect(InnerScheduler.unstable_clearYields()).toEqual([]);
12261230
assertLog([]);
12271231

12281232
await act(async () => {
@@ -1234,9 +1238,9 @@ describe('ReactDOMServerSelectiveHydration', () => {
12341238
});
12351239

12361240
// Inner hydrates
1237-
expect(InnerScheduler).toHaveYielded(['Inner']);
1241+
expect(InnerScheduler.unstable_clearYields()).toEqual(['Inner']);
12381242
// Outer was hydrated earlier
1239-
expect(OuterScheduler).toHaveYielded([]);
1243+
expect(OuterScheduler.unstable_clearYields()).toEqual([]);
12401244

12411245
await act(async () => {
12421246
Scheduler.unstable_flushAllWithoutAsserting();

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
let React;
1313
let ReactNoop;
14-
let Scheduler;
1514
let JSXDEVRuntime;
1615
let waitForAll;
1716

@@ -20,15 +19,14 @@ describe('ReactDeprecationWarnings', () => {
2019
jest.resetModules();
2120
React = require('react');
2221
ReactNoop = require('react-noop-renderer');
23-
Scheduler = require('scheduler');
2422
const InternalTestUtils = require('internal-test-utils');
2523
waitForAll = InternalTestUtils.waitForAll;
2624
if (__DEV__) {
2725
JSXDEVRuntime = require('react/jsx-dev-runtime');
2826
}
2927
});
3028

31-
it('should warn when given defaultProps', () => {
29+
it('should warn when given defaultProps', async () => {
3230
function FunctionalComponent(props) {
3331
return null;
3432
}
@@ -38,14 +36,14 @@ describe('ReactDeprecationWarnings', () => {
3836
};
3937

4038
ReactNoop.render(<FunctionalComponent />);
41-
expect(() => expect(Scheduler).toFlushWithoutYielding()).toErrorDev(
39+
await expect(async () => await waitForAll([])).toErrorDev(
4240
'Warning: FunctionalComponent: Support for defaultProps ' +
4341
'will be removed from function components in a future major ' +
4442
'release. Use JavaScript default parameters instead.',
4543
);
4644
});
4745

48-
it('should warn when given defaultProps on a memoized function', () => {
46+
it('should warn when given defaultProps on a memoized function', async () => {
4947
const MemoComponent = React.memo(function FunctionalComponent(props) {
5048
return null;
5149
});
@@ -59,14 +57,14 @@ describe('ReactDeprecationWarnings', () => {
5957
<MemoComponent />
6058
</div>,
6159
);
62-
expect(() => expect(Scheduler).toFlushWithoutYielding()).toErrorDev(
60+
await expect(async () => await waitForAll([])).toErrorDev(
6361
'Warning: FunctionalComponent: Support for defaultProps ' +
6462
'will be removed from memo components in a future major ' +
6563
'release. Use JavaScript default parameters instead.',
6664
);
6765
});
6866

69-
it('should warn when given string refs', () => {
67+
it('should warn when given string refs', async () => {
7068
class RefComponent extends React.Component {
7169
render() {
7270
return null;
@@ -79,7 +77,7 @@ describe('ReactDeprecationWarnings', () => {
7977
}
8078

8179
ReactNoop.render(<Component />);
82-
expect(() => expect(Scheduler).toFlushWithoutYielding()).toErrorDev(
80+
await expect(async () => await waitForAll([])).toErrorDev(
8381
'Warning: Component "Component" contains the string ref "refComponent". ' +
8482
'Support for string refs will be removed in a future major release. ' +
8583
'We recommend using useRef() or createRef() instead. ' +
@@ -108,7 +106,7 @@ describe('ReactDeprecationWarnings', () => {
108106
await waitForAll([]);
109107
});
110108

111-
it('should warn when owner and self are different for string refs', () => {
109+
it('should warn when owner and self are different for string refs', async () => {
112110
class RefComponent extends React.Component {
113111
render() {
114112
return null;
@@ -121,7 +119,7 @@ describe('ReactDeprecationWarnings', () => {
121119
}
122120

123121
ReactNoop.render(<Component />);
124-
expect(() => expect(Scheduler).toFlushWithoutYielding()).toErrorDev([
122+
await expect(async () => await waitForAll([])).toErrorDev([
125123
'Warning: Component "Component" contains the string ref "refComponent". ' +
126124
'Support for string refs will be removed in a future major release. ' +
127125
'This case cannot be automatically converted to an arrow function. ' +
@@ -132,7 +130,7 @@ describe('ReactDeprecationWarnings', () => {
132130
});
133131

134132
if (__DEV__) {
135-
it('should warn when owner and self are different for string refs', () => {
133+
it('should warn when owner and self are different for string refs', async () => {
136134
class RefComponent extends React.Component {
137135
render() {
138136
return null;
@@ -152,7 +150,7 @@ describe('ReactDeprecationWarnings', () => {
152150
}
153151

154152
ReactNoop.render(<Component />);
155-
expect(() => expect(Scheduler).toFlushWithoutYielding()).toErrorDev(
153+
await expect(async () => await waitForAll([])).toErrorDev(
156154
'Warning: Component "Component" contains the string ref "refComponent". ' +
157155
'Support for string refs will be removed in a future major release. ' +
158156
'This case cannot be automatically converted to an arrow function. ' +

packages/react-reconciler/src/__tests__/ReactDisableSchedulerTimeoutBasedOnReactExpirationTime-test.internal.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ let Suspense;
66
let scheduleCallback;
77
let NormalPriority;
88
let waitForAll;
9+
let waitFor;
910

1011
describe('ReactSuspenseList', () => {
1112
beforeEach(() => {
@@ -24,6 +25,7 @@ describe('ReactSuspenseList', () => {
2425

2526
const InternalTestUtils = require('internal-test-utils');
2627
waitForAll = InternalTestUtils.waitForAll;
28+
waitFor = InternalTestUtils.waitFor;
2729
});
2830

2931
function Text(props) {
@@ -86,11 +88,11 @@ describe('ReactSuspenseList', () => {
8688
});
8789

8890
// This resolves A and schedules a task for React to retry.
89-
await expect(Scheduler).toFlushAndYieldThrough(['Resolve A']);
91+
await waitFor(['Resolve A']);
9092

9193
// The next task that flushes should be the one that resolves B. The render
9294
// task should not jump the queue ahead of B.
93-
await expect(Scheduler).toFlushAndYieldThrough(['Resolve B']);
95+
await waitFor(['Resolve B']);
9496

9597
await waitForAll(['A', 'B']);
9698
expect(root).toMatchRenderedOutput('AB');

packages/react-reconciler/src/__tests__/ReactFragment-test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
let React;
1313
let ReactNoop;
14-
let Scheduler;
1514
let waitForAll;
1615

1716
describe('ReactFragment', () => {
@@ -20,7 +19,6 @@ describe('ReactFragment', () => {
2019

2120
React = require('react');
2221
ReactNoop = require('react-noop-renderer');
23-
Scheduler = require('scheduler');
2422

2523
const InternalTestUtils = require('internal-test-utils');
2624
waitForAll = InternalTestUtils.waitForAll;
@@ -707,7 +705,7 @@ describe('ReactFragment', () => {
707705
);
708706
});
709707

710-
it('should not preserve state when switching to a keyed fragment to an array', async function () {
708+
it('should not preserve state when switching to a keyed fragment to an array', async () => {
711709
const ops = [];
712710

713711
class Stateful extends React.Component {
@@ -742,7 +740,7 @@ describe('ReactFragment', () => {
742740
await waitForAll([]);
743741

744742
ReactNoop.render(<Foo condition={false} />);
745-
expect(() => expect(Scheduler).toFlushWithoutYielding()).toErrorDev(
743+
await expect(async () => await waitForAll([])).toErrorDev(
746744
'Each child in a list should have a unique "key" prop.',
747745
);
748746

@@ -939,7 +937,7 @@ describe('ReactFragment', () => {
939937
}
940938

941939
ReactNoop.render(<Foo condition={true} />);
942-
expect(() => expect(Scheduler).toFlushWithoutYielding()).toErrorDev(
940+
await expect(async () => await waitForAll([])).toErrorDev(
943941
'Each child in a list should have a unique "key" prop.',
944942
);
945943

0 commit comments

Comments
 (0)
0