Skip to content

Commit

Permalink
Rewrote first tests, mocks, ran prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
the-bay-kay committed Nov 15, 2023
1 parent a2426c4 commit 08c10f2
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 95 deletions.
136 changes: 58 additions & 78 deletions www/__mocks__/timelineHelperMocks.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,59 @@
import { MetaData, ServerData, ServerResponse } from '../js/types/serverData';
import { CompositeTrip, TripTransition } from '../js/types/diaryTypes';
import { CompositeTrip, ConfirmedPlace, TripTransition } from '../js/types/diaryTypes';

const mockMetaData: MetaData = {
write_ts: -13885091,
key: 'test/value',
write_ts: 1,
key: 'test/value/one',
platform: 'test',
time_zone: 'America/Los_Angeles',
write_fmt_time: '1969-07-16T07:01:49.000Z',
write_local_dt: null,
origin_key: '12345',
origin_key: '1',
};

const mockObjId = {
$oid: 'objID',
};

const mockConfirmedPlaceData: ConfirmedPlace = {
source: 'DwellSegmentationTimeFilter',
location: {
type: 'Point',
coordinates: [-122.0876886, 37.3887767],
},
cleaned_place: {
$oid: '6553c3a0f27f16fbf9d1def1',
},
additions: [],
user_input: {},
enter_fmt_time: '2015-07-22T08:14:53.881000-07:00',
exit_fmt_time: '2015-07-22T08:14:53.881000-07:00',
starting_trip: {
$oid: '6553c3a1f27f16fbf9d1df15',
},
ending_trip: {
$oid: '6553c3a1f27f16fbf9d1df15',
},
enter_local_dt: null,
exit_local_dt: null,
raw_places: [
{
$oid: '6553c39df27f16fbf9d1dcef',
},
{
$oid: '6553c39df27f16fbf9d1dcef',
},
],
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 mockData: ServerResponse<CompositeTrip> = {
phone_data: [
{
Expand All @@ -24,8 +67,9 @@ export const mockData: ServerResponse<CompositeTrip> = {
distance: 777,
duration: 777,
end_confirmed_place: {
data: null,
data: mockConfirmedPlaceData,
metadata: mockMetaData,
_id: { $oid: 'endConfirmedPlace' },
},
end_fmt_time: '2023-11-01T17:55:20.999397-07:00',
end_loc: {
Expand Down Expand Up @@ -70,8 +114,9 @@ export const mockData: ServerResponse<CompositeTrip> = {
],
source: 'DwellSegmentationDistFilter',
start_confirmed_place: {
data: null,
data: mockConfirmedPlaceData,
metadata: mockMetaData,
_id: { $oid: 'startConfirmedPlace' },
},
start_fmt_time: '2023-11-01T17:55:20.999397-07:00',
start_loc: {
Expand All @@ -87,8 +132,14 @@ export const mockData: ServerResponse<CompositeTrip> = {
},
],
};

let newPhoneData = JSON.parse(JSON.stringify(mockData.phone_data[0]));
newPhoneData.metadata = mockMetaDataTwo;
newPhoneData.data.start_confirmed_place.metadata = mockMetaDataTwo;
newPhoneData.data.end_confirmed_place.metadata = mockMetaDataTwo;

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

export const mockTransition: Array<ServerData<TripTransition>> = [
Expand All @@ -109,74 +160,3 @@ export const fakeStartTsOne = -14576291;
export const fakeEndTsOne = -13885091;
export const fakeStartTsTwo = 1092844665;
export const fakeEndTsTwo = 1277049465;

export const readAllCheckOne = [
{
additions: [],
cleaned_section_summary: null,
cleaned_trip: null,
confidence_threshold: -1,
confirmed_trip: null,
distance: 777,
duration: 777,
end_confirmed_place: {
key: 'test/value',
origin_key: '12345',
},
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: 'test/value',
locations: [
{
key: 'test/value',
origin_key: '12345',
},
],
origin_key: '12345',
raw_trip: null,
sections: [
{
key: 'test/value',
origin_key: '12345',
},
],
source: 'DwellSegmentationDistFilter',
start_confirmed_place: {
key: 'test/value',
origin_key: '12345',
},
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,
},
];
40 changes: 33 additions & 7 deletions www/__tests__/timelineHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ afterAll(() => {
});

// Once we have end-to-end testing, we could utilize getRawEnteries.
jest.mock('../js/commHelper', () => ({
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;
Expand All @@ -28,15 +28,40 @@ it('works when there are no composite trip objects fetched', async () => {
expect(readAllCompositeTrips(-1, -1)).resolves.toEqual([]);
});

it('fetches a composite trip object and collapses it', async () => {
expect(readAllCompositeTrips(mockTLH.fakeStartTsOne, mockTLH.fakeEndTsOne)).resolves.toEqual(
mockTLH.readAllCheckOne,
// Checks that `readAllCOmpositeTrips` properly unpacks & flattens the confirmedPlaces
const checkTripIsUnpacked = (obj) => {
expect(obj.metadata).toBeUndefined();
expect(obj).toEqual(
expect.objectContaining({
key: expect.any(String),
origin_key: expect.any(String),
start_confirmed_place: expect.objectContaining({
origin_key: expect.any(String),
}),
end_confirmed_place: expect.objectContaining({
origin_key: expect.any(String),
}),
locations: expect.any(Array),
sections: expect.any(Array),
}),
);
expect(
readAllCompositeTrips(mockTLH.fakeStartTsTwo, mockTLH.fakeEndTsTwo),
).resolves.not.toThrow();
};

it('fetches a composite trip object and collapses it', async () => {
const testValue = await readAllCompositeTrips(mockTLH.fakeStartTsOne, mockTLH.fakeEndTsOne);
expect(testValue.length).toEqual(1);
checkTripIsUnpacked(testValue[0]);
});

it('Works with multiple trips', async () => {
const testValue = await readAllCompositeTrips(mockTLH.fakeStartTsTwo, mockTLH.fakeEndTsTwo);
expect(testValue.length).toEqual(2);
checkTripIsUnpacked(testValue[0]);
checkTripIsUnpacked(testValue[1]);
expect(testValue[0].origin_key).toBe('1');
expect(testValue[1].origin_key).toBe('2');
});
/*
jest.mock('../js/services/unifiedDataLoader', () => ({
getUnifiedDataForInterval: jest.fn((key, tq, combiner) => {
if (tq.startTs === mockTLH.fakeStartTsOne) return Promise.resolve(mockTLH.mockTransition);
Expand All @@ -57,3 +82,4 @@ it('works when there are one or more unprocessed trips...', async () => {
readUnprocessedTrips(mockTLH.fakeStartTsTwo, mockTLH.fakeEndTsTwo, null),
).resolves.not.toThrow();
});
*/
2 changes: 1 addition & 1 deletion www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
<div id="appRoot" class="fill-container" style="width: 100vw; height: 100vh;"></div>
</body>
<script src="dist/bundle.js"></script>
</html>
</html>
5 changes: 1 addition & 4 deletions www/js/diary.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import angular from 'angular';
import LabelTab from './diary/LabelTab';

angular
.module('emission.main.diary', [
'emission.plugin.logger',
'emission.survey.enketo.answer',
])
.module('emission.main.diary', ['emission.plugin.logger', 'emission.survey.enketo.answer'])

.config(function ($stateProvider) {
$stateProvider.state('root.main.inf_scroll', {
Expand Down
1 change: 0 additions & 1 deletion www/js/diary/timelineHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { getUnifiedDataForInterval } from '../services/unifiedDataLoader';
import { getRawEntries } from '../services/commHelper';
import { ServerResponse, ServerData } from '../types/serverData';
import L from 'leaflet';
import i18next from 'i18next';
import { DateTime } from 'luxon';
import { UserInputEntry, CompositeTrip, TripTransition, SectionData } from '../types/diaryTypes';
import { getLabelInputDetails, getLabelInputs } from '../survey/multilabel/confirmHelper';
Expand Down
9 changes: 5 additions & 4 deletions www/js/types/diaryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ import { BaseModeKey, MotionTypeKey } from '../diary/diaryHelper';
import { ServerData, LocalDt } from './serverData';

type ObjectId = { $oid: string };
type ConfirmedPlace = {
_id: ObjectId;
export type ConfirmedPlace = {
additions: UserInputEntry[];
cleaned_place: ObjectId;
ending_trip: ObjectId;
enter_fmt_time: string; // ISO string 2023-10-31T12:00:00.000-04:00
enter_local_dt: LocalDt;
enter_ts: number; // Unix timestamp
key: string;
location: { type: string; coordinates: number[] };
origin_key: string;
raw_places: ObjectId[];
source: string;
user_input: {
Expand All @@ -27,6 +24,10 @@ type ConfirmedPlace = {
as a string (e.g. 'walk', 'drove_alone') */
[k: `${string}confirm`]: string;
};
exit_fmt_time: string;
exit_ts: number;
exit_local_dt: LocalDt;
starting_trip: ObjectId;
};

export type TripTransition = {
Expand Down

0 comments on commit 08c10f2

Please sign in to comment.