Skip to content

Commit

Permalink
Merge pull request #564 from CruGlobal/develop
Browse files Browse the repository at this point in the history
develop > master
  • Loading branch information
reldredge71 committed Oct 5, 2018
2 parents fb6f1aa + 70686cd commit d9dde45
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 67 deletions.
2 changes: 2 additions & 0 deletions __tests__/PeopleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const orgs = [
type: 'person',
},
],
user_created: false,
},
{
id: 20,
Expand All @@ -50,6 +51,7 @@ const orgs = [
type: 'person',
},
],
user_created: true,
},
];

Expand Down
23 changes: 1 addition & 22 deletions __tests__/__snapshots__/PeopleList.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ exports[`renders correctly as Jean 1`] = `
},
],
"type": "organization",
"user_created": false,
},
]
}
Expand Down Expand Up @@ -319,28 +320,6 @@ exports[`renders correctly as Jean 1`] = `
direction="row"
justify="end"
>
<IconButton
name="addContactIcon"
onPress={[Function]}
pressProps={
Array [
Object {
"expanded": true,
"id": 20,
"name": "org 2",
"people": Array [
Object {
"id": 21,
"type": "person",
},
],
"type": "organization",
},
]
}
size={24}
type="MissionHub"
/>
<IconButton
name="upArrowIcon"
onPress={[Function]}
Expand Down
93 changes: 92 additions & 1 deletion __tests__/actions/organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ import {
getOrganizationContacts,
getOrganizationMembers,
getOrganizationMembersNextPage,
addNewPerson,
getMyCommunities,
} from '../../src/actions/organizations';

jest.mock('../../src/selectors/organizations');
jest.mock('../../src/actions/api');

const myId = '1';

const mockStore = configureStore([thunk]);
let store;
const auth = { person: { user: {} } };
const auth = { person: { user: {}, id: myId } };

beforeEach(() => {
store = mockStore({ auth });
Expand Down Expand Up @@ -334,6 +337,94 @@ describe('getOrganizationMembers', () => {
});
});

describe('addNewPerson', () => {
const firstName = 'Fred';
let data = { firstName };
let bodyData = {
data: {
type: 'person',
attributes: {
first_name: firstName,
last_name: undefined,
gender: undefined,
},
},
included: [],
};
const apiResponse = { type: 'api response' };

beforeEach(() => {
callApi.mockReturnValue(apiResponse);
});

it('adds person with only first name', () => {
store.dispatch(addNewPerson(data));

expect(callApi).toHaveBeenCalledWith(REQUESTS.ADD_NEW_PERSON, {}, bodyData);
});

it('adds person with includes', () => {
const lastName = 'Smith';
const gender = 'male';
const orgId = '123';
const orgPermission = { permission_id: '2' };
const email = 'fred.smith@cru.org';
const phone = '111-111-1111';

data = {
firstName,
lastName,
gender,
orgId,
orgPermission,
email,
phone,
assignToMe: true,
};
bodyData = {
data: {
type: 'person',
attributes: {
first_name: firstName,
last_name: lastName,
gender,
},
},
included: [
{
type: 'contact_assignment',
attributes: {
assigned_to_id: myId,
organization_id: orgId,
},
},
{
type: 'organizational_permission',
attributes: {
organization_id: orgId,
permission_id: orgPermission.permission_id,
},
},
{
type: 'email',
attributes: { email },
},
{
type: 'phone_number',
attributes: {
number: phone,
location: 'mobile',
},
},
],
};

store.dispatch(addNewPerson(data));

expect(callApi).toHaveBeenCalledWith(REQUESTS.ADD_NEW_PERSON, {}, bodyData);
});
});

describe('getMyCommunities', () => {
it('should get my communities', async () => {
const response = {
Expand Down
65 changes: 50 additions & 15 deletions __tests__/containers/AddContactScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
testSnapshotShallow,
} from '../../testUtils';
import AddContactScreen from '../../src/containers/AddContactScreen';
import { addNewContact } from '../../src/actions/organizations';
import { addNewPerson } from '../../src/actions/organizations';
import { updatePerson } from '../../src/actions/person';
import * as organizations from '../../src/actions/organizations';
import * as person from '../../src/actions/person';
Expand All @@ -23,8 +23,8 @@ const organization = { id: 2 };

const mockContactAssignment = { id: 123, assigned_to: me };

const mockAddNewContact = {
type: 'add new contact',
const mockAddNewPerson = {
type: 'add new person',
response: {
id: contactId,
first_name: contactFName,
Expand All @@ -33,7 +33,7 @@ const mockAddNewContact = {
},
};
jest.mock('../../src/actions/organizations', () => ({
addNewContact: jest.fn(() => mockAddNewContact),
addNewPerson: jest.fn(() => mockAddNewPerson),
}));
const mockUpdatePerson = {
type: 'update person',
Expand Down Expand Up @@ -64,10 +64,12 @@ function buildScreenInstance(props) {
return buildScreen(props).instance();
}

beforeEach(() =>
organizations.addNewContact.mockImplementation(
jest.fn(() => mockAddNewContact),
));
beforeEach(() => {
jest.clearAllMocks();
organizations.addNewPerson.mockImplementation(
jest.fn(() => mockAddNewPerson),
);
});

it('renders correctly', () => {
testSnapshotShallow(
Expand Down Expand Up @@ -114,8 +116,11 @@ describe('savePerson', () => {

await componentInstance.savePerson();

expect(addNewContact).toHaveBeenCalledWith({ first_name: contactFName });
expect(store.dispatch).toHaveBeenCalledWith(mockAddNewContact);
expect(addNewPerson).toHaveBeenCalledWith({
assignToMe: true,
first_name: contactFName,
});
expect(store.dispatch).toHaveBeenCalledWith(mockAddNewPerson);
});

it('should add a new person with an org', async () => {
Expand All @@ -131,11 +136,35 @@ describe('savePerson', () => {

await componentInstance.savePerson();

expect(addNewContact).toHaveBeenCalledWith({
expect(addNewPerson).toHaveBeenCalledWith({
first_name: contactFName,
orgId: organization.id,
assignToMe: true,
});
expect(store.dispatch).toHaveBeenCalledWith(mockAddNewPerson);
});

it('should add a new person with an org without contact assignment', async () => {
const componentInstance = buildScreenInstance({
navigation: createMockNavState(),
organization,
isInvite: true,
});
componentInstance.setState({
person: {
first_name: contactFName,
},
});

await componentInstance.savePerson();

expect(addNewPerson).toHaveBeenCalledWith({
first_name: contactFName,
orgId: organization.id,
assignToMe: false,
});
expect(store.dispatch).toHaveBeenCalledWith(mockAddNewContact);
expect(store.dispatch).toHaveBeenCalledWith(mockAddNewPerson);
expect(navigatePush).not.toHaveBeenCalled();
});

it('should navigate to person stage screen', async () => {
Expand Down Expand Up @@ -182,8 +211,11 @@ describe('savePerson', () => {

await componentInstance.savePerson();

expect(addNewContact).toHaveBeenCalledWith({ first_name: contactFName });
expect(store.dispatch).toHaveBeenCalledWith(mockAddNewContact);
expect(addNewPerson).toHaveBeenCalledWith({
assignToMe: true,
first_name: contactFName,
});
expect(store.dispatch).toHaveBeenCalledWith(mockAddNewPerson);
expect(onCompleteMock).toHaveBeenCalledTimes(1);
expect(navigateBack).toHaveBeenCalledTimes(0);
});
Expand All @@ -207,6 +239,7 @@ describe('savePerson', () => {
expect(updatePerson).toHaveBeenCalledWith({
first_name: contactFName,
id: contactId,
assignToMe: true,
});
expect(store.dispatch).toHaveBeenCalledWith(mockUpdatePerson);
expect(navigateBack).toHaveBeenCalled();
Expand All @@ -229,6 +262,7 @@ describe('savePerson', () => {
expect(updatePerson).toHaveBeenCalledWith({
first_name: contactFName,
id: contactId,
assignToMe: true,
});
expect(store.dispatch).toHaveBeenCalledWith(mockUpdatePerson);
expect(navigatePush).toHaveBeenCalledWith(PERSON_STAGE_SCREEN, {
Expand All @@ -241,7 +275,7 @@ describe('savePerson', () => {
contactAssignmentId: mockContactAssignment.id,
section: 'people',
subsection: 'person',
orgId: organization.id,
orgId: undefined,
});
});

Expand All @@ -263,6 +297,7 @@ describe('savePerson', () => {

expect(updatePerson).toHaveBeenCalledWith({
id: contactId,
assignToMe: true,
});
});

Expand Down
12 changes: 8 additions & 4 deletions __tests__/containers/Groups/GroupScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ jest.mock('../../../src/actions/navigation', () => ({
navigatePush: jest.fn(),
}));

const organization = { id: '5', name: 'Test Org' };
const organization = { id: '5', name: 'Test Org', user_created: false };

describe('GroupScreen', () => {
const header = (
const createHeader = org => (
<GroupScreen
navigation={createMockNavState({
organization: { id: '5', name: 'Test Org' },
organization: org,
})}
/>
);

it('should render header correctly', () => {
testSnapshotShallow(header);
testSnapshotShallow(createHeader(organization));
});

it('should render header correctly for user_created org', () => {
testSnapshotShallow(createHeader({ ...organization, user_created: true }));
});

it('should handle add contact button correctly', () => {
Expand Down
9 changes: 9 additions & 0 deletions __tests__/containers/Groups/__snapshots__/GroupScreen.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ exports[`GroupScreen should render header correctly 1`] = `
title="Test Org"
/>
`;

exports[`GroupScreen should render header correctly for user_created org 1`] = `
<Connect(Header)
left={<Connect(BackButton) />}
right={null}
shadow={false}
title="Test Org"
/>
`;
18 changes: 10 additions & 8 deletions src/actions/organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export function getOrganizationMembersNextPage(orgId) {
};
}

export function addNewContact(data) {
export function addNewPerson(data) {
return (dispatch, getState) => {
const {
person: { id: myId },
Expand All @@ -252,13 +252,15 @@ export function addNewContact(data) {
);
}
const included = [];
included.push({
type: 'contact_assignment',
attributes: {
assigned_to_id: myId,
organization_id: data.orgId || undefined,
},
});
if (data.assignToMe) {
included.push({
type: 'contact_assignment',
attributes: {
assigned_to_id: myId,
organization_id: data.orgId || undefined,
},
});
}
if (data.orgId) {
included.push({
type: 'organizational_permission',
Expand Down
2 changes: 1 addition & 1 deletion src/actions/person.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export function updatePerson(data) {
},
]
: []),
...(data.orgPermission
...(data.orgPermission && data.orgPermission.permission_id
? [
{
type: 'organizational_permission',
Expand Down

0 comments on commit d9dde45

Please sign in to comment.