44
ReactFeatureFlags = require('shared/ReactFeatureFlags');
45
46
ReactFeatureFlags.enableSchedulerTracing = true;
47
- ReactFeatureFlags.flushSuspenseFallbacksInTests = false;
47
ReactFeatureFlags.enableProfilerTimer = true;
48
deferPassiveEffectCleanupDuringUnmount =
49
ReactFeatureFlags.deferPassiveEffectCleanupDuringUnmount;
658
expect(ReactNoop.getChildren()).toEqual([span(22)]);
659
});
660
662
- it('discards render phase updates if something suspends', () => {
661
+ it('discards render phase updates if something suspends', async () => {
662
const thenable = {then() {}};
663
function Foo({signal}) {
664
return (
746
await ReactNoop.act(async () => {
747
root.render(<Foo signal={false} />);
748
setLabel('B');
750
- });
751
- expect(Scheduler).toHaveYielded(['Suspend!']);
752
- expect(root).toMatchRenderedOutput(<span prop="A:0" />);
749
754
- // Rendering again should suspend again.
755
- root.render(<Foo signal={false} />);
756
- expect(Scheduler).toFlushAndYield(['Suspend!']);
750
+ expect(Scheduler).toFlushAndYield(['Suspend!']);
751
+ expect(root).toMatchRenderedOutput(<span prop="A:0" />);
752
758
- // Flip the signal back to "cancel" the update. However, the update to
759
- // label should still proceed. It shouldn't have been dropped.
760
- root.render(<Foo signal={true} />);
761
- expect(Scheduler).toFlushAndYield(['B:0']);
762
- expect(root).toMatchRenderedOutput(<span prop="B:0" />);
753
+ // Rendering again should suspend again.
754
+ root.render(<Foo signal={false} />);
755
+ expect(Scheduler).toFlushAndYield(['Suspend!']);
756
+
757
+ // Flip the signal back to "cancel" the update. However, the update to
758
+ // label should still proceed. It shouldn't have been dropped.
759
+ root.render(<Foo signal={true} />);
760
+ expect(Scheduler).toFlushAndYield(['B:0']);
761
+ expect(root).toMatchRenderedOutput(<span prop="B:0" />);
762
+ });
763
});
764
765
it('regression: render phase updates cause lower pri work to be dropped', async () => {
2755
span('Before... Pending: false'),
2756
]);
2757
2758
- act(() => {
2758
+ await act(async () => {
2759
Scheduler.unstable_runWithPriority(
2760
Scheduler.unstable_UserBlockingPriority,
2761
transition,
2762
);
2763
- });
2764
- Scheduler.unstable_advanceTime(500);
2765
- await advanceTimers(500);
2766
- expect(Scheduler).toHaveYielded([
2767
- 'Before... Pending: true',
2768
- 'Suspend! [After... Pending: false]',
2769
- 'Loading... Pending: false',
2770
- ]);
2771
- expect(ReactNoop.getChildren()).toEqual([
2772
- span('Before... Pending: true'),
2773
- ]);
2763
2775
- Scheduler.unstable_advanceTime(1000);
2776
- await advanceTimers(1000);
2777
- expect(ReactNoop.getChildren()).toEqual([
2778
- hiddenSpan('Before... Pending: true'),
2779
- span('Loading... Pending: false'),
2780
- ]);
2764
+ expect(Scheduler).toFlushAndYield([
2765
+ 'Before... Pending: true',
2766
+ 'Suspend! [After... Pending: false]',
2767
+ 'Loading... Pending: false',
2768
+ ]);
2769
+ expect(ReactNoop.getChildren()).toEqual([
2770
+ span('Before... Pending: true'),
2771
+ ]);
2772
+ Scheduler.unstable_advanceTime(500);
2773
+ await advanceTimers(500);
2774
2782
- Scheduler.unstable_advanceTime(500);
2783
- await advanceTimers(500);
2784
- expect(Scheduler).toHaveYielded([
2785
- 'Promise resolved [After... Pending: false]',
2786
- ]);
2787
- expect(Scheduler).toFlushAndYield(['After... Pending: false']);
2788
- expect(ReactNoop.getChildren()).toEqual([
2789
- span('After... Pending: false'),
2790
- ]);
2775
+ Scheduler.unstable_advanceTime(1000);
2776
+ await advanceTimers(1000);
2777
+ expect(ReactNoop.getChildren()).toEqual([
2778
+ hiddenSpan('Before... Pending: true'),
2779
+ span('Loading... Pending: false'),
2780
+ ]);
2781
+
2782
+ Scheduler.unstable_advanceTime(500);
2783
+ await advanceTimers(500);
2784
+ expect(Scheduler).toHaveYielded([
2785
+ 'Promise resolved [After... Pending: false]',
2786
+ ]);
2787
+ expect(Scheduler).toFlushAndYield(['After... Pending: false']);
2788
+ expect(ReactNoop.getChildren()).toEqual([
2789
+ span('After... Pending: false'),
2790
+ ]);
2791
+ });
2792
});
2793
// @gate experimental
2794
it('delays showing loading state until after busyDelayMs + busyMinDurationMs', async () => {
2821
span('Before... Pending: false'),
2822
]);
2823
2823
- act(() => {
2824
+ await act(async () => {
2825
Scheduler.unstable_runWithPriority(
2826
Scheduler.unstable_UserBlockingPriority,
2827
transition,
2828
);
2828
- });
2829
- Scheduler.unstable_advanceTime(1000);
2830
- await advanceTimers(1000);
2831
- expect(Scheduler).toHaveYielded([
2832
- 'Before... Pending: true',
2833
- 'Suspend! [After... Pending: false]',
2834
- 'Loading... Pending: false',
2835
- ]);
2836
- expect(ReactNoop.getChildren()).toEqual([
2837
- span('Before... Pending: true'),
2838
- ]);
2829
2840
- // Resolve the promise. The whole tree has now completed. However,
2841
- // because we exceeded the busy threshold, we won't commit the
2842
- // result yet.
2843
- Scheduler.unstable_advanceTime(1000);
2844
- await advanceTimers(1000);
2845
- expect(Scheduler).toHaveYielded([
2846
- 'Promise resolved [After... Pending: false]',
2847
- ]);
2848
- expect(Scheduler).toFlushAndYield(['After... Pending: false']);
2849
- expect(ReactNoop.getChildren()).toEqual([
2850
- span('Before... Pending: true'),
2851
- ]);
2830
+ expect(Scheduler).toFlushAndYield([
2831
+ 'Before... Pending: true',
2832
+ 'Suspend! [After... Pending: false]',
2833
+ 'Loading... Pending: false',
2834
+ ]);
2835
+ expect(ReactNoop.getChildren()).toEqual([
2836
+ span('Before... Pending: true'),
2837
+ ]);
2838
2853
- // Advance time until just before the `busyMinDuration` threshold.
2854
- Scheduler.unstable_advanceTime(999);
2855
- await advanceTimers(999);
2856
- expect(ReactNoop.getChildren()).toEqual([
2857
- span('Before... Pending: true'),
2858
- ]);
2839
+ Scheduler.unstable_advanceTime(1000);
2840
+ await advanceTimers(1000);
2841
2860
- // Advance time just a bit more. Now we complete the transition.
2861
- Scheduler.unstable_advanceTime(300);
2862
- await advanceTimers(300);
2863
- expect(ReactNoop.getChildren()).toEqual([
2864
- span('After... Pending: false'),
2865
- ]);
2842
+ // Resolve the promise. The whole tree has now completed. However,
2843
+ // because we exceeded the busy threshold, we won't commit the
2844
+ // result yet.
2845
+ Scheduler.unstable_advanceTime(1000);
2846
+ await advanceTimers(1000);
2847
+ expect(Scheduler).toHaveYielded([
2848
+ 'Promise resolved [After... Pending: false]',
2849
+ ]);
2850
+ expect(Scheduler).toFlushAndYield(['After... Pending: false']);
2851
+ expect(ReactNoop.getChildren()).toEqual([
2852
+ span('Before... Pending: true'),
2853
+ ]);
2854
+
2855
+ // Advance time until just before the `busyMinDuration` threshold.
2856
+ Scheduler.unstable_advanceTime(999);
2857
+ await advanceTimers(999);
2858
+ expect(ReactNoop.getChildren()).toEqual([
2859
+ span('Before... Pending: true'),
2860
+ ]);
2861
+
2862
+ // Advance time just a bit more. Now we complete the transition.
2863
+ Scheduler.unstable_advanceTime(300);
2864
+ await advanceTimers(300);
2865
+ expect(ReactNoop.getChildren()).toEqual([
2866
+ span('After... Pending: false'),
2867
+ ]);
2868
+ });
2869
});
2870
});
2871
+
2872
describe('useDeferredValue', () => {
2873
// @gate experimental
2874
it('defers text value until specified timeout', async () => {
2906
expect(Scheduler).toFlushAndYield(['A']);
2907
expect(ReactNoop.getChildren()).toEqual([span('A'), span('A')]);
2908
2905
- act(() => {
2909
+ await act(async () => {
2910
_setText('B');
2911
+ expect(Scheduler).toFlushAndYield([
2912
+ 'B',
2913
+ 'A',
2914
+ 'B',
2915
+ 'Suspend! [B]',
2916
+ 'Loading',
2917
+ ]);
2918
+ expect(Scheduler).toFlushAndYield([]);
2919
+ expect(ReactNoop.getChildren()).toEqual([span('B'), span('A')]);
2920
});
2908
- expect(Scheduler).toHaveYielded([
2909
- 'B',
2910
- 'A',
2911
- 'B',
2912
- 'Suspend! [B]',
2913
- 'Loading',
2914
- ]);
2915
- expect(Scheduler).toFlushAndYield([]);
2916
- expect(ReactNoop.getChildren()).toEqual([span('B'), span('A')]);
2921
2918
- Scheduler.unstable_advanceTime(250);
2919
- await advanceTimers(250);
2920
- expect(Scheduler).toFlushAndYield([]);
2922
+ await act(async () => {
2923
+ Scheduler.unstable_advanceTime(250);
2924
+ await advanceTimers(250);
2925
+ });
2926
+ expect(Scheduler).toHaveYielded([]);
2927
expect(ReactNoop.getChildren()).toEqual([span('B'), span('A')]);
2928
2923
- Scheduler.unstable_advanceTime(500);
2924
- await advanceTimers(500);
2929
+ await act(async () => {
2930
+ Scheduler.unstable_advanceTime(500);
2931
+ await advanceTimers(500);
2932
+ });
2933
+ expect(Scheduler).toHaveYielded([]);
2934
expect(ReactNoop.getChildren()).toEqual([
2935
span('B'),
2936
hiddenSpan('A'),
2937
span('Loading'),
2938
]);
2939
2931
- Scheduler.unstable_advanceTime(250);
2932
- await advanceTimers(250);
2933
- expect(Scheduler).toHaveYielded(['Promise resolved [B]']);
2934
-
2935
- act(() => {
2936
- expect(Scheduler).toFlushAndYield(['B']);
2940
+ await act(async () => {
2941
+ Scheduler.unstable_advanceTime(250);
2942
+ await advanceTimers(250);
2943
});
2944
+ expect(Scheduler).toHaveYielded(['Promise resolved [B]', 'B']);
2945
expect(ReactNoop.getChildren()).toEqual([span('B'), span('B')]);
2946
});
2947
});