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
This commit is contained in:
Maciej Jastrzebski
2023-01-26 21:28:10 +00:00
committed by GitHub
parent 0abb4a40f9
commit 4decd45f5e
3 changed files with 17 additions and 29 deletions
+2 -2
View File
@@ -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": {
+8 -11
View File
@@ -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();
});
expect(callback).toHaveBeenCalled();
test('should be able to not await act', async () => {
const result = act(() => {});
expect(result).toHaveProperty('then');
});
test('should be able to await act', async () => {
const result = await act(async () => {});
expect(result).toBe(undefined);
});
+7 -16
View File
@@ -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<Act>[0]) => {
function withGlobalActEnvironment(actImplementation: ReactAct) {
return (callback: Parameters<ReactAct>[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 {