Skip to content

Commit

Permalink
Add tests for Auth screen
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-username committed Mar 28, 2018
1 parent 7dd1128 commit 791537c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/screens/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
Expand Down
83 changes: 83 additions & 0 deletions src/screens/Auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import React from 'react';

import { Auth } from './Auth';

describe('Auth screen test', () => {
it('should render as expected', () => {
const wrapper = shallow(<Auth />, {
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(
<Auth requirePasscode={false} navigation={navMock} />
);
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(
<Auth requirePasscode={true} navigation={navMock} />
);
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(
<Auth
requirePasscode={true}
currentPasscode="5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
navigation={navMock}
/>
);

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(
<Auth
requirePasscode={true}
currentPasscode="5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
navigation={navMock}
/>
);
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' }],
});
});
});
8 changes: 8 additions & 0 deletions src/screens/__snapshots__/Auth.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Auth screen test should render as expected 1`] = `
<PasscodeAuth
error={null}
handlePasscodeSubmit={[Function]}
/>
`;

0 comments on commit 791537c

Please sign in to comment.