Skip to content

Commit 729626a

Browse files
committed
Improve readability
1 parent 32b9288 commit 729626a

File tree

2 files changed

+47
-47
lines changed

2 files changed

+47
-47
lines changed

src/__tests__/waitFor.test.tsx

+23-21
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,30 @@ test('waits for element with custom interval', async () => {
7878
expect(mockFn).toHaveBeenCalledTimes(2);
7979
});
8080

81+
// this component is convoluted on purpose. It is not a good react pattern, but it is valid
82+
// react code that will run differently between different react versions (17 and 18), so we need
83+
// explicit tests for it
84+
const Comp = ({ onPress }: { onPress: () => void }) => {
85+
const [state, setState] = React.useState(false);
86+
87+
React.useEffect(() => {
88+
if (state) {
89+
onPress();
90+
}
91+
}, [state, onPress]);
92+
93+
return (
94+
<Pressable
95+
onPress={async () => {
96+
await Promise.resolve();
97+
setState(true);
98+
}}
99+
>
100+
<Text>Trigger</Text>
101+
</Pressable>
102+
);
103+
};
81104
test('waits for async event with fireEvent', async () => {
82-
const Comp = ({ onPress }: { onPress: () => void }) => {
83-
const [state, setState] = React.useState(false);
84-
85-
React.useEffect(() => {
86-
if (state) {
87-
onPress();
88-
}
89-
}, [state, onPress]);
90-
91-
return (
92-
<Pressable
93-
onPress={async () => {
94-
await Promise.resolve();
95-
setState(true);
96-
}}
97-
>
98-
<Text>Trigger</Text>
99-
</Pressable>
100-
);
101-
};
102-
103105
const spy = jest.fn();
104106
const { getByText } = render(<Comp onPress={spy} />);
105107

src/index.ts

+24-26
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,32 @@ import { cleanup } from './pure';
22
import { flushMicroTasks } from './flushMicroTasks';
33
import { getIsReactActEnvironment, setReactActEnvironment } from './act';
44

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

18-
if (
19-
typeof beforeAll === 'function' &&
20-
typeof afterAll === 'function' &&
21-
!process.env.RNTL_SKIP_AUTO_CLEANUP
22-
) {
23-
// This matches the behavior of React < 18.
24-
let previousIsReactActEnvironment = getIsReactActEnvironment();
25-
beforeAll(() => {
26-
previousIsReactActEnvironment = getIsReactActEnvironment();
27-
setReactActEnvironment(true);
28-
});
19+
if (typeof beforeAll === 'function' && typeof afterAll === 'function') {
20+
// This matches the behavior of React < 18.
21+
let previousIsReactActEnvironment = getIsReactActEnvironment();
22+
beforeAll(() => {
23+
previousIsReactActEnvironment = getIsReactActEnvironment();
24+
setReactActEnvironment(true);
25+
});
2926

30-
afterAll(() => {
31-
setReactActEnvironment(previousIsReactActEnvironment);
32-
});
27+
afterAll(() => {
28+
setReactActEnvironment(previousIsReactActEnvironment);
29+
});
30+
}
3331
}
3432

3533
export * from './pure';

0 commit comments

Comments
 (0)