Skip to content

Commit 4decd45

Browse files
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
1 parent 0abb4a4 commit 4decd45

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@
5858
},
5959
"peerDependencies": {
6060
"jest": ">=28.0.0",
61-
"react": ">=16.0.0",
61+
"react": ">=16.8.0",
6262
"react-native": ">=0.59",
63-
"react-test-renderer": ">=16.0.0"
63+
"react-test-renderer": ">=16.8.0"
6464
},
6565
"peerDependenciesMeta": {
6666
"jest": {

src/__tests__/act.test.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as React from 'react';
22
import { Text } from 'react-native';
3-
import ReactTestRenderer from 'react-test-renderer';
43
import act from '../act';
54
import render from '../render';
65
import fireEvent from '../fireEvent';
@@ -42,14 +41,12 @@ test('fireEvent should trigger useState', () => {
4241
expect(counter.props.children).toEqual('Total count: 1');
4342
});
4443

45-
test('should act even if there is no act in react-test-renderer', () => {
46-
// @ts-ignore
47-
ReactTestRenderer.act = undefined;
48-
const callback = jest.fn();
49-
50-
act(() => {
51-
callback();
52-
});
44+
test('should be able to not await act', async () => {
45+
const result = act(() => {});
46+
expect(result).toHaveProperty('then');
47+
});
5348

54-
expect(callback).toHaveBeenCalled();
49+
test('should be able to await act', async () => {
50+
const result = await act(async () => {});
51+
expect(result).toBe(undefined);
5552
});

src/act.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import { act as reactTestRendererAct } from 'react-test-renderer';
44
import { checkReactVersionAtLeast } from './react-versions';
55

6-
const actMock = (callback: () => void) => {
7-
callback();
8-
};
6+
type ReactAct = typeof reactTestRendererAct;
97

108
// See https://github.com/reactwg/react-18/discussions/102 for more context on global.IS_REACT_ACT_ENVIRONMENT
119
declare global {
@@ -20,10 +18,8 @@ function getIsReactActEnvironment() {
2018
return globalThis.IS_REACT_ACT_ENVIRONMENT;
2119
}
2220

23-
type Act = typeof reactTestRendererAct;
24-
25-
function withGlobalActEnvironment(actImplementation: Act) {
26-
return (callback: Parameters<Act>[0]) => {
21+
function withGlobalActEnvironment(actImplementation: ReactAct) {
22+
return (callback: Parameters<ReactAct>[0]) => {
2723
const previousActEnvironment = getIsReactActEnvironment();
2824
setIsReactActEnvironment(true);
2925

@@ -44,6 +40,7 @@ function withGlobalActEnvironment(actImplementation: Act) {
4440
}
4541
return result;
4642
});
43+
4744
if (callbackNeedsToBeAwaited) {
4845
const thenable = actResult;
4946
return {
@@ -77,16 +74,10 @@ function withGlobalActEnvironment(actImplementation: Act) {
7774
}
7875
};
7976
}
80-
const getAct = () => {
81-
if (!reactTestRendererAct) {
82-
return actMock;
83-
}
8477

85-
return checkReactVersionAtLeast(18, 0)
86-
? withGlobalActEnvironment(reactTestRendererAct)
87-
: reactTestRendererAct;
88-
};
89-
const act = getAct();
78+
const act: ReactAct = checkReactVersionAtLeast(18, 0)
79+
? (withGlobalActEnvironment(reactTestRendererAct) as ReactAct)
80+
: reactTestRendererAct;
9081

9182
export default act;
9283
export {

0 commit comments

Comments
 (0)