0.0.21 • Published 3 years ago

react-native-navigation-test-renderer v0.0.21

Weekly downloads
56
License
MIT
Repository
-
Last release
3 years ago

React Native Navigation Test Renderer

Allows developers to render RNN screens without depending on the native hierarchy maintained by RNN.

Installation

  • npm install react-native-navigation-test-renderer --save, or
  • yarn add react-native-navigation-test-renderer

The problem

You want to write tests for your RNN application, right now you have two main options e2e tests using Detox. Detox is great but the feedback loop is very slow. After all e2e tests are not suited for TDD. The second option is a component test, but those aren't testing much as you can't move between screens

The solution

react-native-navigation-test-renderer provides you with the ability to move between screens, and to test complete flows with a fast feedback loop.

Usage

Create a TestComponent and wrap it with withNativeNavigation HOC

import React from "react";
import mockNavigation, {
  withNativeNavigation
} from "react-native-navigation-test-renderer";

// mock react-native-navigation imports with mockNavigation
jest.doMock("react-native-navigation", () => ({
  Navigation: mockNavigation
}));

 class TestComponent extends React.Component {
  render() {
    // withNativeNavigation injects the Screen to be rendered
    const { Screen } = this.props;
    return <Screen />;
  }
}


export default withNativeNavigation(TestComponent)

Now in your tests

import React from "react";
import { render, wait } from "@testing-library/react-native";
import mockNavigation from "react-native-navigation-test-renderer";
import TestComponent from "../TestComponent";

class SimpleScreen extends React.Component {
  render() {
    return (
      <View>
        <Text>Hello World</Text>
      </View>
    );
  }
}

mockNavigation.registerComponent("EXAMPLES.SIMPLE_SCEEN", () => SimpleScreen)

describe("Simple Screen Tests", () => {
  it("should render the screen", async () => {
    const { getByText } = render(
      <TestComponent
        component={{
          name: "EXAMPLES.SIMPLE_SCEEN"
        }}
      />
    );
    await wait();
    expect(getByText(/hello world/i)).toBeTruthy();
  });
});

See __test__ directory for more examples

0.0.21

3 years ago

0.0.20

3 years ago

0.0.17

3 years ago

0.0.18

3 years ago

0.0.19

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago