You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: versioned_docs/version-7.x/testing.md
+34Lines changed: 34 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -79,6 +79,12 @@ If you're not using Jest, then you'll need to mock these modules according to th
79
79
80
80
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.
81
81
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
+
82
88
<TabsgroupId="example"queryString="example">
83
89
<TabItemvalue="static"label="Static"default>
84
90
@@ -96,6 +102,34 @@ We recommend using [React Native Testing Library](https://callstack.github.io/re
96
102
</TabItem>
97
103
</Tabs>
98
104
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
+
99
133
## Best practices
100
134
101
135
There are a couple of things to keep in mind when writing tests for components using React Navigation:
0 commit comments