Skip to content

Commit

Permalink
Add tests for Details screen
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-username committed Mar 28, 2018
1 parent 791537c commit fbce085
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/screens/Details.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
deleteEntry,
} from '../redux/modules/listings';

class Details extends React.Component {
export class Details extends React.Component {
static navigationOptions = {
title: 'Details',
};
Expand Down
69 changes: 69 additions & 0 deletions src/screens/Details.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React from 'react';
import { Alert } from 'react-native';

import { Details } from './Details';
import { MODIFY_ENTRY } from '../redux/modules/listings';

describe('Details screen test', () => {
const navigationMock = {
state: {
params: {
id: 10,
},
},
};

const entryDetailsMock = {
name: 'malak',
code: '1234',
};

it('should render as expected', () => {
const wrapper = shallow(
<Details navigation={navigationMock} entryDetails={entryDetailsMock} />
);
expect(wrapper).toMatchSnapshot();
});

it('should navigate to Entry screen and pass the correct params on edit', () => {
const navMock = {
...navigationMock,
navigate: jest.fn(),
};
const wrapper = shallow(
<Details navigation={navMock} entryDetails={entryDetailsMock} />
);
wrapper.instance().handleEditPress();
expect(navMock.navigate.mock.calls.length).toBe(1);
expect(navMock.navigate.mock.calls[0][0]).toBe('Entry');
expect(navMock.navigate.mock.calls[0][1]).toEqual({
action: MODIFY_ENTRY,
id: 10,
});
});

it('should delete entry and navigate back to Home on alert click', () => {
jest.mock('Alert', () => ({
alert: jest.fn(),
}));

const mockDelete = jest.fn();
const navMock = {
...navigationMock,
popToTop: jest.fn(),
};

const wrapper = shallow(
<Details
navigation={navMock}
entryDetails={entryDetailsMock}
handleDeleteEntry={mockDelete}
/>
);
wrapper.instance().handleDeletionPress();
Alert.alert.mock.calls[0][2][1].onPress();
expect(mockDelete.mock.calls.length).toBe(1);
expect(mockDelete.mock.calls[0][0]).toBe(10);
expect(navMock.popToTop.mock.calls.length).toBe(1);
});
});
10 changes: 10 additions & 0 deletions src/screens/__snapshots__/Details.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[`Details screen test should render as expected 1`] = `
<EntryDetail
code="1234"
name="malak"
onDeletePress={[Function]}
onEditPress={[Function]}
/>
`;

0 comments on commit fbce085

Please sign in to comment.