Skip to content

Commit

Permalink
Add tests for Menu screen
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-username committed Mar 29, 2018
1 parent 2c885af commit 8bf8bb0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/screens/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import Settings from '../components/Settings';
import Debuggers from '../components/Debuggers';

class Menu extends React.Component {
export class Menu extends React.Component {
static navigationOptions = {
title: 'Settings',
};
Expand Down Expand Up @@ -44,7 +44,7 @@ const mapStateToProps = state => ({
currentPasscode: getCurrentPasscode(state),
});

const mapDispatchToProps = dispatch => ({
export const mapDispatchToProps = dispatch => ({
handleTogglePasscodeRequirement(currentPasscode, requirePasscode) {
if (!currentPasscode && !requirePasscode) {
Alert.alert(
Expand Down
58 changes: 58 additions & 0 deletions src/screens/Menu.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react';
import { Alert } from 'react-native';

import { Menu, mapDispatchToProps as dispatchProps } from './Menu';
import {
TOGGLE_PASSCODE_REQUIREMENT,
RESET_PASSCODE_SETTINGS,
} from '../redux/modules/settings';

describe('Menu screen test', () => {
it('should render as expected', () => {
const wrapper = shallow(<Menu />);
expect(wrapper).toMatchSnapshot();
});

describe('mapDispatchToProps test', () => {
beforeEach(() => {
jest.resetModules();
jest.mock('Alert', () => ({
alert: jest.fn(),
}));
});

describe('handleTogglePasscodeRequirement test', () => {
it('should not activate passscode requirement if user no passcode set yet', () => {
const dispatchMock = jest.fn();
const props = dispatchProps(dispatchMock);
props.handleTogglePasscodeRequirement(null, false);
expect(Alert.alert.mock.calls.length).toBe(1);
expect(dispatchMock.mock.calls.length).toBe(0);
});

it('should activate passcode requirement if user has set a passcode', () => {
const dispatchMock = jest.fn();
const props = dispatchProps(dispatchMock);
props.handleTogglePasscodeRequirement('HASH', true);
expect(Alert.alert.mock.calls.length).toBe(0);
expect(dispatchMock.mock.calls.length).toBe(1);
expect(dispatchMock.mock.calls[0]).toEqual([
{ type: TOGGLE_PASSCODE_REQUIREMENT },
]);
});
});

describe('onPasscodeSettingsReset test', () => {
it('should dispatch reset action if user presses OK', () => {
const dispatchMock = jest.fn();
const props = dispatchProps(dispatchMock);
props.onPasscodeSettingsReset();
expect(Alert.alert.mock.calls.length).toBe(1);
Alert.alert.mock.calls[0][2][1].onPress();
expect(dispatchMock.mock.calls[0]).toEqual([
{ type: RESET_PASSCODE_SETTINGS },
]);
});
});
});
});
10 changes: 10 additions & 0 deletions src/screens/__snapshots__/Menu.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Menu screen test should render as expected 1`] = `
<ScrollView>
<Settings
handleSetPasscode={[Function]}
/>
<Debuggers />
</ScrollView>
`;

0 comments on commit 8bf8bb0

Please sign in to comment.