From 4decd45f5ead077577e56dd75cf2e6b09b00a2da Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Thu, 26 Jan 2023 21:28:10 +0000 Subject: [PATCH] fix: force cast await to proper type (#1293) * fix: force cast await to proper type * fix: ci * refactor: remove support for mock act * refactor: tweaks * chore: remove unused test * chore: fix lint * refactor: code review changes --- package.json | 4 ++-- src/__tests__/act.test.tsx | 17 +++++++---------- src/act.ts | 23 +++++++---------------- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 5edcf12af..bcc2a145d 100644 --- a/package.json +++ b/package.json @@ -58,9 +58,9 @@ }, "peerDependencies": { "jest": ">=28.0.0", - "react": ">=16.0.0", + "react": ">=16.8.0", "react-native": ">=0.59", - "react-test-renderer": ">=16.0.0" + "react-test-renderer": ">=16.8.0" }, "peerDependenciesMeta": { "jest": { diff --git a/src/__tests__/act.test.tsx b/src/__tests__/act.test.tsx index 138985896..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'; @@ -42,14 +41,12 @@ 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-ignore - ReactTestRenderer.act = undefined; - const callback = jest.fn(); - - act(() => { - callback(); - }); +test('should be able to not await act', async () => { + const result = act(() => {}); + expect(result).toHaveProperty('then'); +}); - expect(callback).toHaveBeenCalled(); +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..42c7c039c 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,16 +74,10 @@ function withGlobalActEnvironment(actImplementation: Act) { } }; } -const getAct = () => { - if (!reactTestRendererAct) { - return actMock; - } - return checkReactVersionAtLeast(18, 0) - ? withGlobalActEnvironment(reactTestRendererAct) - : reactTestRendererAct; -}; -const act = getAct(); +const act: ReactAct = checkReactVersionAtLeast(18, 0) + ? (withGlobalActEnvironment(reactTestRendererAct) as ReactAct) + : reactTestRendererAct; export default act; export {