Skip to content

Commit 5c5ec89

Browse files
committed
fix fake timers
1 parent c582029 commit 5c5ec89

File tree

1 file changed

+17
-36
lines changed

1 file changed

+17
-36
lines changed

versioned_docs/version-7.x/testing.md

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,11 @@ test('navigates to settings by tab bar button press', () => {
302302
const TabNavigation = createStaticNavigation(TabNavigator);
303303
render(<TabNavigation />);
304304

305-
act(() => jest.runAllTimers());
306-
307305
const button = screen.getByRole('button', { name: 'Settings, tab, 2 of 2' });
308306

309307
const event = {};
310308
fireEvent.press(button, event);
309+
act(() => jest.runAllTimers());
311310

312311
expect(screen.queryByText('Settings screen')).toBeOnTheScreen();
313312
});
@@ -332,12 +331,11 @@ test('navigates to settings by tab bar button press', () => {
332331
</NavigationContainer>
333332
);
334333

335-
act(() => jest.runAllTimers());
336-
337334
const button = screen.getByRole('button', { name: 'Settings, tab, 2 of 2' });
338335

339336
const event = {};
340337
fireEvent.press(button, event);
338+
act(() => jest.runAllTimers());
341339

342340
expect(screen.queryByText('Settings screen')).toBeOnTheScreen();
343341
});
@@ -363,10 +361,7 @@ const event = {};
363361
fireEvent.press(button, event);
364362
```
365363

366-
Sometimes navigation animations need some time to finish and render screen's content. You need to wait until animations finish before querying components. Therefore, you have to use `fake timers`. [`Fake Timers`](https://jestjs.io/docs/timer-mocks) replace real implementation of times function to use fake clock ticks. They allow you to instantly skip animation time and avoid getting state change error.
367-
368-
<Tabs>
369-
<TabItem value="static" label="Static">
364+
Sometimes navigation animations need some time to finish. You need to wait until animations finish before querying components. Therefore, you have to use `fake timers`. [`Fake Timers`](https://jestjs.io/docs/timer-mocks) replace real implementation of times function to use fake clock ticks. They allow you to instantly skip animations time and avoid getting state change error.
370365

371366
```js
372367
// Enable fake timers
@@ -378,16 +373,6 @@ jest.useFakeTimers();
378373
act(() => jest.runAllTimers());
379374
```
380375

381-
</TabItem>
382-
<TabItem value="dynamic" label="Dynamic">
383-
384-
```js
385-
386-
```
387-
388-
</TabItem>
389-
</Tabs>
390-
391376
### Example 3
392377

393378
Always displays settings screen after tab bar settings button press.
@@ -533,22 +518,18 @@ export function TabNavigator() {
533518
<TabItem value="static" label="Static" default>
534519

535520
```js
536-
import '@testing-library/react-native/extend-expect';
537-
538521
import { expect, jest, test } from '@jest/globals';
539522
import { createStaticNavigation } from '@react-navigation/native';
540523
import { act, fireEvent, render, screen } from '@testing-library/react-native';
541524

542525
import { TabNavigator } from './TabNavigator';
543526

544-
test('always displays settings screen after tab bar settings button press', async () => {
527+
test('always displays settings screen after tab bar settings button press', () => {
545528
jest.useFakeTimers();
546529

547530
const TabNavigation = createStaticNavigation(TabNavigator);
548531
render(<TabNavigation />);
549532

550-
act(() => jest.runAllTimers());
551-
552533
const homeTabButton = screen.getByRole('button', {
553534
name: 'Home, tab, 1 of 2',
554535
});
@@ -561,14 +542,18 @@ test('always displays settings screen after tab bar settings button press', asyn
561542

562543
fireEvent.press(settingsTabButton, event);
563544
expect(screen.queryByText('Settings screen')).toBeOnTheScreen();
545+
act(() => jest.runAllTimers());
564546

565547
fireEvent.press(screen.queryByText('Go to Details'), event);
548+
act(() => jest.runAllTimers());
566549
expect(screen.queryByText('Details screen')).toBeOnTheScreen();
567550

568551
fireEvent.press(homeTabButton, event);
552+
act(() => jest.runAllTimers());
569553
expect(screen.queryByText('Home screen')).toBeOnTheScreen();
570554

571555
fireEvent.press(settingsTabButton, event);
556+
act(() => jest.runAllTimers());
572557
expect(screen.queryByText('Settings screen')).toBeOnTheScreen();
573558
});
574559
```
@@ -577,15 +562,13 @@ test('always displays settings screen after tab bar settings button press', asyn
577562
<TabItem value="dynamic" label="Dynamic">
578563

579564
```js
580-
import '@testing-library/react-native/extend-expect';
581-
582565
import { expect, jest, test } from '@jest/globals';
583566
import { NavigationContainer } from '@react-navigation/native';
584567
import { act, fireEvent, render, screen } from '@testing-library/react-native';
585568

586569
import { TabNavigator } from './TabNavigator';
587570

588-
test('always displays settings screen after tab bar settings button press', async () => {
571+
test('always displays settings screen after tab bar settings button press', () => {
589572
jest.useFakeTimers();
590573

591574
render(
@@ -594,8 +577,6 @@ test('always displays settings screen after tab bar settings button press', asyn
594577
</NavigationContainer>
595578
);
596579

597-
act(() => jest.runAllTimers());
598-
599580
const homeTabButton = screen.getByRole('button', {
600581
name: 'Home, tab, 1 of 2',
601582
});
@@ -606,15 +587,19 @@ test('always displays settings screen after tab bar settings button press', asyn
606587
const event = {};
607588

608589
fireEvent.press(settingsTabButton, event);
590+
act(() => jest.runAllTimers());
609591
expect(screen.queryByText('Settings screen')).toBeOnTheScreen();
610592

611593
fireEvent.press(screen.queryByText('Go to Details'), event);
594+
act(() => jest.runAllTimers());
612595
expect(screen.queryByText('Details screen')).toBeOnTheScreen();
613596

614597
fireEvent.press(homeTabButton, event);
598+
act(() => jest.runAllTimers());
615599
expect(screen.queryByText('Home screen')).toBeOnTheScreen();
616600

617601
fireEvent.press(settingsTabButton, event);
602+
act(() => jest.runAllTimers());
618603
expect(screen.queryByText('Settings screen')).toBeOnTheScreen();
619604
});
620605
```
@@ -754,8 +739,6 @@ export function TabNavigator() {
754739
<TabItem value="static" label="Static" default>
755740

756741
```js
757-
import '@testing-library/react-native/extend-expect';
758-
759742
import { expect, jest, test } from '@jest/globals';
760743
import { createStaticNavigation } from '@react-navigation/native';
761744
import { act, fireEvent, render, screen } from '@testing-library/react-native';
@@ -783,8 +766,6 @@ test('Display loading state while waiting for data and then fetched profile nick
783766
const TabNavigation = createStaticNavigation(TabNavigator);
784767
render(<TabNavigation />);
785768

786-
act(() => jest.runAllTimers());
787-
788769
const spy = jest.spyOn(window, 'fetch').mockImplementation(mockedFetch);
789770

790771
const homeTabButton = screen.getByRole('button', {
@@ -797,13 +778,15 @@ test('Display loading state while waiting for data and then fetched profile nick
797778

798779
const event = {};
799780
fireEvent.press(profileTabButton, event);
781+
act(() => jest.runAllTimers());
800782

801783
expect(screen.queryByText('Loading')).toBeOnTheScreen();
802784
expect(spy).toHaveBeenCalled();
803785
expect(await screen.findByText('CookieDough')).toBeOnTheScreen();
804786

805787
fireEvent.press(homeTabButton, event);
806788
fireEvent.press(profileTabButton, event);
789+
act(() => jest.runAllTimers());
807790

808791
expect(screen.queryByText('Loading')).toBeOnTheScreen();
809792
expect(spy).toHaveBeenCalled();
@@ -815,8 +798,6 @@ test('Display loading state while waiting for data and then fetched profile nick
815798
<TabItem value="dynamic" label="Dynamic">
816799

817800
```js
818-
import '@testing-library/react-native/extend-expect';
819-
820801
import { expect, jest, test } from '@jest/globals';
821802
import { NavigationContainer } from '@react-navigation/native';
822803
import { act, fireEvent, render, screen } from '@testing-library/react-native';
@@ -847,8 +828,6 @@ test('Display loading state while waiting for data and then fetched profile nick
847828
</NavigationContainer>
848829
);
849830

850-
act(() => jest.runAllTimers());
851-
852831
const spy = jest.spyOn(window, 'fetch').mockImplementation(mockedFetch);
853832

854833
const homeTabButton = screen.getByRole('button', {
@@ -860,13 +839,15 @@ test('Display loading state while waiting for data and then fetched profile nick
860839

861840
const event = {};
862841
fireEvent.press(profileTabButton, event);
842+
act(() => jest.runAllTimers());
863843

864844
expect(screen.queryByText('Loading')).toBeOnTheScreen();
865845
expect(spy).toHaveBeenCalled();
866846
expect(await screen.findByText('CookieDough')).toBeOnTheScreen();
867847

868848
fireEvent.press(homeTabButton, event);
869849
fireEvent.press(profileTabButton, event);
850+
act(() => jest.runAllTimers());
870851

871852
expect(screen.queryByText('Loading')).toBeOnTheScreen();
872853
expect(spy).toHaveBeenCalled();

0 commit comments

Comments
 (0)