From b2b3c8596bf1a5bfe1515f5092e255c0334f78fa Mon Sep 17 00:00:00 2001 From: Steve Galili Date: Thu, 18 Jun 2020 20:38:21 +0200 Subject: [PATCH] Update example-navigation.md Hi :wave: My name is Steve Galili, big fan of the library I've discovered lately after participating in a workshop given by Kent. I've added an extra navigation example that suits react-navigation v5. It will be great if you could update this in your docs. I have more examples up my sleeve see my example repo https://github.com/vanGalilea/react-native-testing I would be happy to contribute more to this repo, if you need more help. Email- galiliziv@gmail.com Twitter- https://twitter.com/vanGalilea Greetings, Steve --- docs/example-navigation.md | 93 +++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/docs/example-navigation.md b/docs/example-navigation.md index 0498c5d..23edcde 100644 --- a/docs/example-navigation.md +++ b/docs/example-navigation.md @@ -4,6 +4,98 @@ title: Navigation sidebar_label: Navigation --- +### react-navigation v5 +```js +// jest.config.js + +// react-native-gesture-handler use native modules, we mock it by using it's build in jestSetup. +// react-navigations will try to import it's assets, to avoid an error we will mock it by using a custom assets transformer + +module.exports = { + preset: '@testing-library/react-native', + setupFiles: ["./node_modules/react-native-gesture-handler/jestSetup.js"], + moduleNameMapper: { + "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "/assetsTransformer.js", + "\\.(css|less)$": "/assetsTransformer.js" + } +} +``` + +```js +// assetsTransformer.js +// see https://jestjs.io/docs/en/webpack.html#handling-static-assets +const path = require('path') + +module.exports = { + process(src, filename, config, options) { + return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';' + }, +} +``` + +```js +import React from 'react'; +import { Button, Text, View } from 'react-native'; +import {NavigationContainer, useNavigationState} from '@react-navigation/native' +import {createStackNavigator} from '@react-navigation/stack' +import {fireEvent, render, wait} from '@testing-library/react-native' + +const Home = ({ navigation }) => ( + + Home page +