RNTL example app for React Navigation
This example shows how to write integration tests using React Navigation without mocking it. Presented approach has been consulted with and influenced by React Navigation team.
Recommended tests
There are two types of recommeded tests:
- Tests operating on navigator level - these use
renderNavigatorhelper to render a navigator component used in the app. It is useful when you want to test a scenario that includes multiple screens. - Tests operating on single screen level - these use regular
renderhelper but require refactoring screen components intoScreenandScreenContentcomponents. WhereScreenreceives React Navigation props and/or uses hooks likeuseNavigationwhileScreenContentdoes not have a direct relation to React Navigation API but gets props fromScreenand calls relevant callbacks to trigger navigation.
Note that this example applies
includeHiddenElements: falseby default, so all queries will ignore elements on the hidden screens, e.g. inactive tabs or screens present in stack navigators. This option is enabled injest-setup.jsfile, usingdefaultIncludeHiddenElements: falseoption toconfigurefunction.
Non-recommended tests
There also exists another popular type of screen level tests, where users mock React Navigation objects like navigation, route and/or hooks like useNavigation, etc. We don't recommend this way of testing. Mocking internal parts of the libraries is effectively testing implementation details, which goes against the Testing Library's Guiding Principles.