From f031d331fb27fc60cfe6e12c735b54aa44653b32 Mon Sep 17 00:00:00 2001 From: Katie Rischpater <98350084+the-bay-kay@users.noreply.github.com> Date: Thu, 9 Nov 2023 11:40:52 -0800 Subject: [PATCH] Moved mockData to separate file for legibility --- www/__mocks__/timelineHelperMocks.ts | 111 ++++++++++++++++++++++ www/__tests__/timelineHelper.test.ts | 136 ++++----------------------- 2 files changed, 128 insertions(+), 119 deletions(-) create mode 100644 www/__mocks__/timelineHelperMocks.ts diff --git a/www/__mocks__/timelineHelperMocks.ts b/www/__mocks__/timelineHelperMocks.ts new file mode 100644 index 000000000..0b951174d --- /dev/null +++ b/www/__mocks__/timelineHelperMocks.ts @@ -0,0 +1,111 @@ +import { MetaData, ServerData, ServerResponse } from '../js/types/serverData'; +import { CompositeTrip, TripTransition } from '../js/types/diaryTypes'; + +const mockMetaData: MetaData = { + write_ts: -13885091, + key: 'test/value', + platform: 'test', + time_zone: 'America/Los_Angeles', + write_fmt_time: '1969-07-16T07:01:49.000Z', + write_local_dt: null, + origin_key: '12345', +}; + +export const mockData: ServerResponse = { + phone_data: [ + { + data: { + _id: null, + additions: [], + cleaned_section_summary: null, + cleaned_trip: null, + confidence_threshold: -1, + confirmed_trip: null, + distance: 777, + duration: 777, + end_confirmed_place: { + data: null, + metadata: JSON.parse(JSON.stringify(mockMetaData)), + }, + end_fmt_time: '2023-11-01T17:55:20.999397-07:00', + end_loc: { + type: 'Point', + coordinates: [-1, -1], + }, + end_local_dt: null, + end_place: null, + end_ts: -1, + expectation: null, + expected_trip: null, + inferred_labels: [], + inferred_section_summary: { + count: { + CAR: 1, + WALKING: 1, + }, + distance: { + CAR: 222, + WALKING: 222, + }, + duration: { + CAR: 333, + WALKING: 333, + }, + }, + inferred_trip: null, + key: '12345', + locations: [ + { + metadata: JSON.parse(JSON.stringify(mockMetaData)), + data: null, + }, + ], + origin_key: '', + raw_trip: null, + sections: [ + { + metadata: JSON.parse(JSON.stringify(mockMetaData)), + data: null, + }, + ], + source: 'DwellSegmentationDistFilter', + start_confirmed_place: { + data: null, + metadata: JSON.parse(JSON.stringify(mockMetaData)), + }, + start_fmt_time: '2023-11-01T17:55:20.999397-07:00', + start_loc: { + type: 'Point', + coordinates: [-1, -1], + }, + start_local_dt: null, + start_place: null, + start_ts: null, + user_input: null, + }, + metadata: JSON.parse(JSON.stringify(mockMetaData)), + }, + ], +}; +export const mockDataTwo = { + phone_data: [mockData.phone_data[0], mockData.phone_data[0]], +}; + +export const mockTransition: Array> = [ + { + data: { + currstate: 'STATE_WAITING_FOR_TRIP_TO_START', + transition: 'T_NOP', + ts: 12345.6789, + }, + metadata: mockMetaData, + }, +]; + +export const mockTransitionTwo = mockTransition.push(mockTransition[0]); + +// When called by mocks, pair 1 returns 1 value, Pair two 2, pair 3 returns none. +export const fakeStartTsOne = -14576291; +export const fakeEndTsOne = -13885091; +export const fakeStartTsTwo = 1092844665; +export const fakeEndTsTwo = 1277049465; diff --git a/www/__tests__/timelineHelper.test.ts b/www/__tests__/timelineHelper.test.ts index 0ae65d34c..0faac2c2e 100644 --- a/www/__tests__/timelineHelper.test.ts +++ b/www/__tests__/timelineHelper.test.ts @@ -2,8 +2,7 @@ import { mockLogger } from '../__mocks__/globalMocks'; import { readAllCompositeTrips, readUnprocessedTrips } from '../js/diary/timelineHelper'; import { mockBEMUserCache } from '../__mocks__/cordovaMocks'; -import { MetaData, ServerData, ServerResponse } from '../js/types/serverData'; -import { CompositeTrip, TripTransition } from '../js/types/diaryTypes'; +import * as mockTLH from '../__mocks__/timelineHelperMocks'; mockLogger(); mockBEMUserCache(); @@ -12,120 +11,11 @@ afterAll(() => { jest.restoreAllMocks(); }); -const mockMetaData: MetaData = { - write_ts: -13885091, - key: 'test/value', - platform: 'test', - time_zone: 'America/Los_Angeles', - write_fmt_time: '1969-07-16T07:01:49.000Z', - write_local_dt: null, - origin_key: '12345', -}; - -const mockData: ServerResponse = { - phone_data: [ - { - data: { - _id: null, - additions: [], - cleaned_section_summary: null, // TODO - cleaned_trip: null, //ObjId; - confidence_threshold: -1, - confirmed_trip: null, //ObjId; - distance: 777, - duration: 777, - end_confirmed_place: { - data: null, - metadata: JSON.parse(JSON.stringify(mockMetaData)), - }, - end_fmt_time: '2023-11-01T17:55:20.999397-07:00', - end_loc: { - type: 'Point', - coordinates: [-1, -1], - }, - end_local_dt: null, //LocalDt; - end_place: null, //ObjId; - end_ts: -1, - expectation: null, // TODO "{to_label: boolean}" - expected_trip: null, //ObjId; - inferred_labels: [], // TODO - inferred_section_summary: { - count: { - CAR: 1, - WALKING: 1, - }, - distance: { - CAR: 222, - WALKING: 222, - }, - duration: { - CAR: 333, - WALKING: 333, - }, - }, - inferred_trip: null, - key: '12345', - locations: [ - { - metadata: JSON.parse(JSON.stringify(mockMetaData)), - data: null, - }, - ], // LocationType - origin_key: '', - raw_trip: null, - sections: [ - { - metadata: JSON.parse(JSON.stringify(mockMetaData)), - data: null, - }, - ], // TODO - source: 'DwellSegmentationDistFilter', - start_confirmed_place: { - data: null, - metadata: JSON.parse(JSON.stringify(mockMetaData)), - }, - start_fmt_time: '2023-11-01T17:55:20.999397-07:00', - start_loc: { - type: 'Point', - coordinates: [-1, -1], - }, - start_local_dt: null, - start_place: null, - start_ts: null, - user_input: null, - }, - metadata: JSON.parse(JSON.stringify(mockMetaData)), - }, - ], -}; - -let mockDataTwo = mockData; -mockDataTwo.phone_data = [mockData.phone_data[0], mockData.phone_data[0]]; - -const mockTransition: Array> = [ - { - data: { - currstate: 'STATE_WAITING_FOR_TRIP_TO_START', - transition: 'T_NOP', - ts: 12345.6789, - }, - metadata: mockMetaData, - }, -]; - -const mockTransitionTwo = mockTransition.push(mockTransition[0]); - -// When called by mocks, pair 1 returns 1 value, Pair two 2, pair 3 returns none. -const fakeStartTsOne = -14576291; -const fakeEndTsOne = -13885091; -const fakeStartTsTwo = 1092844665; -const fakeEndTsTwo = 1277049465; - // Once we have end-to-end testing, we could utilize getRawEnteries. jest.mock('../js/commHelper', () => ({ getRawEntries: jest.fn((key, startTs, endTs, valTwo) => { - if (startTs === fakeStartTsOne) return mockData; - if (startTs == fakeStartTsTwo) return mockDataTwo; + if (startTs === mockTLH.fakeStartTsOne) return mockTLH.mockData; + if (startTs == mockTLH.fakeStartTsTwo) return mockTLH.mockDataTwo; return {}; }), })); @@ -135,14 +25,18 @@ it('works when there are no composite trip objects fetched', async () => { }); it('fetches a composite trip object and collapses it', async () => { - expect(readAllCompositeTrips(fakeStartTsOne, fakeEndTsOne)).resolves.not.toThrow(); - expect(readAllCompositeTrips(fakeStartTsTwo, fakeEndTsTwo)).resolves.not.toThrow(); + expect( + readAllCompositeTrips(mockTLH.fakeStartTsOne, mockTLH.fakeEndTsOne), + ).resolves.not.toThrow(); + expect( + readAllCompositeTrips(mockTLH.fakeStartTsTwo, mockTLH.fakeEndTsTwo), + ).resolves.not.toThrow(); }); jest.mock('../js/unifiedDataLoader', () => ({ getUnifiedDataForInterval: jest.fn((key, tq, combiner) => { - if (tq.startTs === fakeStartTsOne) return Promise.resolve(mockTransition); - if (tq.startTs === fakeStartTsTwo) return Promise.resolve(mockTransitionTwo); + if (tq.startTs === mockTLH.fakeStartTsOne) return Promise.resolve(mockTLH.mockTransition); + if (tq.startTs === mockTLH.fakeStartTsTwo) return Promise.resolve(mockTLH.mockTransitionTwo); return Promise.resolve([]); }), })); @@ -152,6 +46,10 @@ it('works when there are no unprocessed trips...', async () => { }); it('works when there are one or more unprocessed trips...', async () => { - expect(readUnprocessedTrips(fakeStartTsOne, fakeEndTsOne, null)).resolves.not.toThrow(); - expect(readUnprocessedTrips(fakeStartTsTwo, fakeEndTsTwo, null)).resolves.not.toThrow(); + expect( + readUnprocessedTrips(mockTLH.fakeStartTsOne, mockTLH.fakeEndTsOne, null), + ).resolves.not.toThrow(); + expect( + readUnprocessedTrips(mockTLH.fakeStartTsTwo, mockTLH.fakeEndTsTwo, null), + ).resolves.not.toThrow(); });