Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/service_rewrite_2023' into met…
Browse files Browse the repository at this point in the history
…rics-services-rewrite
  • Loading branch information
Abby Wheelis committed Nov 10, 2023
2 parents 9340297 + ccb50c5 commit 47daa3e
Show file tree
Hide file tree
Showing 27 changed files with 1,926 additions and 1,158 deletions.
53 changes: 53 additions & 0 deletions www/__mocks__/cordovaMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const mockBEMUserCache = () => {
return new Promise<void>((rs, rj) =>
setTimeout(() => {
for (let p in _cache) delete _cache[p];
for (let doc in _storage) delete _storage[doc];
rs();
}, 100),
);
Expand Down Expand Up @@ -141,7 +142,59 @@ export const mockBEMDataCollection = () => {
_storage['config/consent'] = consentDoc;
}, 100);
},
getConfig: () => {
return new Promise<any>((rs, rj) => {
setTimeout(() => {
rs({ ios_use_remote_push_for_sync: true });
}, 100);
});
},
handleSilentPush: () => {
return new Promise<void>((rs, rj) =>
setTimeout(() => {
rs();
}, 100),
);
},
};
window['cordova'] ||= {};
window['cordova'].plugins.BEMDataCollection = mockBEMDataCollection;
};

export const mockBEMServerCom = () => {
const mockBEMServerCom = {
postUserPersonalData: (actionString, typeString, updateDoc, rs, rj) => {
setTimeout(() => {
console.log('set in mock', updateDoc);
_storage['user_data'] = updateDoc;
rs();
}, 100);
},

getUserPersonalData: (actionString, rs, rj) => {
setTimeout(() => {
rs(_storage['user_data']);
}, 100);
},
};
window['cordova'].plugins.BEMServerComm = mockBEMServerCom;
};

let _url_stash = '';

export const mockInAppBrowser = () => {
const mockInAppBrowser = {
open: (url: string, mode: string, options: {}) => {
_url_stash = url;
},
};
window['cordova'].InAppBrowser = mockInAppBrowser;
};

export const getURL = () => {
return _url_stash;
};

export const clearURL = () => {
_url_stash = '';
};
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;
};
33 changes: 33 additions & 0 deletions www/__mocks__/pushNotificationMocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
let notifSettings;
let onList: any = {};
let called = null;

export const mockPushNotification = () => {
window['PushNotification'] = {
init: (settings: Object) => {
notifSettings = settings;
return {
on: (event: string, callback: Function) => {
onList[event] = callback;
},
finish: (content: any, errorFcn: Function, notID: any) => {
called = notID;
},
};
},
};
};

export const clearNotifMock = function () {
notifSettings = {};
onList = {};
called = null;
};

export const getOnList = function () {
return onList;
};

export const getCalled = function () {
return called;
};
24 changes: 24 additions & 0 deletions www/__tests__/customEventHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { publish, subscribe, unsubscribe } from '../js/customEventHandler';
import { mockLogger } from '../__mocks__/globalMocks';

mockLogger();

it('subscribes and publishes to an event', () => {
const listener = jest.fn();
subscribe('test', listener);
publish('test', 'test data');
expect(listener).toHaveBeenCalledWith(
expect.objectContaining({
type: 'test',
detail: 'test data',
}),
);
});

it('can unsubscribe', () => {
const listener = jest.fn();
subscribe('test', listener);
unsubscribe('test', listener);
publish('test', 'test data');
expect(listener).not.toHaveBeenCalled();
});
251 changes: 251 additions & 0 deletions www/__tests__/inputMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
import {
fmtTs,
printUserInput,
validUserInputForDraftTrip,
validUserInputForTimelineEntry,
getNotDeletedCandidates,
getUserInputForTrip,
getAdditionsForTimelineEntry,
getUniqueEntries,
} from '../js/survey/inputMatcher';
import { TlEntry, UserInput } from '../js/types/diaryTypes';

describe('input-matcher', () => {
let userTrip: UserInput;
let trip: TlEntry;

beforeEach(() => {
/*
Create a valid userTrip and trip object before each test case.
The trip data is from the 'real_examples' data (shankari_2015-07-22) on the server.
For some test cases, I need to generate fake data, such as labels, keys, and origin_keys.
In such cases, I referred to 'TestUserInputFakeData.py' on the server.
*/
userTrip = {
data: {
end_ts: 1437604764,
start_ts: 1437601247,
label: 'FOO',
status: 'ACTIVE',
},
metadata: {
time_zone: 'America/Los_Angeles',
plugin: 'none',
write_ts: 1695921991,
platform: 'ios',
read_ts: 0,
key: 'manual/mode_confirm',
},
key: 'manual/place',
};
trip = {
key: 'FOO',
origin_key: 'FOO',
start_ts: 1437601000,
end_ts: 1437605000,
enter_ts: 1437605000,
exit_ts: 1437605000,
duration: 100,
getNextEntry: jest.fn(),
};

// mock Logger
window['Logger'] = { log: console.log };
});

it('tests fmtTs with valid input', () => {
const pstTime = fmtTs(1437601247.8459613, 'America/Los_Angeles');
const estTime = fmtTs(1437601247.8459613, 'America/New_York');

// Check if it contains correct year-mm-dd hr:mm
expect(pstTime).toContain('2015-07-22T14:40');
expect(estTime).toContain('2015-07-22T17:40');
});

it('tests fmtTs with invalid input', () => {
const formattedTime = fmtTs(0, '');
expect(formattedTime).toBeFalsy();
});

it('tests printUserInput prints the trip log correctly', () => {
const userTripLog = printUserInput(userTrip);
expect(userTripLog).toContain('1437604764');
expect(userTripLog).toContain('1437601247');
expect(userTripLog).toContain('FOO');
});

it('tests validUserInputForDraftTrip with valid trip input', () => {
const validTrp = {
end_ts: 1437604764,
start_ts: 1437601247,
};
const validUserInput = validUserInputForDraftTrip(validTrp, userTrip, false);
expect(validUserInput).toBeTruthy();
});

it('tests validUserInputForDraftTrip with invalid trip input', () => {
const invalidTrip = {
end_ts: 0,
start_ts: 0,
};
const invalidUserInput = validUserInputForDraftTrip(invalidTrip, userTrip, false);
expect(invalidUserInput).toBeFalsy();
});

it('tests validUserInputForTimelineEntry with valid trip object', () => {
// we need valid key and origin_key for validUserInputForTimelineEntry test
trip['key'] = 'analysis/confirmed_place';
trip['origin_key'] = 'analysis/confirmed_place';
const validTimelineEntry = validUserInputForTimelineEntry(trip, userTrip, false);
expect(validTimelineEntry).toBeTruthy();
});

it('tests validUserInputForTimelineEntry with tlEntry with invalid key and origin_key', () => {
const invalidTlEntry = trip;
const invalidTimelineEntry = validUserInputForTimelineEntry(invalidTlEntry, userTrip, false);
expect(invalidTimelineEntry).toBeFalsy();
});

it('tests validUserInputForTimelineEntry with tlEntry with invalie start & end time', () => {
const invalidTlEntry: TlEntry = {
key: 'analysis/confirmed_place',
origin_key: 'analysis/confirmed_place',
start_ts: 1,
end_ts: 1,
enter_ts: 1,
exit_ts: 1,
duration: 1,
getNextEntry: jest.fn(),
};
const invalidTimelineEntry = validUserInputForTimelineEntry(invalidTlEntry, userTrip, false);
expect(invalidTimelineEntry).toBeFalsy();
});

it('tests getNotDeletedCandidates called with 0 candidates', () => {
jest.spyOn(console, 'log');
const candidates = getNotDeletedCandidates([]);

// check if the log printed collectly with
expect(console.log).toHaveBeenCalledWith('getNotDeletedCandidates called with 0 candidates');
expect(candidates).toStrictEqual([]);
});

it('tests getNotDeletedCandidates called with multiple candidates', () => {
const activeTrip = userTrip;
const deletedTrip = {
data: {
end_ts: 1437604764,
start_ts: 1437601247,
label: 'FOO',
status: 'DELETED',
match_id: 'FOO',
},
metadata: {
time_zone: 'America/Los_Angeles',
plugin: 'none',
write_ts: 1695921991,
platform: 'ios',
read_ts: 0,
key: 'manual/mode_confirm',
},
key: 'manual/place',
};
const candidates = [activeTrip, deletedTrip];
const validCandidates = getNotDeletedCandidates(candidates);

// check if the result has only 'ACTIVE' data
expect(validCandidates).toHaveLength(1);
expect(validCandidates[0]).toMatchObject(userTrip);
});

it('tests getUserInputForTrip with valid userInputList', () => {
const userInputWriteFirst = {
data: {
end_ts: 1437607732,
label: 'bus',
start_ts: 1437606026,
},
metadata: {
time_zone: 'America/Los_Angeles',
plugin: 'none',
write_ts: 1695830232,
platform: 'ios',
read_ts: 0,
key: 'manual/mode_confirm',
type: 'message',
},
};
const userInputWriteSecond = {
data: {
end_ts: 1437598393,
label: 'e-bike',
start_ts: 1437596745,
},
metadata: {
time_zone: 'America/Los_Angeles',
plugin: 'none',
write_ts: 1695838268,
platform: 'ios',
read_ts: 0,
key: 'manual/mode_confirm',
type: 'message',
},
};
const userInputWriteThird = {
data: {
end_ts: 1437604764,
label: 'e-bike',
start_ts: 1437601247,
},
metadata: {
time_zone: 'America/Los_Angeles',
plugin: 'none',
write_ts: 1695921991,
platform: 'ios',
read_ts: 0,
key: 'manual/mode_confirm',
type: 'message',
},
};

// make the linst unsorted and then check if userInputWriteThird(latest one) is return output
const userInputList = [userInputWriteSecond, userInputWriteThird, userInputWriteFirst];
const mostRecentEntry = getUserInputForTrip(trip, {}, userInputList);
expect(mostRecentEntry).toMatchObject(userInputWriteThird);
});

it('tests getUserInputForTrip with invalid userInputList', () => {
const userInputList = undefined;
const mostRecentEntry = getUserInputForTrip(trip, {}, userInputList);
expect(mostRecentEntry).toBe(undefined);
});

it('tests getAdditionsForTimelineEntry with valid additionsList', () => {
const additionsList = new Array(5).fill(userTrip);
trip['key'] = 'analysis/confirmed_place';
trip['origin_key'] = 'analysis/confirmed_place';

// check if the result keep the all valid userTrip items
const matchingAdditions = getAdditionsForTimelineEntry(trip, additionsList);
expect(matchingAdditions).toHaveLength(5);
});

it('tests getAdditionsForTimelineEntry with invalid additionsList', () => {
const additionsList = undefined;
const matchingAdditions = getAdditionsForTimelineEntry(trip, additionsList);
expect(matchingAdditions).toMatchObject([]);
});

it('tests getUniqueEntries with valid combinedList', () => {
const combinedList = new Array(5).fill(userTrip);

// check if the result keeps only unique userTrip items
const uniqueEntires = getUniqueEntries(combinedList);
expect(uniqueEntires).toHaveLength(1);
});

it('tests getUniqueEntries with empty combinedList', () => {
const uniqueEntires = getUniqueEntries([]);
expect(uniqueEntires).toMatchObject([]);
});
});
Loading

0 comments on commit 47daa3e

Please sign in to comment.