Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: force cast await to proper type #1293

Merged
merged 7 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved
},
"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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it even useful to pass it actImplementation as a param?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmmm, good question, we've got our code from RTL and just sprinkled types and eslint ignores over it.
I guess it's easier to keep it in sync with their implmentation.

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