Skip to content

Commit

Permalink
write tests for metHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
Abby Wheelis committed Nov 9, 2023
1 parent 7ebcc94 commit 3943053
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
45 changes: 45 additions & 0 deletions www/__tests__/metHelper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { getMet, setUseCustomMET } from '../js/metrics/metHelper';
import { mockBEMUserCache } from '../__mocks__/cordovaMocks';
import { mockLogger } from '../__mocks__/globalMocks';
import fakeLabels from '../__mocks__/fakeLabels.json';
import { getConfig } from '../js/config/dynamicConfig';
import { initCustomDatasetHelper } from '../js/metrics/CustomMetricsHelper';

mockBEMUserCache();
mockLogger();

global.fetch = (url: string) =>
new Promise((rs, rj) => {
setTimeout(() =>
rs({
text: () =>
new Promise((rs, rj) => {
let myJSON = JSON.stringify(fakeLabels);
setTimeout(() => rs(myJSON), 100);
}),
}),
);
}) as any;

beforeEach(() => {
setUseCustomMET(false);
});

it('gets met for mode and speed', () => {
expect(getMet('WALKING', 1.47523, 0)).toBe(4.3);
expect(getMet('BICYCLING', 4.5, 0)).toBe(6.8);
expect(getMet('UNICYCLE', 100, 0)).toBe(0);
expect(getMet('CAR', 25, 1)).toBe(0);
});

it('gets custom met for mode and speed', async () => {
initCustomDatasetHelper(getConfig());
setUseCustomMET(true);
await new Promise((r) => setTimeout(r, 500));
expect(getMet('walk', 1.47523, 0)).toBe(4.3);
expect(getMet('bike', 4.5, 0)).toBe(6.8);
expect(getMet('unicycle', 100, 0)).toBe(0);
expect(getMet('drove_alone', 25, 1)).toBe(0);
expect(getMet('e-bike', 6, 1)).toBe(4.9);
expect(getMet('e-bike', 12, 1)).toBe(4.9);
});
4 changes: 2 additions & 2 deletions www/js/metrics/metHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { standardMETs } from './metDataset';

let useCustom = false;

export const setUseCustomMET = function () {
useCustom = true;
export const setUseCustomMET = function (val: boolean) {
useCustom = val;
};

const getMETs = function () {
Expand Down

0 comments on commit 3943053

Please sign in to comment.