Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFiberAsync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,61 @@ describe('ReactDOMFiberAsync', () => {
});
});

it('calls effect cleanup when unmounting in a transition inside StrictMode', async () => {
let handlerCalls = 0;
let cleanupCalls = 0;
let setShow;

function Child() {
React.useEffect(() => {
const onResize = () => {
handlerCalls++;
};
window.addEventListener('resize', onResize);
return () => {
cleanupCalls++;
window.removeEventListener('resize', onResize);
};
}, []);

return <div>Child</div>;
}

function App() {
const [show, _setShow] = React.useState(true);
setShow = _setShow;
return <>{show ? <Child /> : null}</>;
}

const root = ReactDOMClient.createRoot(container);
await act(async () => {
root.render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
});

await act(async () => {
React.startTransition(() => {
setShow(false);
});
});

// Cleanup should remove the listener before any post-unmount event.
window.dispatchEvent(new Event('resize'));
expect(handlerCalls).toBe(0);
if (__DEV__) {
expect(cleanupCalls).toBe(2);
} else {
expect(cleanupCalls).toBe(1);
}

await act(async () => {
root.unmount();
});
});

it('Should not flush transition lanes if there is no transition scheduled in popState', async () => {
let setHasNavigated;
function App() {
Expand Down
29 changes: 29 additions & 0 deletions packages/react-reconciler/src/__tests__/ActivityStrictMode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,35 @@ describe('Activity StrictMode', () => {
]);
});

// @gate __DEV__
it('calls passive cleanup when unmounting in a transition', async () => {
const root = ReactNoop.createRoot();

await act(() => {
root.render(
<React.StrictMode>
<Activity mode="visible">
<Component label="A" />
</Activity>
</React.StrictMode>,
);
});

log = [];

await act(() => {
React.startTransition(() => {
root.render(
<React.StrictMode>
<Activity mode="visible" />
</React.StrictMode>,
);
});
});

expect(log).toContain('A: useEffect unmount');
});

it('should not cause infinite render loop when StrictMode is used with Suspense and synchronous set states', async () => {
// This is a regression test, see https://github.com/facebook/react/pull/25179 for more details.
function App() {
Expand Down
Loading