Skip to content

Commit

Permalink
Added tests for updateUnprocessedInputs
Browse files Browse the repository at this point in the history
- Added tests that utilize labelsPRomises
- Cleaned up mocks, diaryTypes.
  • Loading branch information
the-bay-kay committed Nov 30, 2023
1 parent 07c5f80 commit 0d1717d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 66 deletions.
79 changes: 70 additions & 9 deletions www/__mocks__/timelineHelperMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ const mockConfirmedPlaceData: ConfirmedPlace = {
enter_ts: 1437578093.881,
exit_ts: 1437578093.881,
};

// using parse/stringify to deep copy & populate data
let tempMetaData = JSON.parse(JSON.stringify(mockMetaData));
tempMetaData.write_ts = 2;
tempMetaData.origin_key = '2';

export const mockMetaDataTwo = tempMetaData;

export const mockUnprocessedTrip: UnprocessedTrip = {
Expand All @@ -87,7 +85,7 @@ export const mockUnprocessedTrip: UnprocessedTrip = {
user_input: null,
};

export const mockData: ServerResponse<CompositeTrip> = {
export const mockCompData: ServerResponse<CompositeTrip> = {
phone_data: [
{
data: {
Expand Down Expand Up @@ -165,18 +163,16 @@ export const mockData: ServerResponse<CompositeTrip> = {
},
],
};

// Setup for second mockData
let newPhoneData = JSON.parse(JSON.stringify(mockData.phone_data[0]));
let newPhoneData = JSON.parse(JSON.stringify(mockCompData.phone_data[0]));
newPhoneData.data._id.$oid = 'mockDataTwo';
newPhoneData.metadata = mockMetaDataTwo;
newPhoneData.data.start_confirmed_place.metadata = mockMetaDataTwo;
newPhoneData.data.start_confirmed_place._id.$oid = 'startConfirmedPlaceTwo';
newPhoneData.data.end_confirmed_place.metadata = mockMetaDataTwo;
newPhoneData.data.end_confirmed_place._id.$oid = 'endConfirmedPlaceTwo';

export const mockDataTwo = {
phone_data: [mockData.phone_data[0], newPhoneData],
export const mockCompDataTwo = {
phone_data: [mockCompData.phone_data[0], newPhoneData],
};

export const mockTransitions: Array<ServerData<TripTransition>> = [
Expand Down Expand Up @@ -230,7 +226,72 @@ export const mockFilterLocations: Array<ServerData<FilteredLocation>> = [
},
];

// When called by mocks, pair 1 returns 1 value, Pair two 2, pair 3 returns none.
export const mockAppConfigOne = {
survey_info: {
'trip-labels': 'ENKETO',
},
};
export const mockAppConfigTwo = {
survey_info: {
'trip-labels': 'Other',
},
intro: {
mode_studied: 'sample_study',
},
};
export const mockAppConfigThree = {
survey_info: {
'trip-labels': 'Other',
},
intro: {
mode_studied: false,
},
};

export const mockLabelDataPromises = [
Promise.resolve([
// Mode
{
data: {
end_ts: 1681438322.981,
label: 'walk',
start_ts: 1681437527.4971218,
},
metadata: mockMetaData,
},
{
data: {
end_ts: 1681439339.983,
label: 'walk',
start_ts: 1681438918.6598706,
},
metadata: mockMetaDataTwo,
},
]),
Promise.resolve([
// Purpose
{
data: {
end_ts: 1681438322.981,
label: 'test',
start_ts: 1681437527.4971218,
},
metadata: mockMetaData,
},
{
data: {
end_ts: 1681438322.983,
label: 'testValue',
start_ts: 1681438918.6598706,
},
metadata: mockMetaDataTwo,
},
]),
Promise.resolve([]), // Replaced_Mode
];
//let mockLabelDataPromisesTwo = JSON.parse(JSON.stringify(mockLabelDataPromises));

// Used by jest.mocks() to return a various mocked objects.
export const fakeStartTsOne = -14576291;
export const fakeEndTsOne = -13885091;
export const fakeStartTsTwo = 1092844665;
Expand Down
74 changes: 40 additions & 34 deletions www/__tests__/timelineHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {
readAllCompositeTrips,
readUnprocessedTrips,
compositeTrips2TimelineMap,
updateUnprocessedInputs,
keysForLabelInputs,
} from '../js/diary/timelineHelper';
import { mockBEMUserCache } from '../__mocks__/cordovaMocks';
import * as mockTLH from '../__mocks__/timelineHelperMocks';
import { GeoJSONData, GeoJSONStyledFeature } from '../js/types/diaryTypes';
import { unprocessedLabels } from '../js/diary/timelineHelper';
import { GeoJSONData, GeoJSONStyledFeature, UserInputEntry } from '../js/types/diaryTypes';

mockLogger();
mockAlert();
Expand Down Expand Up @@ -40,7 +42,7 @@ describe('useGeojsonForTrip', () => {

it('works without labelMode flag', () => {
const testValue = useGeojsonForTrip(
mockTLH.mockDataTwo.phone_data[1].data,
mockTLH.mockCompDataTwo.phone_data[1].data,
mockTLH.mockLabelOptions,
);
checkGeojson(testValue);
Expand All @@ -49,14 +51,14 @@ describe('useGeojsonForTrip', () => {
});

describe('compositeTrips2TimelineMap', () => {
const tripListOne = [mockTLH.mockData.phone_data[0].data];
const tripListOne = [mockTLH.mockCompData.phone_data[0].data];
const tripListTwo = [
mockTLH.mockDataTwo.phone_data[0].data,
mockTLH.mockDataTwo.phone_data[1].data,
mockTLH.mockCompDataTwo.phone_data[0].data,
mockTLH.mockCompDataTwo.phone_data[1].data,
];
const keyOne = mockTLH.mockData.phone_data[0].data._id.$oid;
const keyTwo = mockTLH.mockDataTwo.phone_data[1].data._id.$oid;
const keyThree = mockTLH.mockData.phone_data[0].data._id.$oid;
const keyOne = mockTLH.mockCompData.phone_data[0].data._id.$oid;
const keyTwo = mockTLH.mockCompDataTwo.phone_data[1].data._id.$oid;
const keyThree = mockTLH.mockCompData.phone_data[0].data._id.$oid;
let testValue;

it('Works with an empty list', () => {
Expand Down Expand Up @@ -90,38 +92,42 @@ describe('compositeTrips2TimelineMap', () => {
});
});

// Tests for updateLocalUnprocessedInputs & keysForLabelInputs
describe('The updateUnprocessedInput functions can ', () => {
const mockAppConfigOne = {
survey_info: {
'trip-labels': 'ENKETO',
},
};
const mockAppConfigTwo = {
survey_info: {
'trip-labels': 'Other',
},
intro: {
mode_studied: 'sample',
},
};
// keysForLabelInputs tests
it('use an appConfig to get labelInputKeys', () => {
expect(keysForLabelInputs(mockAppConfigOne)).rejects;
expect(keysForLabelInputs(mockAppConfigOne)).toEqual(['manual/trip_user_input']);
expect(keysForLabelInputs(mockAppConfigTwo).length).toEqual(3);
});
it('update the unprocessed labels', () => {
// TODO
});
it('use an appConfig to get labelInputKeys', () => {
expect(keysForLabelInputs(mockTLH.mockAppConfigOne)).toEqual(['manual/trip_user_input']);
expect(keysForLabelInputs(mockTLH.mockAppConfigTwo).length).toEqual(3);
});

// updateUnprocessedInputs Tests
jest.mock('../js/survey/multilabel/confirmHelper', () => ({
...jest.requireActual('../js/survey/multilabel/confirmHelper'),
getLabelInputs: jest.fn(() => ['MODE', 'PURPOSE', 'REPLACED_MODE']),
}));

it('processed empty labels', async () => {
await updateUnprocessedInputs([], [], mockTLH.mockAppConfigThree);
expect(unprocessedLabels).toEqual({});
});

it('updates unprocessed labels', async () => {
await updateUnprocessedInputs(mockTLH.mockLabelDataPromises, [], mockTLH.mockAppConfigThree);
expect(unprocessedLabels).toEqual(
expect.objectContaining({
MODE: expect.any(Array<UserInputEntry>),
PURPOSE: expect.any(Array<UserInputEntry>),
REPLACED_MODE: expect.any(Array<UserInputEntry>),
}),
);
expect(unprocessedLabels.MODE.length).toEqual(2);
expect(unprocessedLabels.PURPOSE.length).toEqual(2);
expect(unprocessedLabels.REPLACED_MODE.length).toEqual(0);
});

// Tests for readAllCompositeTrips
// Once we have end-to-end testing, we could utilize getRawEnteries.
jest.mock('../js/services/commHelper', () => ({
getRawEntries: jest.fn((key, startTs, endTs, valTwo) => {
if (startTs === mockTLH.fakeStartTsOne) return mockTLH.mockData;
if (startTs == mockTLH.fakeStartTsTwo) return mockTLH.mockDataTwo;
if (startTs === mockTLH.fakeStartTsOne) return mockTLH.mockCompData;
if (startTs == mockTLH.fakeStartTsTwo) return mockTLH.mockCompDataTwo;
return {};
}),
}));
Expand Down
10 changes: 6 additions & 4 deletions www/js/diary/timelineHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ const getUnprocessedInputQuery = (pipelineRange) => ({
endTs: DateTime.now().toUnixInteger() + 10,
});

function updateUnprocessedInputs(labelsPromises, notesPromises, appConfig) {
Promise.all([...labelsPromises, ...notesPromises]).then((comboResults) => {
/**
* updateUnprocessedInputs is a helper function for updateLocalUnprocessedInputs
* and updateAllUnprocessedInputs, exported for unit testing.
*/
export function updateUnprocessedInputs(labelsPromises, notesPromises, appConfig) {
return Promise.all([...labelsPromises, ...notesPromises]).then((comboResults) => {
const labelResults = comboResults.slice(0, labelsPromises.length);
const notesResults = comboResults.slice(labelsPromises.length).flat(2);
// fill in the unprocessedLabels object with the labels we just read
Expand All @@ -121,7 +125,6 @@ function updateUnprocessedInputs(labelsPromises, notesPromises, appConfig) {
* @param pipelineRange an object with start_ts and end_ts representing the range of time
* for which travel data has been processed through the pipeline on the server
* @param appConfig the app configuration
* @returns Promise an array with 1) results for labels and 2) results for notes
*/
export async function updateLocalUnprocessedInputs(pipelineRange, appConfig) {
const BEMUserCache = window['cordova'].plugins.BEMUserCache;
Expand All @@ -141,7 +144,6 @@ export async function updateLocalUnprocessedInputs(pipelineRange, appConfig) {
* @param pipelineRange an object with start_ts and end_ts representing the range of time
* for which travel data has been processed through the pipeline on the server
* @param appConfig the app configuration
* @returns Promise an array with 1) results for labels and 2) results for notes
*/
export async function updateAllUnprocessedInputs(pipelineRange, appConfig) {
const tq = getUnprocessedInputQuery(pipelineRange);
Expand Down
29 changes: 10 additions & 19 deletions www/js/types/diaryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,27 +135,18 @@ export type SectionSummary = {
duration: { [k: MotionTypeKey | BaseModeKey]: number };
};

export type UserInputEntry = {
data: {
end_ts: number;
start_ts: number;
label: string;
start_local_dt?: LocalDt;
end_local_dt?: LocalDt;
status?: string;
match_id?: string;
};
metadata: {
time_zone: string;
plugin: string;
write_ts: number;
platform: string;
read_ts: number;
key: string;
};
key?: string;
type UserInputData = {
end_ts: number;
start_ts: number;
label: string;
start_local_dt?: LocalDt;
end_local_dt?: LocalDt;
status?: string;
match_id?: string;
};

export type UserInputEntry = ServerData<UserInputData>;

export type Location = {
speed: number;
heading: number;
Expand Down

0 comments on commit 0d1717d

Please sign in to comment.