Skip to content

Commit

Permalink
Improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
AugustinLF committed Sep 22, 2022
1 parent 32b9288 commit 729626a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
44 changes: 23 additions & 21 deletions src/__tests__/waitFor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,30 @@ test('waits for element with custom interval', async () => {
expect(mockFn).toHaveBeenCalledTimes(2);
});

// this component is convoluted on purpose. It is not a good react pattern, but it is valid
// react code that will run differently between different react versions (17 and 18), so we need
// explicit tests for it
const Comp = ({ onPress }: { onPress: () => void }) => {
const [state, setState] = React.useState(false);

React.useEffect(() => {
if (state) {
onPress();
}
}, [state, onPress]);

return (
<Pressable
onPress={async () => {
await Promise.resolve();
setState(true);
}}
>
<Text>Trigger</Text>
</Pressable>
);
};
test('waits for async event with fireEvent', async () => {
const Comp = ({ onPress }: { onPress: () => void }) => {
const [state, setState] = React.useState(false);

React.useEffect(() => {
if (state) {
onPress();
}
}, [state, onPress]);

return (
<Pressable
onPress={async () => {
await Promise.resolve();
setState(true);
}}
>
<Text>Trigger</Text>
</Pressable>
);
};

const spy = jest.fn();
const { getByText } = render(<Comp onPress={spy} />);

Expand Down
50 changes: 24 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@ import { cleanup } from './pure';
import { flushMicroTasks } from './flushMicroTasks';
import { getIsReactActEnvironment, setReactActEnvironment } from './act';

// If we're running in a test runner that supports afterEach
// then we'll automatically run cleanup afterEach test
// this ensures that tests run in isolation from each other
// if you don't like this then either import the `pure` module
// or set the RNTL_SKIP_AUTO_CLEANUP env variable to 'true'.
if (typeof afterEach === 'function' && !process.env.RNTL_SKIP_AUTO_CLEANUP) {
// eslint-disable-next-line no-undef
afterEach(async () => {
await flushMicroTasks();
cleanup();
});
}
if (typeof process === 'undefined' || !process.env?.RNTL_SKIP_AUTO_CLEANUP) {
// If we're running in a test runner that supports afterEach
// then we'll automatically run cleanup afterEach test
// this ensures that tests run in isolation from each other
// if you don't like this then either import the `pure` module
// or set the RNTL_SKIP_AUTO_CLEANUP env variable to 'true'.
if (typeof afterEach === 'function') {
// eslint-disable-next-line no-undef
afterEach(async () => {
await flushMicroTasks();
cleanup();
});
}

if (
typeof beforeAll === 'function' &&
typeof afterAll === 'function' &&
!process.env.RNTL_SKIP_AUTO_CLEANUP
) {
// This matches the behavior of React < 18.
let previousIsReactActEnvironment = getIsReactActEnvironment();
beforeAll(() => {
previousIsReactActEnvironment = getIsReactActEnvironment();
setReactActEnvironment(true);
});
if (typeof beforeAll === 'function' && typeof afterAll === 'function') {
// This matches the behavior of React < 18.
let previousIsReactActEnvironment = getIsReactActEnvironment();
beforeAll(() => {
previousIsReactActEnvironment = getIsReactActEnvironment();
setReactActEnvironment(true);
});

afterAll(() => {
setReactActEnvironment(previousIsReactActEnvironment);
});
afterAll(() => {
setReactActEnvironment(previousIsReactActEnvironment);
});
}
}

export * from './pure';

0 comments on commit 729626a

Please sign in to comment.