Skip to content

Commit 8e25ed2

Browse files
authored
Unify noop and test renderer assertion APIs (#14952)
* Throw in tests if work is done before emptying log Test renderer already does this. Makes it harder to miss unexpected behavior by forcing you to assert on every logged value. * Convert ReactNoop tests to use jest matchers The matchers warn if work is flushed while the log is empty. This is the pattern we already follow for test renderer. I've used the same APIs as test renderer, so it should be easy to switch between the two.
1 parent 870214f commit 8e25ed2

30 files changed

+1072
-1004
lines changed

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

+28-28
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ describe('createSubscription', () => {
6565
);
6666

6767
// Updates while subscribed should re-render the child component
68-
expect(ReactNoop.flush()).toEqual(['default']);
68+
expect(ReactNoop).toFlushAndYield(['default']);
6969
observable.next(123);
70-
expect(ReactNoop.flush()).toEqual([123]);
70+
expect(ReactNoop).toFlushAndYield([123]);
7171
observable.next('abc');
72-
expect(ReactNoop.flush()).toEqual(['abc']);
72+
expect(ReactNoop).toFlushAndYield(['abc']);
7373

7474
// Unmounting the subscriber should remove listeners
7575
ReactNoop.render(<div />);
7676
observable.next(456);
77-
expect(ReactNoop.flush()).toEqual([]);
77+
expect(ReactNoop).toFlushAndYield([]);
7878
});
7979

8080
it('should support observable types like RxJS ReplaySubject', () => {
@@ -102,13 +102,13 @@ describe('createSubscription', () => {
102102
const observable = createReplaySubject('initial');
103103

104104
ReactNoop.render(<Subscription source={observable}>{render}</Subscription>);
105-
expect(ReactNoop.flush()).toEqual(['initial']);
105+
expect(ReactNoop).toFlushAndYield(['initial']);
106106
observable.next('updated');
107-
expect(ReactNoop.flush()).toEqual(['updated']);
107+
expect(ReactNoop).toFlushAndYield(['updated']);
108108

109109
// Unsetting the subscriber prop should reset subscribed values
110110
ReactNoop.render(<Subscription>{render}</Subscription>);
111-
expect(ReactNoop.flush()).toEqual(['default']);
111+
expect(ReactNoop).toFlushAndYield(['default']);
112112
});
113113

114114
describe('Promises', () => {
@@ -141,19 +141,19 @@ describe('createSubscription', () => {
141141

142142
// Test a promise that resolves after render
143143
ReactNoop.render(<Subscription source={promiseA}>{render}</Subscription>);
144-
expect(ReactNoop.flush()).toEqual(['loading']);
144+
expect(ReactNoop).toFlushAndYield(['loading']);
145145
resolveA(true);
146146
await promiseA;
147-
expect(ReactNoop.flush()).toEqual(['finished']);
147+
expect(ReactNoop).toFlushAndYield(['finished']);
148148

149149
// Test a promise that resolves before render
150150
// Note that this will require an extra render anyway,
151151
// Because there is no way to synchronously get a Promise's value
152152
rejectB(false);
153153
ReactNoop.render(<Subscription source={promiseB}>{render}</Subscription>);
154-
expect(ReactNoop.flush()).toEqual(['loading']);
154+
expect(ReactNoop).toFlushAndYield(['loading']);
155155
await promiseB.catch(() => true);
156-
expect(ReactNoop.flush()).toEqual(['failed']);
156+
expect(ReactNoop).toFlushAndYield(['failed']);
157157
});
158158

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

178178
// Subscribe first to Promise A then Promise B
179179
ReactNoop.render(<Subscription source={promiseA}>{render}</Subscription>);
180-
expect(ReactNoop.flush()).toEqual(['default']);
180+
expect(ReactNoop).toFlushAndYield(['default']);
181181
ReactNoop.render(<Subscription source={promiseB}>{render}</Subscription>);
182-
expect(ReactNoop.flush()).toEqual(['default']);
182+
expect(ReactNoop).toFlushAndYield(['default']);
183183

184184
// Resolve both Promises
185185
resolveB(123);
186186
resolveA('abc');
187187
await Promise.all([promiseA, promiseB]);
188188

189189
// Ensure that only Promise B causes an update
190-
expect(ReactNoop.flush()).toEqual([123]);
190+
expect(ReactNoop).toFlushAndYield([123]);
191191
});
192192

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

213213
ReactNoop.render(<Subscription source={promise}>{render}</Subscription>);
214-
expect(ReactNoop.flush()).toEqual(['rendered']);
214+
expect(ReactNoop).toFlushAndYield(['rendered']);
215215

216216
// Unmount
217217
ReactNoop.render(null);
218-
ReactNoop.flush();
218+
expect(ReactNoop).toFlushWithoutYielding();
219219

220220
// Resolve Promise should not trigger a setState warning
221221
resolvePromise(true);
@@ -245,21 +245,21 @@ describe('createSubscription', () => {
245245
);
246246

247247
// Updates while subscribed should re-render the child component
248-
expect(ReactNoop.flush()).toEqual(['a-0']);
248+
expect(ReactNoop).toFlushAndYield(['a-0']);
249249

250250
// Unsetting the subscriber prop should reset subscribed values
251251
ReactNoop.render(
252252
<Subscription source={observableB}>{render}</Subscription>,
253253
);
254-
expect(ReactNoop.flush()).toEqual(['b-0']);
254+
expect(ReactNoop).toFlushAndYield(['b-0']);
255255

256256
// Updates to the old subscribable should not re-render the child component
257257
observableA.next('a-1');
258-
expect(ReactNoop.flush()).toEqual([]);
258+
expect(ReactNoop).toFlushAndYield([]);
259259

260260
// Updates to the bew subscribable should re-render the child component
261261
observableB.next('b-1');
262-
expect(ReactNoop.flush()).toEqual(['b-1']);
262+
expect(ReactNoop).toFlushAndYield(['b-1']);
263263
});
264264

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

317317
ReactNoop.render(<Parent observed={observableA} />);
318-
expect(ReactNoop.flush()).toEqual(['Subscriber: a-0', 'Child: a-0']);
318+
expect(ReactNoop).toFlushAndYield(['Subscriber: a-0', 'Child: a-0']);
319319
expect(log).toEqual(['Parent.componentDidMount']);
320320

321321
// Start React update, but don't finish
322322
ReactNoop.render(<Parent observed={observableB} />);
323-
ReactNoop.flushThrough(['Subscriber: b-0']);
323+
expect(ReactNoop).toFlushAndYieldThrough(['Subscriber: b-0']);
324324
expect(log).toEqual(['Parent.componentDidMount']);
325325

326326
// Emit some updates from the uncommitted subscribable
@@ -335,7 +335,7 @@ describe('createSubscription', () => {
335335
// We expect the last emitted update to be rendered (because of the commit phase value check)
336336
// But the intermediate ones should be ignored,
337337
// And the final rendered output should be the higher-priority observable.
338-
expect(ReactNoop.flush()).toEqual([
338+
expect(ReactNoop).toFlushAndYield([
339339
'Child: b-0',
340340
'Subscriber: b-3',
341341
'Child: b-3',
@@ -402,12 +402,12 @@ describe('createSubscription', () => {
402402
const observableB = createBehaviorSubject('b-0');
403403

404404
ReactNoop.render(<Parent observed={observableA} />);
405-
expect(ReactNoop.flush()).toEqual(['Subscriber: a-0', 'Child: a-0']);
405+
expect(ReactNoop).toFlushAndYield(['Subscriber: a-0', 'Child: a-0']);
406406
expect(log).toEqual(['Parent.componentDidMount']);
407407

408408
// Start React update, but don't finish
409409
ReactNoop.render(<Parent observed={observableB} />);
410-
ReactNoop.flushThrough(['Subscriber: b-0']);
410+
expect(ReactNoop).toFlushAndYieldThrough(['Subscriber: b-0']);
411411
expect(log).toEqual(['Parent.componentDidMount']);
412412

413413
// Emit some updates from the old subscribable
@@ -420,7 +420,7 @@ describe('createSubscription', () => {
420420
// Flush everything and ensure that the correct subscribable is used
421421
// We expect the new subscribable to finish rendering,
422422
// But then the updated values from the old subscribable should be used.
423-
expect(ReactNoop.flush()).toEqual([
423+
expect(ReactNoop).toFlushAndYield([
424424
'Child: b-0',
425425
'Subscriber: a-2',
426426
'Child: a-2',
@@ -433,7 +433,7 @@ describe('createSubscription', () => {
433433

434434
// Updates from the new subscribable should be ignored.
435435
observableB.next('b-1');
436-
expect(ReactNoop.flush()).toEqual([]);
436+
expect(ReactNoop).toFlushAndYield([]);
437437
expect(log).toEqual([
438438
'Parent.componentDidMount',
439439
'Parent.componentDidUpdate',
@@ -479,7 +479,7 @@ describe('createSubscription', () => {
479479
<Subscription source={observable}>{value => null}</Subscription>,
480480
);
481481

482-
expect(ReactNoop.flush).toThrow(
482+
expect(ReactNoop).toFlushAndThrow(
483483
'A subscription must return an unsubscribe function.',
484484
);
485485
});

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ describe('ReactART', () => {
385385
</CurrentRendererContext.Provider>,
386386
);
387387

388-
ReactNoop.flushThrough(['A']);
388+
expect(ReactNoop).toFlushAndYieldThrough(['A']);
389389

390390
ReactDOM.render(
391391
<Surface>
@@ -400,7 +400,7 @@ describe('ReactART', () => {
400400
expect(ops).toEqual([null, 'ART']);
401401

402402
ops = [];
403-
expect(ReactNoop.flush()).toEqual(['B', 'C']);
403+
expect(ReactNoop).toFlushAndYield(['B', 'C']);
404404

405405
expect(ops).toEqual(['Test']);
406406
});

0 commit comments

Comments
 (0)