Skip to content

Commit

Permalink
Merge pull request #560 from CruGlobal/develop
Browse files Browse the repository at this point in the history
develop > master
  • Loading branch information
reldredge71 committed Oct 1, 2018
2 parents 110efbb + d2af705 commit 3c9f0d9
Show file tree
Hide file tree
Showing 22 changed files with 1,253 additions and 60 deletions.
86 changes: 82 additions & 4 deletions __tests__/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import ReactNative from 'react-native';
import Adapter from 'enzyme-adapter-react-16/build/index';
import { shallow } from 'enzyme/build/index';
import Enzyme from 'enzyme/build/index';
import { Crashlytics } from 'react-native-fabric';
import StackTrace from 'stacktrace-js';

import App from '../src/App';
import {
Expand All @@ -18,8 +20,6 @@ Enzyme.configure({ adapter: new Adapter() });

jest.mock('../src/AppNavigator', () => ({ AppNavigator: 'mockAppNavigator' }));

jest.mock('react-native-fabric');

jest.mock('react-native-default-preference', () => ({
get: jest.fn().mockReturnValue(Promise.reject()),
}));
Expand All @@ -33,6 +33,8 @@ jest.mock('react-navigation-redux-helpers', () => ({
createReactNavigationReduxMiddleware: jest.fn(),
}));

jest.mock('stacktrace-js');

jest.mock('../src/store', () => ({
store: require('../testUtils').createMockStore(),
persistor: {},
Expand All @@ -51,15 +53,21 @@ const lastTwoArgs = [
{ onDismiss: expect.anything() },
];

const stackFrames = [{ id: '1' }, { id: '2' }];
const shiftedStackFrames = [stackFrames[1]];
const stacktraceResponse = new Promise(resolve => {
resolve(stackFrames);
});

beforeEach(() =>
(ReactNative.Alert.alert = jest
.fn()
.mockImplementation((_, __, buttons) => buttons[0].onPress())));

const test = response => {
const test = async response => {
const shallowScreen = shallow(<App />);

shallowScreen.instance().handleError(response);
await shallowScreen.instance().handleError(response);

return shallowScreen;
};
Expand Down Expand Up @@ -127,3 +135,73 @@ it('should not show alert if no error message', () => {

expect(ReactNative.Alert.alert).not.toHaveBeenCalled();
});

describe('__DEV__ === false', () => {
let dev;
beforeAll(() => {
dev = __DEV__;
__DEV__ = false;
});

beforeEach(() => {
jest.clearAllMocks();
StackTrace.fromError.mockReturnValue(stacktraceResponse);
StackTrace.get.mockReturnValue(stacktraceResponse);
});

afterAll(() => {
__DEV__ = dev;
});

it('Sends Crashlytics report for API error', async () => {
const apiError = {
apiError: { message: 'Error Text' },
key: 'ADD_NEW_PERSON',
method: 'POST',
endpoint: 'apis/v4/people',
query: { filters: { organization_ids: '1' } },
};

await test(apiError);

expect(Crashlytics.recordCustomExceptionName).toHaveBeenCalledWith(
`API Error: ${apiError.key} ${apiError.method.toUpperCase()} ${
apiError.endpoint
}`,
`\n\nQuery Params:\n${JSON.stringify(
apiError.query,
null,
2,
)}\n\nResponse:\n${JSON.stringify(apiError.apiError, null, 2)}`,
stackFrames,
);
});

it('Sends Crashlytics report for error with message', async () => {
const errorName = 'Error Name';
const errorDetails = 'Error Details';
const apiError = { message: `${errorName}\n${errorDetails}` };

await test(apiError);

expect(Crashlytics.recordCustomExceptionName).toHaveBeenCalledWith(
errorName,
apiError.message,
stackFrames,
);
});

test({ apiError: {}, key: 'ADD_NEW_PERSON', method: '', message: '' });

it('Sends Crashlytics report for unknown error', async () => {
const unknownError = { key: 'test', method: '' };

await test(unknownError);

expect(Crashlytics.recordCustomExceptionName).toHaveBeenCalledWith(
'Unknown Error',
JSON.stringify(unknownError),
shiftedStackFrames,
);
});
});
45 changes: 28 additions & 17 deletions __tests__/actions/person.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import * as analytics from '../../src/actions/analytics';
import { navigatePush } from '../../src/actions/navigation';
import {
CONTACT_PERSON_SCREEN,
IS_USER_CREATED_MEMBER_PERSON_SCREEN,
IS_GROUPS_MEMBER_PERSON_SCREEN,
MEMBER_PERSON_SCREEN,
ME_PERSONAL_PERSON_SCREEN,
Expand Down Expand Up @@ -610,36 +611,49 @@ describe('navToPersonScreen', () => {
personSelector.mockReturnValue(person);
});

afterEach(() => {
const testResult = (route, testPerson, testOrg) => {
expect(organizationSelector).toHaveBeenCalledWith(
{ organizations },
{ orgId: organization.id },
{ orgId: testOrg.id },
);
expect(personSelector).toHaveBeenCalledWith(
{ people },
{ orgId: organization.id, personId: person.id },
{ orgId: testOrg.id, personId: testPerson.id },
);
expect(orgPermissionSelector).toHaveBeenCalledWith(null, {
person,
organization,
person: testPerson,
organization: testOrg,
});
expect(contactAssignmentSelector).toHaveBeenCalledWith(
{ auth },
{ person, orgId: organization.id },
{ person: testPerson, orgId: testOrg.id },
);
expect(navigatePush).toHaveBeenCalledWith(route, {
person: testPerson,
organization: testOrg,
});
};

describe('isUserCreatedOrg', () => {
it('navigates to user created member person screen', () => {
const userCreatedOrg = { ...organization, user_created: true };
organizationSelector.mockReturnValue(userCreatedOrg);

store.dispatch(navToPersonScreen(person, userCreatedOrg));

testResult(
IS_USER_CREATED_MEMBER_PERSON_SCREEN,
person,
userCreatedOrg,
);
});
});

describe('isGroups', () => {
it('navigates to groups member person screen', () => {
store.dispatch(navToPersonScreen(person, organization));

expect(navigatePush).toHaveBeenCalledWith(
IS_GROUPS_MEMBER_PERSON_SCREEN,
{
person,
organization,
},
);
testResult(IS_GROUPS_MEMBER_PERSON_SCREEN, person, organization);
});
});

Expand All @@ -654,10 +668,7 @@ describe('navToPersonScreen', () => {

store.dispatch(navToPersonScreen(person, organization));

expect(navigatePush).toHaveBeenCalledWith(MEMBER_PERSON_SCREEN, {
person,
organization,
});
testResult(MEMBER_PERSON_SCREEN, person, organization);
});
});
});
Expand Down
55 changes: 53 additions & 2 deletions __tests__/actions/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
completeStepReminder,
deleteStepWithTracking,
} from '../../src/actions/steps';
import { reloadGroupCelebrateFeed } from '../../src/actions/celebration';
import { refreshImpact } from '../../src/actions/impact';
import * as analytics from '../../src/actions/analytics';
import { mockFnWithParams } from '../../testUtils';
Expand All @@ -39,9 +40,10 @@ common.formatApiDate = jest.fn().mockReturnValue(mockDate);

jest.mock('../../src/actions/api');
jest.mock('../../src/actions/impact');
jest.mock('../../src/actions/celebration');

beforeEach(() => {
callApi.mockClear();
jest.clearAllMocks();
store = mockStore();
});

Expand Down Expand Up @@ -207,9 +209,10 @@ describe('addSteps', () => {

describe('complete challenge', () => {
const stepId = 34556;
const stepOrgId = '555';
const step = {
id: stepId,
organization: { id: '555' },
organization: { id: stepOrgId },
receiver: { id: receiverId },
};

Expand All @@ -232,6 +235,7 @@ describe('complete challenge', () => {
const trackActionResult = { type: 'tracked action' };

const impactResponse = { type: 'test impact' };
const celebrateResponse = { type: 'test celebrate' };

const screen = 'contact steps';

Expand Down Expand Up @@ -266,6 +270,7 @@ describe('complete challenge', () => {

callApi.mockReturnValue(() => Promise.resolve({ type: 'test api' }));
refreshImpact.mockReturnValue(impactResponse);
reloadGroupCelebrateFeed.mockReturnValue(celebrateResponse);
});

it('completes step', async () => {
Expand All @@ -279,6 +284,52 @@ describe('complete challenge', () => {
challengeCompleteQuery,
data,
);
expect(callApi).toHaveBeenCalledWith(
REQUESTS.CHALLENGE_COMPLETE,
challengeCompleteQuery,
data,
);
expect(reloadGroupCelebrateFeed).toHaveBeenCalledWith(stepOrgId);
expect(store.getActions()).toEqual([
{ type: COMPLETED_STEP_COUNT, userId: receiverId },
impactResponse,
{
type: NAVIGATE_FORWARD,
routeName: ADD_STEP_SCREEN,
params: {
type: STEP_NOTE,
onComplete: expect.anything(),
trackingObj: buildTrackingObj(
'people : person : steps : complete comment',
'people',
'person',
'steps',
),
},
},
trackActionResult,
celebrateResponse,
]);
});

it('completes step for personal ministry', async () => {
const noOrgStep = { ...step, organization: null };
await store.dispatch(completeStep(noOrgStep, screen));
expect(callApi).toHaveBeenCalledWith(
REQUESTS.GET_MY_CHALLENGES,
stepsQuery,
);
expect(callApi).toHaveBeenCalledWith(
REQUESTS.CHALLENGE_COMPLETE,
challengeCompleteQuery,
data,
);
expect(callApi).toHaveBeenCalledWith(
REQUESTS.CHALLENGE_COMPLETE,
challengeCompleteQuery,
data,
);
expect(reloadGroupCelebrateFeed).not.toHaveBeenCalled();
expect(store.getActions()).toEqual([
{ type: COMPLETED_STEP_COUNT, userId: receiverId },
impactResponse,
Expand Down
10 changes: 10 additions & 0 deletions __tests__/containers/ContactJourney.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ describe('ContactJourney', () => {

expect(instance.state.isPersonalMinistry).toEqual(false);
});

it('loads with user_created true', () => {
const userCreatedOrg = { ...org, user_created: true };
store = createMockStore(personId, { [personId]: mockJourneyList });
const instance = createComponent({
organization: userCreatedOrg,
}).instance();

expect(instance.state.isPersonalMinistry).toEqual(false);
});
});

describe('journey methods', () => {
Expand Down
45 changes: 38 additions & 7 deletions __tests__/containers/Groups/GroupsListScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { navigatePush } from '../../../src/actions/navigation';
import { getMyCommunities } from '../../../src/actions/organizations';
import { communitiesSelector } from '../../../src/selectors/organizations';
import * as common from '../../../src/utils/common';
import {
GROUP_SCREEN,
USER_CREATED_GROUP_SCREEN,
} from '../../../src/containers/Groups/GroupScreen';

jest.mock('../../../src/selectors/organizations');
jest.mock('../../../src/actions/navigation', () => ({
Expand All @@ -26,6 +30,7 @@ const organizations = {
{
id: '2',
name: 'Test Org 2',
user_created: true,
},
],
};
Expand All @@ -52,13 +57,39 @@ describe('GroupsListScreen', () => {
expect(component).toMatchSnapshot();
});

it('should handlePress correctly', () => {
const instance = component.instance();
instance.handlePress();
expect(navigatePush).toHaveBeenCalled();
expect(communitiesSelector).toHaveBeenCalledWith({
organizations,
auth,
describe('handlePress', () => {
it('navigates to groups screen', () => {
const organization = organizations.all[0];
const item = component
.childAt(1)
.childAt(0)
.props()
.renderItem({ item: organization });
item.props.onPress(organization);

expect(navigatePush).toHaveBeenCalledWith(GROUP_SCREEN, { organization });
expect(communitiesSelector).toHaveBeenCalledWith({
organizations,
auth,
});
});

it('navigates to user created org screen', () => {
const organization = organizations.all[1];
const item = component
.childAt(1)
.childAt(0)
.props()
.renderItem({ item: organization });
item.props.onPress(organization);

expect(navigatePush).toHaveBeenCalledWith(USER_CREATED_GROUP_SCREEN, {
organization,
});
expect(communitiesSelector).toHaveBeenCalledWith({
organizations,
auth,
});
});
});

Expand Down

0 comments on commit 3c9f0d9

Please sign in to comment.