Skip to content

Commit 6039c03

Browse files
committed
init text
1 parent 9f02dd3 commit 6039c03

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

versioned_docs/version-7.x/testing.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ If you're not using Jest, then you'll need to mock these modules according to th
7979

8080
We recommend using [React Native Testing Library](https://callstack.github.io/react-native-testing-library/) along with [`jest-native`](https://github.com/testing-library/jest-native) to write your tests.
8181

82+
We will go through some real-world case test code examples. Each code example consist of navigator and test code file.
83+
84+
### Example 1
85+
86+
Navigate to another screen by button press.
87+
8288
<Tabs groupId="example" queryString="example">
8389
<TabItem value="static" label="Static" default>
8490

@@ -96,6 +102,34 @@ We recommend using [React Native Testing Library](https://callstack.github.io/re
96102
</TabItem>
97103
</Tabs>
98104

105+
We simulate user’s button press by `FireEvent` and call `expect` to check if rendered content is correct.
106+
107+
### Example 2
108+
109+
Navigate to another tab caused by tab bar button press.
110+
111+
We use `id` to query for tab buttons, simulate button press by `FireEvent` and check if rendered content is correct.
112+
113+
To setup tab `id` you need to add `tabBarButtonTestID` in settings tab screen `options` defined in `TabNavigator`.
114+
115+
Bottom tabs bar buttons `handlePress` function expects `GestureResponderEvent`. To avoid error you should pass `event` object as the second argument of `fireEvent`.
116+
117+
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.
118+
119+
### Example 3
120+
121+
Always display first screen of the settings stack nested in tab after settings stack tab focus.
122+
123+
We query tab buttons, simulating button presses and check if rendered screens are correct.
124+
125+
### Example 4
126+
127+
Display loading state while waiting for data and then fetched nick on every profile screen focus.
128+
129+
We query bottoms tabs buttons and mock fetch function using `spyOn` and `mockImplementation`. Then, we navigate to profile screen and checks if loading state displays correctly. To check if fetched data is displayed we add `await` to query - we need to wait for the fetch to finish before checking if fetched data is correct. To ensure that operation will succeed on every focus, we cause it again by navigating back to home, again to settings and check rendered content again.
130+
131+
It a good practice to use mocks while testing API functions. `mockedFetch` will override real implementation of fetch and enable us to make test deterministic. Thanks to `spyOnYou` you can check if mocked function was called using `toHaveBeenCalled` assertion.
132+
99133
## Best practices
100134

101135
There are a couple of things to keep in mind when writing tests for components using React Navigation:

0 commit comments

Comments
 (0)