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
+171
Original file line number
Diff line number
Diff line change
@@ -783,3 +783,174 @@ In the above test, we:
783
783
In a production app, we recommend using a library like [React Query](https://tanstack.com/query/) to handle data fetching and caching. The above example is for demonstration purposes only.
784
784
785
785
:::
786
+
787
+
### Re-usable components
788
+
789
+
To make it easier to test components that don't depend on the navigation structure, we can create a light-weight test navigator:
Here we create a test stack navigator using the `createTestStackNavigator` function. We then render the `MyComponent` component within the test navigator and assert that the modal is shown or hidden based on the focus state.
933
+
934
+
The `initialState` prop is used to set the initial state of the navigator, i.e. which screens are rendered in the stack and which one is focused. See [navigation state](navigation-state.md) for more information on the structure of the state object.
935
+
936
+
You can also pass a [`ref`](navigation-container.md#ref) to programmatically navigate in your tests.
937
+
938
+
The test navigator is a simplified version of the stack navigator, but it's still a real navigator and behaves like one. This means that you can use it to test any other navigation logic.
939
+
940
+
See [Custom navigators](custom-navigators.md) for more information on how to write custom navigators if you want adjust the behavior of the test navigator or add more functionality.
941
+
942
+
## Best practices
943
+
944
+
Generally, we recommend avoiding mocking React Navigation. Mocking can help you isolate the component you're testing, but when testing components with navigation logic, mocking means that your tests don't test for the navigation logic.
945
+
946
+
- Mocking APIs such as `useFocusEffect` means you're not testing the focus logic in your component.
947
+
- Mocking `navigation` prop or `useNavigation` means that the `navigation` object may not have the same shape as the real one.
948
+
- Asserting `navigation.navigate` calls means you only test that the function was called, not that the call was correct based on the navigation structure.
949
+
- etc.
950
+
951
+
Avoiding mocks means additional work when writing tests, but it also means:
952
+
953
+
- Refactors that don't change the logic won't break the tests, e.g. changing `navigation` prop to `useNavigation`, using a different navigation action that does the same thing, etc.
954
+
- Library upgrades or refactor that actually change the behavior will correctly break the tests, surfacing actual regressions.
955
+
956
+
Tests should break when there's a regression, not due to a refactor. Otherwise it leads to additional work to fix the tests, making it harder to know when a regression is introduced.
0 commit comments