From 94387b69e59a0299e7409d96a230c4f24425c94c Mon Sep 17 00:00:00 2001 From: Biki das Date: Wed, 22 Jan 2025 22:19:49 +0530 Subject: [PATCH] Added test for state reset for different component types --- .../src/__tests__/ReactFresh-test.js | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/packages/react-refresh/src/__tests__/ReactFresh-test.js b/packages/react-refresh/src/__tests__/ReactFresh-test.js index 7f9d79900f409..e14cdcfe32cb5 100644 --- a/packages/react-refresh/src/__tests__/ReactFresh-test.js +++ b/packages/react-refresh/src/__tests__/ReactFresh-test.js @@ -856,6 +856,71 @@ describe('ReactFresh', () => { } }); + it('resets state when switching between different component types', async () => { + if (__DEV__) { + await act(async () => { + await render(() => { + function Test() { + const [count, setCount] = React.useState(0); + return ( +
setCount(c => c + 1)}>count: {count}
+ ); + } + $RefreshReg$(Test, 'Test'); + return Test; + }); + }); + + expect(container.firstChild.textContent).toBe('count: 0'); + await act(async () => { + container.firstChild.click(); + }); + expect(container.firstChild.textContent).toBe('count: 1'); + + await act(async () => { + await patch(() => { + function Test2() { + const [count, setCount] = React.useState(0); + return ( +
setCount(c => c + 1)}>count: {count}
+ ); + } + const Test = React.memo(Test2); + $RefreshReg$(Test2, 'Test2'); + $RefreshReg$(Test, 'Test'); + return Test; + }); + }); + + expect(container.firstChild.textContent).toBe('count: 0'); + await act(async () => { + container.firstChild.click(); + }); + expect(container.firstChild.textContent).toBe('count: 1'); + + await act(async () => { + await patch(() => { + const Test = React.forwardRef((props, ref) => { + const [count, setCount] = React.useState(0); + return ( +
setCount(c => c + 1)}> + count: {count} +
+ ); + }); + $RefreshReg$(Test, 'Test'); + return Test; + }); + }); + + expect(container.firstChild.textContent).toBe('count: 0'); + await act(async () => { + container.firstChild.click(); + }); + expect(container.firstChild.textContent).toBe('count: 1'); + } + }); + it('can update simple memo function in isolation', async () => { if (__DEV__) { await render(() => {