Skip to content

Commit 69060e1

Browse files
authored
Swap expect(ReactNoop) for expect(Scheduler) (#14971)
* Swap expect(ReactNoop) for expect(Scheduler) In the previous commits, I upgraded our custom Jest matchers for the noop and test renderers to use Scheduler under the hood. Now that all these matchers are using Scheduler, we can drop support for passing ReactNoop and test roots and always pass Scheduler directly. * Externalize Scheduler in noop and test bundles I also noticed we don't need to regenerator runtime in noop anymore.
1 parent ccb2a8a commit 69060e1

35 files changed

+1184
-1277
lines changed

packages/create-subscription/src/__tests__/createSubscription-test.internal.js

+30-28
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let BehaviorSubject;
1414
let ReactFeatureFlags;
1515
let React;
1616
let ReactNoop;
17+
let Scheduler;
1718
let ReplaySubject;
1819

1920
describe('createSubscription', () => {
@@ -24,6 +25,7 @@ describe('createSubscription', () => {
2425
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
2526
React = require('react');
2627
ReactNoop = require('react-noop-renderer');
28+
Scheduler = require('scheduler');
2729

2830
BehaviorSubject = require('rxjs/BehaviorSubject').BehaviorSubject;
2931
ReplaySubject = require('rxjs/ReplaySubject').ReplaySubject;
@@ -65,16 +67,16 @@ describe('createSubscription', () => {
6567
);
6668

6769
// Updates while subscribed should re-render the child component
68-
expect(ReactNoop).toFlushAndYield(['default']);
70+
expect(Scheduler).toFlushAndYield(['default']);
6971
observable.next(123);
70-
expect(ReactNoop).toFlushAndYield([123]);
72+
expect(Scheduler).toFlushAndYield([123]);
7173
observable.next('abc');
72-
expect(ReactNoop).toFlushAndYield(['abc']);
74+
expect(Scheduler).toFlushAndYield(['abc']);
7375

7476
// Unmounting the subscriber should remove listeners
7577
ReactNoop.render(<div />);
7678
observable.next(456);
77-
expect(ReactNoop).toFlushAndYield([]);
79+
expect(Scheduler).toFlushAndYield([]);
7880
});
7981

8082
it('should support observable types like RxJS ReplaySubject', () => {
@@ -102,13 +104,13 @@ describe('createSubscription', () => {
102104
const observable = createReplaySubject('initial');
103105

104106
ReactNoop.render(<Subscription source={observable}>{render}</Subscription>);
105-
expect(ReactNoop).toFlushAndYield(['initial']);
107+
expect(Scheduler).toFlushAndYield(['initial']);
106108
observable.next('updated');
107-
expect(ReactNoop).toFlushAndYield(['updated']);
109+
expect(Scheduler).toFlushAndYield(['updated']);
108110

109111
// Unsetting the subscriber prop should reset subscribed values
110112
ReactNoop.render(<Subscription>{render}</Subscription>);
111-
expect(ReactNoop).toFlushAndYield(['default']);
113+
expect(Scheduler).toFlushAndYield(['default']);
112114
});
113115

114116
describe('Promises', () => {
@@ -141,19 +143,19 @@ describe('createSubscription', () => {
141143

142144
// Test a promise that resolves after render
143145
ReactNoop.render(<Subscription source={promiseA}>{render}</Subscription>);
144-
expect(ReactNoop).toFlushAndYield(['loading']);
146+
expect(Scheduler).toFlushAndYield(['loading']);
145147
resolveA(true);
146148
await promiseA;
147-
expect(ReactNoop).toFlushAndYield(['finished']);
149+
expect(Scheduler).toFlushAndYield(['finished']);
148150

149151
// Test a promise that resolves before render
150152
// Note that this will require an extra render anyway,
151153
// Because there is no way to synchronously get a Promise's value
152154
rejectB(false);
153155
ReactNoop.render(<Subscription source={promiseB}>{render}</Subscription>);
154-
expect(ReactNoop).toFlushAndYield(['loading']);
156+
expect(Scheduler).toFlushAndYield(['loading']);
155157
await promiseB.catch(() => true);
156-
expect(ReactNoop).toFlushAndYield(['failed']);
158+
expect(Scheduler).toFlushAndYield(['failed']);
157159
});
158160

159161
it('should still work if unsubscription is managed incorrectly', async () => {
@@ -177,17 +179,17 @@ describe('createSubscription', () => {
177179

178180
// Subscribe first to Promise A then Promise B
179181
ReactNoop.render(<Subscription source={promiseA}>{render}</Subscription>);
180-
expect(ReactNoop).toFlushAndYield(['default']);
182+
expect(Scheduler).toFlushAndYield(['default']);
181183
ReactNoop.render(<Subscription source={promiseB}>{render}</Subscription>);
182-
expect(ReactNoop).toFlushAndYield(['default']);
184+
expect(Scheduler).toFlushAndYield(['default']);
183185

184186
// Resolve both Promises
185187
resolveB(123);
186188
resolveA('abc');
187189
await Promise.all([promiseA, promiseB]);
188190

189191
// Ensure that only Promise B causes an update
190-
expect(ReactNoop).toFlushAndYield([123]);
192+
expect(Scheduler).toFlushAndYield([123]);
191193
});
192194

193195
it('should not call setState for a Promise that resolves after unmount', async () => {
@@ -211,11 +213,11 @@ describe('createSubscription', () => {
211213
});
212214

213215
ReactNoop.render(<Subscription source={promise}>{render}</Subscription>);
214-
expect(ReactNoop).toFlushAndYield(['rendered']);
216+
expect(Scheduler).toFlushAndYield(['rendered']);
215217

216218
// Unmount
217219
ReactNoop.render(null);
218-
expect(ReactNoop).toFlushWithoutYielding();
220+
expect(Scheduler).toFlushWithoutYielding();
219221

220222
// Resolve Promise should not trigger a setState warning
221223
resolvePromise(true);
@@ -245,21 +247,21 @@ describe('createSubscription', () => {
245247
);
246248

247249
// Updates while subscribed should re-render the child component
248-
expect(ReactNoop).toFlushAndYield(['a-0']);
250+
expect(Scheduler).toFlushAndYield(['a-0']);
249251

250252
// Unsetting the subscriber prop should reset subscribed values
251253
ReactNoop.render(
252254
<Subscription source={observableB}>{render}</Subscription>,
253255
);
254-
expect(ReactNoop).toFlushAndYield(['b-0']);
256+
expect(Scheduler).toFlushAndYield(['b-0']);
255257

256258
// Updates to the old subscribable should not re-render the child component
257259
observableA.next('a-1');
258-
expect(ReactNoop).toFlushAndYield([]);
260+
expect(Scheduler).toFlushAndYield([]);
259261

260262
// Updates to the bew subscribable should re-render the child component
261263
observableB.next('b-1');
262-
expect(ReactNoop).toFlushAndYield(['b-1']);
264+
expect(Scheduler).toFlushAndYield(['b-1']);
263265
});
264266

265267
it('should ignore values emitted by a new subscribable until the commit phase', () => {
@@ -315,12 +317,12 @@ describe('createSubscription', () => {
315317
const observableB = createBehaviorSubject('b-0');
316318

317319
ReactNoop.render(<Parent observed={observableA} />);
318-
expect(ReactNoop).toFlushAndYield(['Subscriber: a-0', 'Child: a-0']);
320+
expect(Scheduler).toFlushAndYield(['Subscriber: a-0', 'Child: a-0']);
319321
expect(log).toEqual(['Parent.componentDidMount']);
320322

321323
// Start React update, but don't finish
322324
ReactNoop.render(<Parent observed={observableB} />);
323-
expect(ReactNoop).toFlushAndYieldThrough(['Subscriber: b-0']);
325+
expect(Scheduler).toFlushAndYieldThrough(['Subscriber: b-0']);
324326
expect(log).toEqual(['Parent.componentDidMount']);
325327

326328
// Emit some updates from the uncommitted subscribable
@@ -335,7 +337,7 @@ describe('createSubscription', () => {
335337
// We expect the last emitted update to be rendered (because of the commit phase value check)
336338
// But the intermediate ones should be ignored,
337339
// And the final rendered output should be the higher-priority observable.
338-
expect(ReactNoop).toFlushAndYield([
340+
expect(Scheduler).toFlushAndYield([
339341
'Child: b-0',
340342
'Subscriber: b-3',
341343
'Child: b-3',
@@ -402,12 +404,12 @@ describe('createSubscription', () => {
402404
const observableB = createBehaviorSubject('b-0');
403405

404406
ReactNoop.render(<Parent observed={observableA} />);
405-
expect(ReactNoop).toFlushAndYield(['Subscriber: a-0', 'Child: a-0']);
407+
expect(Scheduler).toFlushAndYield(['Subscriber: a-0', 'Child: a-0']);
406408
expect(log).toEqual(['Parent.componentDidMount']);
407409

408410
// Start React update, but don't finish
409411
ReactNoop.render(<Parent observed={observableB} />);
410-
expect(ReactNoop).toFlushAndYieldThrough(['Subscriber: b-0']);
412+
expect(Scheduler).toFlushAndYieldThrough(['Subscriber: b-0']);
411413
expect(log).toEqual(['Parent.componentDidMount']);
412414

413415
// Emit some updates from the old subscribable
@@ -420,7 +422,7 @@ describe('createSubscription', () => {
420422
// Flush everything and ensure that the correct subscribable is used
421423
// We expect the new subscribable to finish rendering,
422424
// But then the updated values from the old subscribable should be used.
423-
expect(ReactNoop).toFlushAndYield([
425+
expect(Scheduler).toFlushAndYield([
424426
'Child: b-0',
425427
'Subscriber: a-2',
426428
'Child: a-2',
@@ -433,7 +435,7 @@ describe('createSubscription', () => {
433435

434436
// Updates from the new subscribable should be ignored.
435437
observableB.next('b-1');
436-
expect(ReactNoop).toFlushAndYield([]);
438+
expect(Scheduler).toFlushAndYield([]);
437439
expect(log).toEqual([
438440
'Parent.componentDidMount',
439441
'Parent.componentDidUpdate',
@@ -479,7 +481,7 @@ describe('createSubscription', () => {
479481
<Subscription source={observable}>{value => null}</Subscription>,
480482
);
481483

482-
expect(ReactNoop).toFlushAndThrow(
484+
expect(Scheduler).toFlushAndThrow(
483485
'A subscription must return an unsubscribe function.',
484486
);
485487
});

packages/jest-react/src/JestReact.js

-48
Original file line numberDiff line numberDiff line change
@@ -35,54 +35,6 @@ function assertYieldsWereCleared(root) {
3535
);
3636
}
3737

38-
export function unstable_toFlushAndYield(root, expectedYields) {
39-
assertYieldsWereCleared(root);
40-
const actualYields = root.unstable_flushAll();
41-
return captureAssertion(() => {
42-
expect(actualYields).toEqual(expectedYields);
43-
});
44-
}
45-
46-
export function unstable_toFlushAndYieldThrough(root, expectedYields) {
47-
assertYieldsWereCleared(root);
48-
const actualYields = root.unstable_flushNumberOfYields(expectedYields.length);
49-
return captureAssertion(() => {
50-
expect(actualYields).toEqual(expectedYields);
51-
});
52-
}
53-
54-
export function unstable_toFlushWithoutYielding(root) {
55-
return unstable_toFlushAndYield(root, []);
56-
}
57-
58-
export function unstable_toHaveYielded(ReactTestRenderer, expectedYields) {
59-
return captureAssertion(() => {
60-
if (
61-
ReactTestRenderer === null ||
62-
typeof ReactTestRenderer !== 'object' ||
63-
typeof ReactTestRenderer.unstable_setNowImplementation !== 'function'
64-
) {
65-
invariant(
66-
false,
67-
'The matcher `unstable_toHaveYielded` expects an instance of React Test ' +
68-
'Renderer.\n\nTry: ' +
69-
'expect(ReactTestRenderer).unstable_toHaveYielded(expectedYields)',
70-
);
71-
}
72-
const actualYields = ReactTestRenderer.unstable_clearYields();
73-
expect(actualYields).toEqual(expectedYields);
74-
});
75-
}
76-
77-
export function unstable_toFlushAndThrow(root, ...rest) {
78-
assertYieldsWereCleared(root);
79-
return captureAssertion(() => {
80-
expect(() => {
81-
root.unstable_flushAll();
82-
}).toThrow(...rest);
83-
});
84-
}
85-
8638
export function unstable_toMatchRenderedOutput(root, expectedJSX) {
8739
assertYieldsWereCleared(root);
8840
const actualJSON = root.toJSON();

packages/react-art/src/__tests__/ReactART-test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const Wedge = require('react-art/Wedge');
3131
// Isolate the noop renderer
3232
jest.resetModules();
3333
const ReactNoop = require('react-noop-renderer');
34+
const Scheduler = require('scheduler');
3435

3536
let Group;
3637
let Shape;
@@ -385,7 +386,7 @@ describe('ReactART', () => {
385386
</CurrentRendererContext.Provider>,
386387
);
387388

388-
expect(ReactNoop).toFlushAndYieldThrough(['A']);
389+
expect(Scheduler).toFlushAndYieldThrough(['A']);
389390

390391
ReactDOM.render(
391392
<Surface>
@@ -400,7 +401,7 @@ describe('ReactART', () => {
400401
expect(ops).toEqual([null, 'ART']);
401402

402403
ops = [];
403-
expect(ReactNoop).toFlushAndYield(['B', 'C']);
404+
expect(Scheduler).toFlushAndYield(['B', 'C']);
404405

405406
expect(ops).toEqual(['Test']);
406407
});

0 commit comments

Comments
 (0)