From 20ff1fb43350f529e9c359a785b1384471bdc003 Mon Sep 17 00:00:00 2001 From: AnCichocka Date: Mon, 7 Oct 2024 14:08:07 +0200 Subject: [PATCH 01/25] add missing sections --- versioned_docs/version-7.x/testing.md | 155 +++++++++++++++++++++----- 1 file changed, 128 insertions(+), 27 deletions(-) diff --git a/versioned_docs/version-7.x/testing.md b/versioned_docs/version-7.x/testing.md index 07b86a0e892..ac83a3e59bb 100644 --- a/versioned_docs/version-7.x/testing.md +++ b/versioned_docs/version-7.x/testing.md @@ -25,18 +25,12 @@ If you're using `@react-navigation/stack`, you will only need to mock: To add the mocks, create a file `jest/setup.js` (or any other file name of your choice) and paste the following code in it: ```js -// include this line for mocking react-native-gesture-handler +// Include this line for mocking react-native-gesture-handler import 'react-native-gesture-handler/jestSetup'; -// include this section and the NativeAnimatedHelper section for mocking react-native-reanimated +// Include this section for mocking react-native-reanimated jest.mock('react-native-reanimated', () => { - const Reanimated = require('react-native-reanimated/mock'); - - // The mock for `call` immediately calls the callback which is incorrect - // So we override it with a no-op - Reanimated.default.call = () => {}; - - return Reanimated; + require('react-native-reanimated/mock'); }); // Silence the warning: Animated: `useNativeDriver` is not supported because the native animated module is missing @@ -54,53 +48,160 @@ Then we need to use this setup file in our jest config. You can add it under `se Make sure that the path to the file in `setupFiles` is correct. Jest will run these files before running your tests, so it's the best place to put your global mocks. +If your configuration works correctly, you can skip this section, but in some unusual cases you will need to mock `react-native-screen` as well. To do so add the following code in `jest/setup.js` file: + +```js +// Include this section form mocking react-native-screens +jest.mock('react-native-screens', () => { + // Require actual module instead of a mock + let screens = jest.requireActual('react-native-screens'); + + // All exports in react-native-screens are getters + // We cannot use spread for cloning as it will call the getters + // So we need to clone it with Object.create + screens = Object.create( + Object.getPrototypeOf(screens), + Object.getOwnPropertyDescriptors(screens) + ); + + return screens; +}); +``` + If you're not using Jest, then you'll need to mock these modules according to the test framework you are using. ## Writing tests 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. -Example: +We are going to write example tests for Root Navigator defined below: - + -```js name='Testing with jest' -import * as React from 'react'; -import { screen, render, fireEvent } from '@testing-library/react-native'; -import { createStaticNavigation } from '@react-navigation/native'; -import { RootNavigator } from './RootNavigator'; +```js + +``` + + + + +```js +import { Button, Text, View } from 'react-native'; +import { createNativeStackNavigator } from '@react-navigation/native-stack'; + +const Profile = ({ navigation }) => { + return ( + + Profile +