From 791537c9772901f35654fe95250f9f8a317b79d6 Mon Sep 17 00:00:00 2001 From: LA Roberto Date: Wed, 28 Mar 2018 17:59:03 +0800 Subject: [PATCH] Add tests for Auth screen --- src/screens/Auth.js | 2 +- src/screens/Auth.test.js | 83 +++++++++++++++++++++ src/screens/__snapshots__/Auth.test.js.snap | 8 ++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/screens/Auth.test.js create mode 100644 src/screens/__snapshots__/Auth.test.js.snap diff --git a/src/screens/Auth.js b/src/screens/Auth.js index 59c3300..e24900e 100644 --- a/src/screens/Auth.js +++ b/src/screens/Auth.js @@ -9,7 +9,7 @@ import { } from '../redux/modules/settings'; import { hashString } from '../utils/crypto'; -class Auth extends React.Component { +export class Auth extends React.Component { static navigationOptions = { title: 'Passcode Auth', }; diff --git a/src/screens/Auth.test.js b/src/screens/Auth.test.js new file mode 100644 index 0000000..ff24e6b --- /dev/null +++ b/src/screens/Auth.test.js @@ -0,0 +1,83 @@ +import React from 'react'; + +import { Auth } from './Auth'; + +describe('Auth screen test', () => { + it('should render as expected', () => { + const wrapper = shallow(, { + disableLifecycleMethods: true, + }); + expect(wrapper).toMatchSnapshot(); + }); + + it('should redirect to Home route if passcode protection is off', () => { + const navMock = { + navigate: jest.fn(), + dispatch: jest.fn(), + }; + const wrapper = shallow( + + ); + expect(navMock.navigate.mock.calls.length).toBe(1); + expect(navMock.navigate.mock.calls[0][0]).toBe('Home'); + expect(navMock.dispatch.mock.calls[0][0]).toEqual({ + type: 'Navigation/RESET', + index: 0, + key: undefined, + actions: [{ type: 'Navigation/NAVIGATE', routeName: 'Home' }], + }); + }); + + it('should not redirect to Home route if passcode protection is on', () => { + const navMock = { + navigate: jest.fn(), + dispatch: jest.fn(), + }; + const wrapper = shallow( + + ); + expect(navMock.navigate.mock.calls.length).toBe(0); + }); + + it('should throw an error if entered passcode is invalid and not redirect to Home', () => { + const navMock = { + navigate: jest.fn(), + dispatch: jest.fn(), + }; + const wrapper = shallow( + + ); + + wrapper.instance()._isPasscodeValid('notpassword'); + expect(wrapper.state('error')).toEqual('Passcode Invalid!'); + expect(navMock.navigate.mock.calls.length).toBe(0); + }); + + it('should redirect to Home if passcode entered is valid', () => { + const navMock = { + navigate: jest.fn(), + dispatch: jest.fn(), + }; + const wrapper = shallow( + + ); + wrapper.instance()._isPasscodeValid('password'); + expect(wrapper.state('error')).toEqual(null); + expect(navMock.navigate.mock.calls.length).toBe(1); + expect(navMock.navigate.mock.calls[0][0]).toBe('Home'); + expect(navMock.dispatch.mock.calls[0][0]).toEqual({ + type: 'Navigation/RESET', + index: 0, + key: undefined, + actions: [{ type: 'Navigation/NAVIGATE', routeName: 'Home' }], + }); + }); +}); diff --git a/src/screens/__snapshots__/Auth.test.js.snap b/src/screens/__snapshots__/Auth.test.js.snap new file mode 100644 index 0000000..757bffb --- /dev/null +++ b/src/screens/__snapshots__/Auth.test.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Auth screen test should render as expected 1`] = ` + +`;