Skip to content

Commit

Permalink
Expanded mocks, sured up tests
Browse files Browse the repository at this point in the history
- borrowed the windowAlert mocks from PR #1093, to allow error checking
- added mockCheck, updated some tests to use to `toEqual()`
  • Loading branch information
the-bay-kay committed Nov 9, 2023
1 parent adab677 commit 2d0d321
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 5 deletions.
16 changes: 16 additions & 0 deletions www/__mocks__/globalMocks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
export const mockLogger = () => {
window['Logger'] = { log: console.log };
};

let alerts = [];

export const mockAlert = () => {
window['alert'] = (message) => {
alerts.push(message);
};
};

export const clearAlerts = () => {
alerts = [];
};

export const getAlerts = () => {
return alerts;
};
77 changes: 77 additions & 0 deletions www/__mocks__/timelineHelperMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,80 @@ export const fakeStartTsOne = -14576291;
export const fakeEndTsOne = -13885091;
export const fakeStartTsTwo = 1092844665;
export const fakeEndTsTwo = 1277049465;

export const readAllCheck = [
{
...mockData.phone_data[0].data,
},
];

export const readAllCompositeCheck = [
{
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,
},
];
15 changes: 10 additions & 5 deletions www/__tests__/timelineHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { mockLogger } from '../__mocks__/globalMocks';
import { clearAlerts, mockAlert, mockLogger } from '../__mocks__/globalMocks';
import { readAllCompositeTrips, readUnprocessedTrips } from '../js/diary/timelineHelper';
import { mockBEMUserCache } from '../__mocks__/cordovaMocks';

import * as mockTLH from '../__mocks__/timelineHelperMocks';

mockLogger();
mockAlert();
mockBEMUserCache();

beforeEach(() => {
clearAlerts();
});

afterAll(() => {
jest.restoreAllMocks();
});
Expand All @@ -21,13 +26,13 @@ jest.mock('../js/commHelper', () => ({
}));

it('works when there are no composite trip objects fetched', async () => {
expect(readAllCompositeTrips(-1, -1)).resolves.not.toThrow();
expect(readAllCompositeTrips(-1, -1)).resolves.toEqual([]);
});

it('fetches a composite trip object and collapses it', async () => {
expect(
readAllCompositeTrips(mockTLH.fakeStartTsOne, mockTLH.fakeEndTsOne),
).resolves.not.toThrow();
expect(readAllCompositeTrips(mockTLH.fakeStartTsOne, mockTLH.fakeEndTsOne)).resolves.toEqual(
mockTLH.readAllCompositeCheck,
);
expect(
readAllCompositeTrips(mockTLH.fakeStartTsTwo, mockTLH.fakeEndTsTwo),
).resolves.not.toThrow();
Expand Down

0 comments on commit 2d0d321

Please sign in to comment.