Skip to content

Commit

Permalink
fix: force cast await to proper type (#1293)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mdjastrzebski committed Jan 26, 2023
1 parent 0abb4a4 commit 4decd45
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
17 changes: 7 additions & 10 deletions src/__tests__/act.test.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
});
23 changes: 7 additions & 16 deletions src/act.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand All @@ -44,6 +40,7 @@ function withGlobalActEnvironment(actImplementation: Act) {
}
return result;
});

if (callbackNeedsToBeAwaited) {
const thenable = actResult;
return {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4decd45

Please sign in to comment.