From 9344a0a0b0323f1f87bb8e52de929926c081cefe Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Fri, 3 Aug 2018 15:53:07 -0700 Subject: [PATCH] Add tests (unsure why Transitioner is failing) --- package.json | 2 +- src/navigators/__tests__/.eslintrc | 6 + .../__tests__/NestedNavigator-test.js | 40 ++ .../__tests__/StackNavigator-test.js | 93 +++++ .../NestedNavigator-test.js.snap | 367 ++++++++++++++++ .../__snapshots__/StackNavigator-test.js.snap | 391 ++++++++++++++++++ src/views/__tests__/Transitioner-test.js | 49 +++ yarn.lock | 6 +- 8 files changed, 950 insertions(+), 4 deletions(-) create mode 100644 src/navigators/__tests__/.eslintrc create mode 100644 src/navigators/__tests__/NestedNavigator-test.js create mode 100644 src/navigators/__tests__/StackNavigator-test.js create mode 100644 src/navigators/__tests__/__snapshots__/NestedNavigator-test.js.snap create mode 100644 src/navigators/__tests__/__snapshots__/StackNavigator-test.js.snap create mode 100644 src/views/__tests__/Transitioner-test.js diff --git a/package.json b/package.json index 3ce71f7..a518011 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "react": "16.3.1", "react-dom": "16.3.1", "react-native": "~0.55.4", - "react-navigation": "^2.10.0", + "react-navigation": "^2.11.2", "react-test-renderer": "16.3.1" }, "peerDependencies": { diff --git a/src/navigators/__tests__/.eslintrc b/src/navigators/__tests__/.eslintrc new file mode 100644 index 0000000..2d17718 --- /dev/null +++ b/src/navigators/__tests__/.eslintrc @@ -0,0 +1,6 @@ +{ + "extends": "../../../.eslintrc", + "env": { + "jest": true + }, +} diff --git a/src/navigators/__tests__/NestedNavigator-test.js b/src/navigators/__tests__/NestedNavigator-test.js new file mode 100644 index 0000000..8633bb3 --- /dev/null +++ b/src/navigators/__tests__/NestedNavigator-test.js @@ -0,0 +1,40 @@ +import React from 'react'; +import renderer from 'react-test-renderer'; +import StackNavigator from '../createContainedStackNavigator'; + +const SubNavigator = StackNavigator({ + Home: { + screen: () => null, + }, +}); + +const NavNestedDirect = StackNavigator({ + Sub: { + screen: SubNavigator, + }, +}); + +const NavNestedIndirect = StackNavigator({ + Sub: { + // eslint-disable-next-line react/display-name + screen: props => , + }, +}); + +/* Prevent React error boundaries from swallowing the errors */ +NavNestedIndirect.prototype.componentDidCatch = null; +SubNavigator.prototype.componentDidCatch = null; + +describe('Nested navigators', () => { + it('renders succesfully as direct child', () => { + const rendered = renderer.create().toJSON(); + expect(rendered).toMatchSnapshot(); + }); + + it('throw when trying to pass navigation prop', () => { + const tryRender = () => { + renderer.create(); + }; + expect(tryRender).toThrowErrorMatchingSnapshot(); + }); +}); diff --git a/src/navigators/__tests__/StackNavigator-test.js b/src/navigators/__tests__/StackNavigator-test.js new file mode 100644 index 0000000..08b2786 --- /dev/null +++ b/src/navigators/__tests__/StackNavigator-test.js @@ -0,0 +1,93 @@ +import React, { Component } from 'react'; +import { StyleSheet, View } from 'react-native'; +import renderer from 'react-test-renderer'; + +import StackNavigator from '../createContainedStackNavigator'; +import { withNavigation } from 'react-navigation'; +import NavigationTestUtils from 'react-navigation/NavigationTestUtils'; + +const styles = StyleSheet.create({ + header: { + opacity: 0.5, + }, +}); + +class HomeScreen extends Component { + static navigationOptions = ({ navigation }) => ({ + title: `Welcome ${ + navigation.state.params ? navigation.state.params.user : 'anonymous' + }`, + gesturesEnabled: true, + headerStyle: [{ backgroundColor: 'red' }, styles.header], + }); + + render() { + return null; + } +} + +const routeConfig = { + Home: { + screen: HomeScreen, + }, +}; + +describe('StackNavigator', () => { + beforeEach(() => { + NavigationTestUtils.resetInternalState(); + }); + + it('renders successfully', () => { + const MyStackNavigator = StackNavigator(routeConfig); + const rendered = renderer.create().toJSON(); + + expect(rendered).toMatchSnapshot(); + }); + + it('applies correct values when headerRight is present', () => { + const MyStackNavigator = StackNavigator({ + Home: { + screen: HomeScreen, + navigationOptions: { + headerRight: , + }, + }, + }); + const rendered = renderer.create().toJSON(); + + expect(rendered).toMatchSnapshot(); + }); + + it('passes navigation to headerRight when wrapped in withNavigation', () => { + const spy = jest.fn(); + + class TestComponent extends React.Component { + render() { + return {this.props.onPress(this.props.navigation)}; + } + } + + const TestComponentWithNavigation = withNavigation(TestComponent); + + class A extends React.Component { + static navigationOptions = { + headerRight: , + }; + + render() { + return ; + } + } + + const Nav = StackNavigator({ A: { screen: A } }); + + renderer.create(