From 8d71c73a1dd821e537ee647798cbe46ef30ec685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Thu, 26 Jan 2023 07:55:48 +0000 Subject: [PATCH 1/7] fix: force cast await to proper type --- src/__tests__/act.test.tsx | 12 +++++++++++- src/act.ts | 22 ++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/__tests__/act.test.tsx b/src/__tests__/act.test.tsx index 138985896..1b4e043ef 100644 --- a/src/__tests__/act.test.tsx +++ b/src/__tests__/act.test.tsx @@ -43,7 +43,7 @@ test('fireEvent should trigger useState', () => { }); test('should act even if there is no act in react-test-renderer', () => { - // @ts-ignore + // @ts-expect-error ReactTestRenderer.act = undefined; const callback = jest.fn(); @@ -53,3 +53,13 @@ test('should act even if there is no act in react-test-renderer', () => { expect(callback).toHaveBeenCalled(); }); + +test('should be able to not await act', async () => { + const result = act(() => {}); + expect(result).toBe(undefined); +}); + +test('should be able to await act', async () => { + const result = await act(async () => {}); + expect(result).toBe(undefined); +}); diff --git a/src/act.ts b/src/act.ts index 10d55d073..27718cf7e 100644 --- a/src/act.ts +++ b/src/act.ts @@ -3,9 +3,7 @@ import { act as reactTestRendererAct } from 'react-test-renderer'; import { checkReactVersionAtLeast } from './react-versions'; -const actMock = (callback: () => void) => { - callback(); -}; +type ReactAct = typeof reactTestRendererAct; // See https://github.com/reactwg/react-18/discussions/102 for more context on global.IS_REACT_ACT_ENVIRONMENT declare global { @@ -20,10 +18,8 @@ function getIsReactActEnvironment() { return globalThis.IS_REACT_ACT_ENVIRONMENT; } -type Act = typeof reactTestRendererAct; - -function withGlobalActEnvironment(actImplementation: Act) { - return (callback: Parameters[0]) => { +function withGlobalActEnvironment(actImplementation: ReactAct) { + return (callback: Parameters[0]) => { const previousActEnvironment = getIsReactActEnvironment(); setIsReactActEnvironment(true); @@ -44,6 +40,7 @@ function withGlobalActEnvironment(actImplementation: Act) { } return result; }); + if (callbackNeedsToBeAwaited) { const thenable = actResult; return { @@ -77,6 +74,14 @@ function withGlobalActEnvironment(actImplementation: Act) { } }; } + +type MockAct = (callback: () => void) => void; +type Act = typeof reactTestRendererAct extends undefined ? MockAct : ReactAct; + +const actMock = (callback: () => void) => { + callback(); +}; + const getAct = () => { if (!reactTestRendererAct) { return actMock; @@ -86,7 +91,8 @@ const getAct = () => { ? withGlobalActEnvironment(reactTestRendererAct) : reactTestRendererAct; }; -const act = getAct(); + +const act = getAct() as Act; export default act; export { From b4a6d21892b0244b8f2662e2323e220babfd3c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Thu, 26 Jan 2023 08:17:54 +0000 Subject: [PATCH 2/7] fix: ci --- src/__tests__/act.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/act.test.tsx b/src/__tests__/act.test.tsx index 1b4e043ef..044091d19 100644 --- a/src/__tests__/act.test.tsx +++ b/src/__tests__/act.test.tsx @@ -56,7 +56,7 @@ test('should act even if there is no act in react-test-renderer', () => { test('should be able to not await act', async () => { const result = act(() => {}); - expect(result).toBe(undefined); + expect(result).toHaveProperty('then'); }); test('should be able to await act', async () => { From f2e267c8c73e99cfbdaf1acdd15b2e9da3552faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Thu, 26 Jan 2023 08:54:28 +0000 Subject: [PATCH 3/7] refactor: remove support for mock act --- package.json | 2 +- src/act.ts | 13 +------------ 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 71b5791f3..3fffb3151 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "jest": ">=28.0.0", "react": ">=16.0.0", "react-native": ">=0.59", - "react-test-renderer": ">=16.0.0" + "react-test-renderer": ">=16.8.0" }, "peerDependenciesMeta": { "jest": { diff --git a/src/act.ts b/src/act.ts index 27718cf7e..8d22ce3fd 100644 --- a/src/act.ts +++ b/src/act.ts @@ -75,24 +75,13 @@ function withGlobalActEnvironment(actImplementation: ReactAct) { }; } -type MockAct = (callback: () => void) => void; -type Act = typeof reactTestRendererAct extends undefined ? MockAct : ReactAct; - -const actMock = (callback: () => void) => { - callback(); -}; - const getAct = () => { - if (!reactTestRendererAct) { - return actMock; - } - return checkReactVersionAtLeast(18, 0) ? withGlobalActEnvironment(reactTestRendererAct) : reactTestRendererAct; }; -const act = getAct() as Act; +const act = getAct() as ReactAct; export default act; export { From 76df44c636ed05462eadb11dddd5a44ab6d07283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Thu, 26 Jan 2023 11:25:03 +0000 Subject: [PATCH 4/7] refactor: tweaks --- src/act.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/act.ts b/src/act.ts index 8d22ce3fd..42c7c039c 100644 --- a/src/act.ts +++ b/src/act.ts @@ -75,13 +75,9 @@ function withGlobalActEnvironment(actImplementation: ReactAct) { }; } -const getAct = () => { - return checkReactVersionAtLeast(18, 0) - ? withGlobalActEnvironment(reactTestRendererAct) - : reactTestRendererAct; -}; - -const act = getAct() as ReactAct; +const act: ReactAct = checkReactVersionAtLeast(18, 0) + ? (withGlobalActEnvironment(reactTestRendererAct) as ReactAct) + : reactTestRendererAct; export default act; export { From a6498df2338935ff3a44a81a47728511dfc78f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Thu, 26 Jan 2023 14:08:52 +0000 Subject: [PATCH 5/7] chore: remove unused test --- src/__tests__/act.test.tsx | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/__tests__/act.test.tsx b/src/__tests__/act.test.tsx index 044091d19..66f3c40ea 100644 --- a/src/__tests__/act.test.tsx +++ b/src/__tests__/act.test.tsx @@ -42,18 +42,6 @@ test('fireEvent should trigger useState', () => { expect(counter.props.children).toEqual('Total count: 1'); }); -test('should act even if there is no act in react-test-renderer', () => { - // @ts-expect-error - ReactTestRenderer.act = undefined; - const callback = jest.fn(); - - act(() => { - callback(); - }); - - expect(callback).toHaveBeenCalled(); -}); - test('should be able to not await act', async () => { const result = act(() => {}); expect(result).toHaveProperty('then'); From 90624afc975b96e977678c505839a5e9a5f50607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Thu, 26 Jan 2023 14:10:42 +0000 Subject: [PATCH 6/7] chore: fix lint --- src/__tests__/act.test.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/__tests__/act.test.tsx b/src/__tests__/act.test.tsx index 66f3c40ea..ca8e0e5b8 100644 --- a/src/__tests__/act.test.tsx +++ b/src/__tests__/act.test.tsx @@ -1,6 +1,5 @@ import * as React from 'react'; import { Text } from 'react-native'; -import ReactTestRenderer from 'react-test-renderer'; import act from '../act'; import render from '../render'; import fireEvent from '../fireEvent'; From 2e375eb63dfc8a083f1db1ce26a3720cda4d2dbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Thu, 26 Jan 2023 20:58:32 +0000 Subject: [PATCH 7/7] refactor: code review changes --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3fffb3151..0cf5bf84c 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ }, "peerDependencies": { "jest": ">=28.0.0", - "react": ">=16.0.0", + "react": ">=16.8.0", "react-native": ">=0.59", "react-test-renderer": ">=16.8.0" },