Skip to content

Commit 8ef9fe9

Browse files
author
Brian Vaughn
committed
Test cleanup in response to eps1lon's PR feedback
1 parent a5d6b49 commit 8ef9fe9

File tree

1 file changed

+81
-164
lines changed

1 file changed

+81
-164
lines changed

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

Lines changed: 81 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,12 @@ describe('updaters', () => {
7777
await ReactTestUtils.act(async () => {
7878
ReactDOM.render(<Parent />, container);
7979
});
80-
expect(allSchedulerTags).toHaveLength(1);
81-
expect(allSchedulerTags[0]).toHaveLength(1);
82-
expect(allSchedulerTags[0]).toContain(HostRoot);
80+
expect(allSchedulerTags).toEqual([[HostRoot]]);
8381

8482
await ReactTestUtils.act(async () => {
8583
ReactDOM.render(<Parent />, container);
8684
});
87-
expect(allSchedulerTags).toHaveLength(2);
88-
expect(allSchedulerTags[1]).toHaveLength(1);
89-
expect(allSchedulerTags[1]).toContain(HostRoot);
85+
expect(allSchedulerTags).toEqual([[HostRoot], [HostRoot]]);
9086
});
9187

9288
it('should report a function component as the scheduler for a hooks update', async () => {
@@ -116,21 +112,21 @@ describe('updaters', () => {
116112
});
117113
expect(scheduleForA).not.toBeNull();
118114
expect(scheduleForB).not.toBeNull();
119-
expect(allSchedulerTypes).toHaveLength(1);
115+
expect(allSchedulerTypes).toEqual([[null]]);
120116

121117
await ReactTestUtils.act(async () => {
122118
scheduleForA();
123119
});
124-
expect(allSchedulerTypes).toHaveLength(2);
125-
expect(allSchedulerTypes[1]).toHaveLength(1);
126-
expect(allSchedulerTypes[1]).toContain(SchedulingComponentA);
120+
expect(allSchedulerTypes).toEqual([[null], [SchedulingComponentA]]);
127121

128122
await ReactTestUtils.act(async () => {
129123
scheduleForB();
130124
});
131-
expect(allSchedulerTypes).toHaveLength(3);
132-
expect(allSchedulerTypes[2]).toHaveLength(1);
133-
expect(allSchedulerTypes[2]).toContain(SchedulingComponentB);
125+
expect(allSchedulerTypes).toEqual([
126+
[null],
127+
[SchedulingComponentA],
128+
[SchedulingComponentB],
129+
]);
134130
});
135131

136132
it('should report a class component as the scheduler for a setState update', async () => {
@@ -147,15 +143,13 @@ describe('updaters', () => {
147143
await ReactTestUtils.act(async () => {
148144
ReactDOM.render(<Parent />, document.createElement('div'));
149145
});
150-
expect(allSchedulerTypes).toHaveLength(1);
146+
expect(allSchedulerTypes).toEqual([[null]]);
151147

152148
expect(instance).not.toBeNull();
153149
await ReactTestUtils.act(async () => {
154150
instance.setState({});
155151
});
156-
expect(allSchedulerTypes).toHaveLength(2);
157-
expect(allSchedulerTypes[1]).toHaveLength(1);
158-
expect(allSchedulerTypes[1]).toContain(SchedulingComponent);
152+
expect(allSchedulerTypes).toEqual([[null], [SchedulingComponent]]);
159153
});
160154

161155
// @gate experimental
@@ -198,7 +192,7 @@ describe('updaters', () => {
198192
});
199193
expect(triggerActiveCascade).not.toBeNull();
200194
expect(triggerPassiveCascade).not.toBeNull();
201-
expect(allSchedulerTypes).toHaveLength(1);
195+
expect(allSchedulerTypes).toEqual([[null]]);
202196

203197
await ReactTestUtils.act(async () => {
204198
triggerActiveCascade();
@@ -209,11 +203,11 @@ describe('updaters', () => {
209203
'onCommitRoot',
210204
]);
211205
});
212-
expect(allSchedulerTypes).toHaveLength(3);
213-
expect(allSchedulerTypes[1]).toHaveLength(1);
214-
expect(allSchedulerTypes[1]).toContain(SchedulingComponent);
215-
expect(allSchedulerTypes[2]).toHaveLength(1);
216-
expect(allSchedulerTypes[2]).toContain(CascadingChild);
206+
expect(allSchedulerTypes).toEqual([
207+
[null],
208+
[SchedulingComponent],
209+
[CascadingChild],
210+
]);
217211

218212
await ReactTestUtils.act(async () => {
219213
triggerPassiveCascade();
@@ -224,11 +218,13 @@ describe('updaters', () => {
224218
'onCommitRoot',
225219
]);
226220
});
227-
expect(allSchedulerTypes).toHaveLength(5);
228-
expect(allSchedulerTypes[3]).toHaveLength(1);
229-
expect(allSchedulerTypes[3]).toContain(SchedulingComponent);
230-
expect(allSchedulerTypes[4]).toHaveLength(1);
231-
expect(allSchedulerTypes[4]).toContain(CascadingChild);
221+
expect(allSchedulerTypes).toEqual([
222+
[null],
223+
[SchedulingComponent],
224+
[CascadingChild],
225+
[SchedulingComponent],
226+
[CascadingChild],
227+
]);
232228

233229
// Verify no outstanding flushes
234230
Scheduler.unstable_flushAll();
@@ -273,25 +269,21 @@ describe('updaters', () => {
273269
expect(Scheduler).toHaveYielded(['onCommitRoot']);
274270
});
275271
expect(setShouldSuspend).not.toBeNull();
276-
expect(allSchedulerTypes).toHaveLength(1);
272+
expect(allSchedulerTypes).toEqual([[null]]);
277273

278274
await ReactTestUtils.act(async () => {
279275
setShouldSuspend(true);
280276
});
281277
expect(Scheduler).toHaveYielded(['onCommitRoot']);
282-
expect(allSchedulerTypes).toHaveLength(2);
283-
expect(allSchedulerTypes[1]).toHaveLength(1);
284-
expect(allSchedulerTypes[1]).toContain(Suspender);
278+
expect(allSchedulerTypes).toEqual([[null], [Suspender]]);
285279

286280
expect(resolver).not.toBeNull();
287281
await ReactTestUtils.act(() => {
288282
resolver('abc');
289283
return promise;
290284
});
291285
expect(Scheduler).toHaveYielded(['onCommitRoot']);
292-
expect(allSchedulerTypes).toHaveLength(3);
293-
expect(allSchedulerTypes[2]).toHaveLength(1);
294-
expect(allSchedulerTypes[2]).toContain(Suspender);
286+
expect(allSchedulerTypes).toEqual([[null], [Suspender], [Suspender]]);
295287

296288
// Verify no outstanding flushes
297289
Scheduler.unstable_flushAll();
@@ -301,7 +293,10 @@ describe('updaters', () => {
301293

302294
// @gate experimental
303295
it('traces interaction through hidden subtree', async () => {
304-
const {HostRoot} = require('react-reconciler/src/ReactWorkTags');
296+
const {
297+
FunctionComponent,
298+
HostRoot,
299+
} = require('react-reconciler/src/ReactWorkTags');
305300

306301
// Note: This is based on a similar component we use in www. We can delete once
307302
// the extra div wrapper is no longer necessary.
@@ -368,17 +363,17 @@ describe('updaters', () => {
368363
'onCommitRoot',
369364
'Child:update',
370365
]);
371-
// Initial render
372-
expect(allSchedulerTypes).toHaveLength(4);
373-
expect(allSchedulerTags[0]).toHaveLength(1);
374-
expect(allSchedulerTags[0]).toContain(HostRoot);
375-
// Offscreen update
376-
expect(allSchedulerTypes[1]).toHaveLength(0);
377-
// Child passive effect
378-
expect(allSchedulerTypes[2]).toHaveLength(1);
379-
expect(allSchedulerTypes[2]).toContain(Child);
380-
// Offscreen update
381-
expect(allSchedulerTypes[3]).toHaveLength(0);
366+
expect(allSchedulerTypes).toEqual([
367+
// Initial render
368+
[null],
369+
// Offscreen update
370+
[],
371+
// Child passive effect
372+
[Child],
373+
// Offscreen update
374+
[],
375+
]);
376+
expect(allSchedulerTags).toEqual([[HostRoot], [], [FunctionComponent], []]);
382377
});
383378

384379
// @gate experimental
@@ -432,25 +427,26 @@ describe('updaters', () => {
432427
triggerError();
433428
});
434429
expect(Scheduler).toHaveYielded(['onCommitRoot', 'error', 'onCommitRoot']);
435-
expect(allSchedulerTypes).toHaveLength(2);
436-
expect(allSchedulerTypes[0]).toHaveLength(1);
437-
expect(allSchedulerTypes[0]).toContain(Parent);
438-
expect(allSchedulerTypes[1]).toHaveLength(1);
439-
expect(allSchedulerTypes[1]).toContain(ErrorBoundary);
430+
expect(allSchedulerTypes).toEqual([[Parent], [ErrorBoundary]]);
440431

441432
// Verify no outstanding flushes
442433
Scheduler.unstable_flushAll();
443434
});
444435

445436
// @gate experimental
446437
it('should distinguish between updaters in the case of interleaved work', async () => {
438+
const {
439+
FunctionComponent,
440+
HostRoot,
441+
} = require('react-reconciler/src/ReactWorkTags');
442+
447443
let triggerLowPriorityUpdate = null;
448444
let triggerSyncPriorityUpdate = null;
449445

450-
const HighPriorityUpdater = () => {
446+
const SyncPriorityUpdater = () => {
451447
const [count, setCount] = React.useState(0);
452448
triggerSyncPriorityUpdate = () => setCount(prevCount => prevCount + 1);
453-
Scheduler.unstable_yieldValue(`HighPriorityUpdater ${count}`);
449+
Scheduler.unstable_yieldValue(`SyncPriorityUpdater ${count}`);
454450
return <Yield value={`HighPriority ${count}`} />;
455451
};
456452
const LowPriorityUpdater = () => {
@@ -465,139 +461,60 @@ describe('updaters', () => {
465461
};
466462

467463
const root = ReactDOM.unstable_createRoot(document.createElement('div'));
468-
ReactTestUtils.act(() => {
469-
root.render(
470-
<React.Fragment>
471-
<HighPriorityUpdater />
472-
<LowPriorityUpdater />
473-
</React.Fragment>,
474-
);
475-
expect(Scheduler).toFlushAndYieldThrough([
476-
'HighPriorityUpdater 0',
477-
'Yield HighPriority 0',
478-
'LowPriorityUpdater 0',
479-
'Yield LowPriority 0',
480-
'onCommitRoot',
481-
]);
482-
});
464+
root.render(
465+
<React.Fragment>
466+
<SyncPriorityUpdater />
467+
<LowPriorityUpdater />
468+
</React.Fragment>,
469+
);
470+
471+
// Render everything initially.
472+
expect(Scheduler).toFlushAndYield([
473+
'SyncPriorityUpdater 0',
474+
'Yield HighPriority 0',
475+
'LowPriorityUpdater 0',
476+
'Yield LowPriority 0',
477+
'onCommitRoot',
478+
]);
483479
expect(triggerLowPriorityUpdate).not.toBeNull();
484480
expect(triggerSyncPriorityUpdate).not.toBeNull();
485-
expect(allSchedulerTypes).toHaveLength(1);
481+
expect(allSchedulerTags).toEqual([[HostRoot]]);
486482

487-
// Render a partially update, but don't finish.
483+
// Render a partial update, but don't finish.
488484
ReactTestUtils.act(() => {
489485
triggerLowPriorityUpdate();
490486
expect(Scheduler).toFlushAndYieldThrough(['LowPriorityUpdater 1']);
491-
expect(allSchedulerTypes).toHaveLength(1);
487+
expect(allSchedulerTags).toEqual([[HostRoot]]);
492488

493489
// Interrupt with higher priority work.
494490
ReactDOM.flushSync(triggerSyncPriorityUpdate);
495491
expect(Scheduler).toHaveYielded([
496-
'HighPriorityUpdater 1',
492+
'SyncPriorityUpdater 1',
497493
'Yield HighPriority 1',
498494
'onCommitRoot',
499495
]);
500-
expect(allSchedulerTypes).toHaveLength(2);
501-
expect(allSchedulerTypes[1]).toHaveLength(1);
502-
expect(allSchedulerTypes[1]).toContain(HighPriorityUpdater);
496+
expect(allSchedulerTypes).toEqual([[null], [SyncPriorityUpdater]]);
503497

504498
// Finish the initial partial update
505499
triggerLowPriorityUpdate();
506-
expect(Scheduler).toFlushAndYieldThrough([
500+
expect(Scheduler).toFlushAndYield([
507501
'LowPriorityUpdater 2',
508502
'Yield LowPriority 2',
509503
'onCommitRoot',
510504
]);
511505
});
512-
expect(allSchedulerTypes).toHaveLength(3);
513-
expect(allSchedulerTypes[2]).toHaveLength(1);
514-
expect(allSchedulerTypes[2]).toContain(LowPriorityUpdater);
515-
516-
// Verify no outstanding flushes
517-
Scheduler.unstable_flushAll();
518-
});
519-
520-
// @gate experimental
521-
it('should not lose track of updaters if work yields before finishing', async () => {
522-
const {HostRoot} = require('react-reconciler/src/ReactWorkTags');
523-
524-
const Yield = ({renderTime}) => {
525-
Scheduler.unstable_advanceTime(renderTime);
526-
Scheduler.unstable_yieldValue('Yield:' + renderTime);
527-
return null;
528-
};
529-
530-
let first;
531-
class FirstComponent extends React.Component {
532-
state = {renderTime: 1};
533-
render() {
534-
first = this;
535-
Scheduler.unstable_advanceTime(this.state.renderTime);
536-
Scheduler.unstable_yieldValue(
537-
'FirstComponent:' + this.state.renderTime,
538-
);
539-
return <Yield renderTime={4} />;
540-
}
541-
}
542-
let second;
543-
class SecondComponent extends React.Component {
544-
state = {renderTime: 2};
545-
render() {
546-
second = this;
547-
Scheduler.unstable_advanceTime(this.state.renderTime);
548-
Scheduler.unstable_yieldValue(
549-
'SecondComponent:' + this.state.renderTime,
550-
);
551-
return <Yield renderTime={7} />;
552-
}
553-
}
554-
555-
Scheduler.unstable_advanceTime(5); // 0 -> 5
556-
557-
const root = ReactDOM.unstable_createRoot(document.createElement('div'));
558-
root.render(
559-
<React.Fragment>
560-
<FirstComponent />
561-
<SecondComponent />
562-
</React.Fragment>,
563-
);
564-
565-
// Render everything initially.
566-
expect(Scheduler).toFlushAndYield([
567-
'FirstComponent:1',
568-
'Yield:4',
569-
'SecondComponent:2',
570-
'Yield:7',
571-
'onCommitRoot',
506+
expect(allSchedulerTags).toEqual([
507+
[HostRoot],
508+
[FunctionComponent],
509+
[FunctionComponent],
572510
]);
573-
expect(allSchedulerTags).toHaveLength(1);
574-
expect(allSchedulerTags[0]).toHaveLength(1);
575-
expect(allSchedulerTags[0]).toContain(HostRoot);
576-
577-
// Render a partial update, but don't finish.
578-
first.setState({renderTime: 10});
579-
expect(Scheduler).toFlushAndYieldThrough(['FirstComponent:10']);
580-
expect(allSchedulerTypes).toHaveLength(1);
581-
582-
// Interrupt with higher priority work.
583-
ReactDOM.flushSync(() => second.setState({renderTime: 30}));
584-
expect(Scheduler).toHaveYielded([
585-
'SecondComponent:30',
586-
'Yield:7',
587-
'onCommitRoot',
511+
expect(allSchedulerTypes).toEqual([
512+
[null],
513+
[SyncPriorityUpdater],
514+
[LowPriorityUpdater],
588515
]);
589-
expect(allSchedulerTypes).toHaveLength(2);
590-
expect(allSchedulerTypes[1]).toHaveLength(1);
591-
expect(allSchedulerTypes[1]).toContain(SecondComponent);
592516

593-
// Resume the original low priority update.
594-
expect(Scheduler).toFlushAndYield([
595-
'FirstComponent:10',
596-
'Yield:4',
597-
'onCommitRoot',
598-
]);
599-
expect(allSchedulerTypes).toHaveLength(3);
600-
expect(allSchedulerTypes[2]).toHaveLength(1);
601-
expect(allSchedulerTypes[2]).toContain(FirstComponent);
517+
// Verify no outstanding flushes
518+
Scheduler.unstable_flushAll();
602519
});
603520
});

0 commit comments

Comments
 (0)