Skip to content

Commit

Permalink
feat: add integration tests for survey and supplementary forms!
Browse files Browse the repository at this point in the history
  • Loading branch information
hopetambala committed Oct 2, 2022
1 parent 696dfeb commit ed57ad9
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 39 deletions.
22 changes: 16 additions & 6 deletions modules/offline/__test__/post.integrate.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import hooks from '../../../test/hooks';
import { postIdentificationForm, postSupplementaryForm } from '../../cached-resources';
import checkOnlineStatus from '..';
import { postOfflineForms } from '../post';
import { createOfflineUserMockData, createResidentMockData, createSupplementaryFormMockData } from './utils';
import { hooks } from '../../../test/hooks'

hooks()
hooks();

jest.mock('..', () => jest.fn());

Expand All @@ -25,8 +25,17 @@ describe('Testing full feature of offline posting', () => {
const resident2 = await postIdentificationForm(residents[1]);
await postIdentificationForm(residents[2]); // Resident 3 won't have supplementary forms

const supplementaryForms1 = createSupplementaryFormMockData(numberofSupplementaryFormsCollected / 2, resident1.objectId, user.objectId);
const supplementaryForms2 = createSupplementaryFormMockData(numberofSupplementaryFormsCollected / 2, resident2.objectId, user.objectId);
const supplementaryForms1 = createSupplementaryFormMockData(
numberofSupplementaryFormsCollected / 2,
resident1.objectId,
user.objectId
);
const supplementaryForms2 = createSupplementaryFormMockData(
numberofSupplementaryFormsCollected / 2,
resident2.objectId,
user.objectId
);

await supplementaryForms1.reduce(
(p, form) => p.then(() => postSupplementaryForm(form)), // https://jrsinclair.com/articles/2019/how-to-run-async-js-in-parallel-or-sequential/
Promise.resolve(null)
Expand All @@ -38,7 +47,8 @@ describe('Testing full feature of offline posting', () => {

checkOnlineStatus.mockResolvedValue(true);

const { uploadedForms } = await postOfflineForms();
console.log(uploadedForms);
const { uploadedForms, offlineForms } = await postOfflineForms();
expect(uploadedForms[0].length).toEqual(offlineForms.residentForms.length);
expect(uploadedForms[1].length).toEqual(offlineForms.residentSupplementaryForms.length);
});
});
16 changes: 13 additions & 3 deletions modules/offline/__test__/post.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ describe('Testing full feature of offline posting', () => {
const resident2 = await postIdentificationForm(residents[1]);
await postIdentificationForm(residents[2]); // Resident 3 won't have supplementary forms

const supplementaryForms1 = createSupplementaryFormMockData(numberofSupplementaryFormsCollected / 2, resident1.objectId, user.objectId);
const supplementaryForms2 = createSupplementaryFormMockData(numberofSupplementaryFormsCollected / 2, resident2.objectId, user.objectId);
const supplementaryForms1 = createSupplementaryFormMockData(
numberofSupplementaryFormsCollected / 2,
resident1.objectId,
user.objectId
);
const supplementaryForms2 = createSupplementaryFormMockData(
numberofSupplementaryFormsCollected / 2,
resident2.objectId,
user.objectId
);

await supplementaryForms1.reduce(
(p, form) => p.then(() => postSupplementaryForm(form)), // https://jrsinclair.com/articles/2019/how-to-run-async-js-in-parallel-or-sequential/
Promise.resolve(null)
Expand All @@ -36,6 +45,7 @@ describe('Testing full feature of offline posting', () => {

const { offlineForms } = await postOfflineForms();

expect(numberOfResidents + numberofSupplementaryFormsCollected).toEqual(offlineForms.surveyData.length + offlineForms.supForms.length);
expect(numberOfResidents + numberofSupplementaryFormsCollected)
.toEqual(offlineForms.residentForms.length + offlineForms.residentSupplementaryForms.length);
});
});
19 changes: 16 additions & 3 deletions modules/offline/__test__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ const createResidentMockData = (numberOfRecords, userObjectId) => {
return residentArray;
};

const createSupplementaryFormMockData = (numberOfRecords, surveyeeObjectedId, userObjectId) => {
const createSupplementaryFormMockData = (
numberOfRecords,
surveyeeObjectedId,
userObjectId,
numberOfResponses = 5
) => {
const supplementaryFormArray = [];

const customFormSpecifications = {
Expand All @@ -48,11 +53,19 @@ const createSupplementaryFormMockData = (numberOfRecords, surveyeeObjectedId, us
description: faker.commerce.productDescription()
};

const fakeTitles = ['waterSourceDrinking', 'waterSourceOther', 'problemsWater', 'receiveWater', 'timeToGetWater', 'getsWaterHousehold'];
const fakeAnswers = ['aqueduct', 'lessThan1', ['none'], ['adultMale'], 'moreThan30', 'no', '1,000', 'yes'];

const fields = Array(numberOfResponses).fill(0).map(() => ({
title: faker.helpers.arrayElement(fakeTitles),
answer: faker.helpers.arrayElement(fakeAnswers)
}));

const localObject = {
title: customFormSpecifications.name,
description: customFormSpecifications.description,
formSpecificationsId: customFormSpecifications.objectId,
fields: [],
fields,
surveyingUser: 'JJ McCarthy',
surveyingOrganization: 'Michigan'
};
Expand All @@ -62,7 +75,7 @@ const createSupplementaryFormMockData = (numberOfRecords, surveyeeObjectedId, us
parseParentClassID: surveyeeObjectedId,
parseParentClass: 'SurveyData',
parseUser: userObjectId,
parseClass: 'config.class',
parseClass: 'FormResults',
photoFile: faker.image.imageUrl(),
localObject,
};
Expand Down
23 changes: 12 additions & 11 deletions modules/offline/post/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,25 @@ const postOfflineForms = async () => {
const householdRelationsAsync = await getData('offlineHouseholdsRelation');

const offlineForms = {
surveyData: idFormsAsync,
supForms: supplementaryFormsAsync,
residentForms: idFormsAsync,
residentSupplementaryForms: supplementaryFormsAsync,
households: householdsAsync,
householdRelations: householdRelationsAsync,
assetIdForms: assetIdFormsAsync,
assetSupForms: assetSupFormsAsync,
surveyingUser: surveyUser,
surveyingOrganization: organization,
parseUser: user.objectId,
appVersion,
phoneOS
assetForms: assetIdFormsAsync,
assetSupplementaryForms: assetSupFormsAsync,
metadata: {
surveyingUser: surveyUser,
surveyingOrganization: organization,
parseUser: user.objectId,
appVersion,
phoneOS
}
};

const isConnected = await checkOnlineStatus();

if (isConnected) {
const uploadedForms = await uploadOfflineForms(offlineForms);
await cleanupPostedOfflineForms(); // think about retrying failed forms
return {
offlineForms,
uploadedForms
Expand All @@ -58,4 +59,4 @@ const postOfflineForms = async () => {
return 'No Internet Access';
};

export { postOfflineForms };
export { cleanupPostedOfflineForms, postOfflineForms };
9 changes: 5 additions & 4 deletions services/parse/auth/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

import client from '../client';
import selectedENV from '../../../environment';
import { getData } from '../../../modules/async-storage';
import client from '../client';
import notificationTypeRestParams from './_signupHelper';

const { parseAppId, parseJavascriptKey, parseServerUrl, TEST_MODE } = selectedENV;
const Parse = client(TEST_MODE)
const {
parseAppId, parseJavascriptKey, parseServerUrl, TEST_MODE
} = selectedENV;
const Parse = client(TEST_MODE);

function initialize() {
Parse.initialize(parseAppId, parseJavascriptKey);
Expand Down
16 changes: 9 additions & 7 deletions services/parse/client/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Parse as ParseRN } from 'parse/react-native';
import { Parse as ParseNode } from 'parse/node';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Parse as ParseNode } from 'parse/node';
import { Parse as ParseRN } from 'parse/react-native';

const client = (isTest) => {
if (isTest) return ParseNode;
ParseRN.setAsyncStorage(AsyncStorage);
return ParseRN;
};

export default client = (isTest) => {
if (isTest) return ParseNode
ParseRN.setAsyncStorage(AsyncStorage);
return ParseRN
}
export default client;
2 changes: 1 addition & 1 deletion services/parse/crud/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import client from '../client';
import { customMultiParamQueryService, customMultiValueArrayService, customQueryService } from './custom-queries';

const { TEST_MODE } = selectedENV;
const Parse = client(TEST_MODE)
const Parse = client(TEST_MODE);

function retrieveHelloFunction() {
Parse.Cloud.run('hello').then((result) => result);
Expand Down
9 changes: 5 additions & 4 deletions test/hooks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { initialize } from "../services/parse/auth";
import { initialize } from '../services/parse/auth';

export function hooks() {
beforeEach(async () => initialize())
}
// https://medium.com/@phatdev/testing-everything-against-the-real-database-in-nodejs-typescript-application-by-integrating-unit-31b12866d538
export default function hooks() {
beforeEach(async () => initialize());
}

0 comments on commit ed57ad9

Please sign in to comment.