Skip to content

Commit e4fcbf4

Browse files
committed
refactor: split fake timer test to make it pass
1 parent 2a8fbb5 commit e4fcbf4

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/__tests__/waitFor.test.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,31 @@ test('waits for element with custom interval', async () => {
7979
expect(mockFn).toHaveBeenCalledTimes(2);
8080
});
8181

82-
test.each([TimerMode.Legacy, TimerMode.Modern])(
83-
'waits for element until it stops throwing using %s fake timers',
84-
async (fakeTimerType) => {
85-
jest.useFakeTimers(fakeTimerType);
86-
const { getByText, queryByText } = render(<BananaContainer />);
82+
test('waits for element until it stops throwing using modern fake timers', async () => {
83+
jest.useFakeTimers('modern');
84+
const { getByText, queryByText } = render(<BananaContainer />);
8785

88-
fireEvent.press(getByText('Change freshness!'));
89-
expect(queryByText('Fresh')).toBeNull();
86+
fireEvent.press(getByText('Change freshness!'));
87+
expect(queryByText('Fresh')).toBeNull();
9088

91-
jest.advanceTimersByTime(300);
92-
const freshBananaText = await waitFor(() => getByText('Fresh'));
89+
jest.advanceTimersByTime(300);
90+
const freshBananaText = await waitFor(() => getByText('Fresh'));
9391

94-
expect(freshBananaText.props.children).toBe('Fresh');
95-
}
96-
);
92+
expect(freshBananaText.props.children).toBe('Fresh');
93+
});
94+
95+
test('waits for element until it stops throwing using legacy fake timers', async () => {
96+
jest.useFakeTimers('legacy');
97+
const { getByText, queryByText } = render(<BananaContainer />);
98+
99+
fireEvent.press(getByText('Change freshness!'));
100+
expect(queryByText('Fresh')).toBeNull();
101+
102+
jest.advanceTimersByTime(300);
103+
const freshBananaText = await waitFor(() => getByText('Fresh'));
104+
105+
expect(freshBananaText.props.children).toBe('Fresh');
106+
});
97107

98108
test.each([TimerMode.Legacy, TimerMode.Modern])(
99109
'waits for assertion until timeout is met with %s fake timers',

0 commit comments

Comments
 (0)