Skip to content

Commit

Permalink
add tests for highest footprint
Browse files Browse the repository at this point in the history
needed to include methods for clearing out the local variables, so added parameter to be able to set useCuston to false, and to clearHighest
  • Loading branch information
Abby Wheelis committed Nov 9, 2023
1 parent 7820646 commit 7ebcc94
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
43 changes: 38 additions & 5 deletions www/__tests__/footprintHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { initCustomDatasetHelper } from '../js/metrics/CustomMetricsHelper';
import { getFootprintForMetrics, setUseCustomFootprint } from '../js/metrics/footprintHelper';
import {
clearHighestFootprint,
getFootprintForMetrics,
getHighestFootprint,
getHighestFootprintForDistance,
setUseCustomFootprint,
} from '../js/metrics/footprintHelper';
import { getConfig } from '../js/config/dynamicConfig';
import { mockBEMUserCache } from '../__mocks__/cordovaMocks';
import { mockLogger } from '../__mocks__/globalMocks';
Expand All @@ -21,6 +27,11 @@ global.fetch = (url: string) =>
);
}) as any;

beforeEach(() => {
setUseCustomFootprint(false);
clearHighestFootprint();
});

const metrics = [
{ key: 'WALKING', values: 3000 },
{ key: 'BICYCLING', values: 6500 },
Expand All @@ -37,6 +48,14 @@ it('gets footprint for metrics (not custom, fallback 0.1)', () => {
expect(getFootprintForMetrics(metrics, 0.1)).toBe(10.534493474207583 + 0.5);
});

it('gets the highest footprint from the dataset, not custom', () => {
expect(getHighestFootprint()).toBe(278 / 1609);
});

it('gets the highest footprint for distance, not custom', () => {
expect(getHighestFootprintForDistance(12345)).toBe((278 / 1609) * (12345 / 1000));
});

const custom_metrics = [
{ key: 'walk', values: 3000 },
{ key: 'bike', values: 6500 },
Expand All @@ -45,16 +64,30 @@ const custom_metrics = [
{ key: 'unicycle', values: 5000 },
];

it('gets footprint for metrics (custom, fallback 0', async () => {
it('gets footprint for metrics (custom, fallback 0)', async () => {
initCustomDatasetHelper(getConfig());
setUseCustomFootprint();
setUseCustomFootprint(true);
await new Promise((r) => setTimeout(r, 500));
expect(getFootprintForMetrics(custom_metrics, 0)).toBe(2.4266);
});

it('gets footprint for metrics (custom, fallback 0.1', async () => {
it('gets footprint for metrics (custom, fallback 0.1)', async () => {
initCustomDatasetHelper(getConfig());
setUseCustomFootprint();
setUseCustomFootprint(true);
await new Promise((r) => setTimeout(r, 500));
expect(getFootprintForMetrics(custom_metrics, 0.1)).toBe(2.4266 + 0.5);
});

it('gets the highest footprint from the dataset, custom', async () => {
initCustomDatasetHelper(getConfig());
setUseCustomFootprint(true);
await new Promise((r) => setTimeout(r, 500));
expect(getHighestFootprint()).toBe(0.30741);
});

it('gets the highest footprint for distance, custom', async () => {
initCustomDatasetHelper(getConfig());
setUseCustomFootprint(true);
await new Promise((r) => setTimeout(r, 500));
expect(getHighestFootprintForDistance(12345)).toBe(0.30741 * (12345 / 1000));
});
2 changes: 1 addition & 1 deletion www/js/metrics/CarbonFootprintCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const CarbonFootprintCard = ({ userMetrics, aggMetrics }: Props) => {

//set custon dataset, if the labels are custom
if (isCustomLabels(userThisWeekModeMap)) {
setUseCustomFootprint();
setUseCustomFootprint(true);
}

//calculate low-high and format range for prev week, if exists (14 days ago -> 8 days ago)
Expand Down
8 changes: 6 additions & 2 deletions www/js/metrics/footprintHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ let useCustom = false;
const mtokm = function (v) {
return v / 1000;
};
export const setUseCustomFootprint = function (val: boolean) {
useCustom = val;
};

export const setUseCustomFootprint = function () {
useCustom = true;
export const clearHighestFootprint = function () {
//need to clear for testing
highestFootprint = undefined;
};

const getFootprint = function () {
Expand Down

0 comments on commit 7ebcc94

Please sign in to comment.