Skip to content

Commit f5622fa

Browse files
committed
Fix the test
1 parent 714ca3b commit f5622fa

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

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

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ let ReactDOMClient;
1616
let Scheduler;
1717
let act;
1818
let waitForAll;
19-
let waitFor;
2019
let assertLog;
2120

2221
const setUntrackedInputValue = Object.getOwnPropertyDescriptor(
@@ -28,7 +27,6 @@ describe('ReactDOMFiberAsync', () => {
2827
let container;
2928

3029
beforeEach(() => {
31-
jest.resetModules();
3230
container = document.createElement('div');
3331
React = require('react');
3432
ReactDOM = require('react-dom');
@@ -38,7 +36,6 @@ describe('ReactDOMFiberAsync', () => {
3836

3937
const InternalTestUtils = require('internal-test-utils');
4038
waitForAll = InternalTestUtils.waitForAll;
41-
waitFor = InternalTestUtils.waitFor;
4239
assertLog = InternalTestUtils.assertLog;
4340

4441
document.body.appendChild(container);
@@ -579,14 +576,12 @@ describe('ReactDOMFiberAsync', () => {
579576
setHasNavigated(true);
580577
});
581578
setSyncState(true);
582-
583-
// Jest is not emulating window.event correctly in the microtask
584-
// TODO: this causes `window.event` to always be popState in this test file
585-
window.event = window.event;
586579
}
587580
React.useEffect(() => {
588581
window.addEventListener('popstate', onPopstate);
589-
return () => window.removeEventListener('popstate', onPopstate);
582+
return () => {
583+
window.removeEventListener('popstate', onPopstate);
584+
};
590585
}, []);
591586
Scheduler.log(`render:${hasNavigated}/${syncState}`);
592587
return null;
@@ -599,34 +594,38 @@ describe('ReactDOMFiberAsync', () => {
599594

600595
await act(async () => {
601596
const popStateEvent = new Event('popstate');
597+
// Jest is not emulating window.event correctly in the microtask
598+
window.event = popStateEvent;
602599
window.dispatchEvent(popStateEvent);
600+
queueMicrotask(() => {
601+
window.event = undefined;
602+
});
603603
});
604604

605605
assertLog(['popState', 'render:true/true']);
606-
607-
root.unmount();
606+
await act(() => {
607+
root.unmount();
608+
});
608609
});
609610

610-
it('should not flush transition lanes if there is no transition scheduled in popState', async () => {
611-
let _setHasNavigated;
612-
function App({shouldTransition}) {
611+
it('Should not flush transition lanes if there is no transition scheduled in popState', async () => {
612+
let setHasNavigated;
613+
function App() {
613614
const [syncState, setSyncState] = React.useState(false);
614-
const [hasNavigated, setHasNavigated] = React.useState(false);
615-
_setHasNavigated = setHasNavigated;
615+
const [hasNavigated, _setHasNavigated] = React.useState(false);
616+
setHasNavigated = _setHasNavigated;
616617
function onPopstate() {
617618
setSyncState(true);
618-
619-
// Jest is not emulating window.event correctly in the microtask
620-
window.event = window.event;
621619
}
622620

623621
React.useEffect(() => {
624622
window.addEventListener('popstate', onPopstate);
625-
return () => window.removeEventListener('popstate', onPopstate);
623+
return () => {
624+
window.removeEventListener('popstate', onPopstate);
625+
};
626626
}, []);
627627

628628
Scheduler.log(`render:${hasNavigated}/${syncState}`);
629-
630629
return null;
631630
}
632631
const root = ReactDOMClient.createRoot(container);
@@ -635,16 +634,21 @@ describe('ReactDOMFiberAsync', () => {
635634
});
636635
assertLog(['render:false/false']);
637636

638-
// Trigger a transition update to be scheduled
639637
React.startTransition(() => {
640-
_setHasNavigated(true);
641-
})
642-
643-
// The popState should not flush the transition update
644-
const popStateEvent = new Event('popstate');
645-
window.dispatchEvent(popStateEvent);
646-
await waitFor(['render:false/true']);
647-
648-
root.unmount();
638+
setHasNavigated(true);
639+
});
640+
await act(async () => {
641+
const popStateEvent = new Event('popstate');
642+
// Jest is not emulating window.event correctly in the microtask
643+
window.event = popStateEvent;
644+
window.dispatchEvent(popStateEvent);
645+
queueMicrotask(() => {
646+
window.event = undefined;
647+
});
648+
});
649+
assertLog(['render:false/true', 'render:true/true']);
650+
await act(() => {
651+
root.unmount();
652+
});
649653
});
650654
});

packages/react-reconciler/src/ReactFiberRootScheduler.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ function processRootScheduleInMicrotask() {
237237
let root = firstScheduledRoot;
238238
while (root !== null) {
239239
const next = root.next;
240-
241-
if (currentEventTransitionLane !== NoLane && shouldAttemptEagerTransition()) {
242-
markRootEntangled(
243-
root,
244-
mergeLanes(currentEventTransitionLane, SyncLane),
245-
);
240+
241+
if (
242+
currentEventTransitionLane !== NoLane &&
243+
shouldAttemptEagerTransition()
244+
) {
245+
markRootEntangled(root, mergeLanes(currentEventTransitionLane, SyncLane));
246246
}
247247

248248
const nextLanes = scheduleTaskForRootDuringMicrotask(root, currentTime);
@@ -275,7 +275,7 @@ function processRootScheduleInMicrotask() {
275275
}
276276

277277
currentEventTransitionLane = NoLane;
278-
278+
279279
// At the end of the microtask, flush any pending synchronous work. This has
280280
// to come at the end, because it does actual rendering work that might throw.
281281
flushSyncWorkOnAllRoots();

0 commit comments

Comments
 (0)