Skip to content

Commit

Permalink
write footprinthelper tests
Browse files Browse the repository at this point in the history
first pass of tests over the footprintHelper

getFootprintForMetrics is one of the main functions, used to calculate displayed values for the dashboard screen
  • Loading branch information
Abby Wheelis committed Nov 9, 2023
1 parent 5108352 commit 9118133
Show file tree
Hide file tree
Showing 7 changed files with 385 additions and 9 deletions.
22 changes: 17 additions & 5 deletions www/__mocks__/cordovaMocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import packageJsonBuild from '../../package.cordovabuild.json';
import fakeConfig from './fakeConfig.json';

export const mockCordova = () => {
window['cordova'] ||= {};
Expand Down Expand Up @@ -99,11 +100,22 @@ export const mockBEMUserCache = () => {
);
},
getDocument: (key: string, withMetadata?: boolean) => {
return new Promise<any[]>((rs, rj) =>
setTimeout(() => {
rs(_storage[key]);
}, 100),
);
// this was mocked specifically for enketoHelper's use, could be expanded if needed
const fakeSurveyConfig = fakeConfig;

if (key == 'config/app_ui_config') {
return new Promise<any>((rs, rj) =>
setTimeout(() => {
rs(fakeSurveyConfig);
}, 100),
);
} else {
return new Promise<any[]>((rs, rj) =>
setTimeout(() => {
rs(_storage[key]);
}, 100),
);
}
},
isEmptyDoc: (doc) => {
if (doc == undefined) {
Expand Down
88 changes: 88 additions & 0 deletions www/__mocks__/fakeConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"version": 1,
"ts": 1655143472,
"server": {
"connectUrl": "https://openpath-test.nrel.gov/api/",
"aggregate_call_auth": "user_only"
},
"intro": {
"program_or_study": "study",
"start_month": "10",
"start_year": "2023",
"program_admin_contact": "K. Shankari",
"deployment_partner_name": "NREL",
"translated_text": {
"en": {
"deployment_partner_name": "NREL",
"deployment_name": "Testing environment for Jest testing",
"summary_line_1": "",
"summary_line_2": "",
"summary_line_3": "",
"short_textual_description": "",
"why_we_collect": "",
"research_questions": ["", ""]
},
"es": {
"deployment_partner_name": "NREL",
"deployment_name": "Ambiente prueba para las pruebas de Jest",
"summary_line_1": "",
"summary_line_2": "",
"summary_line_3": "",
"short_textual_description": "",
"why_we_collect": "",
"research_questions": ["", ""]
}
}
},
"survey_info": {
"surveys": {
"TimeUseSurvey": {
"compatibleWith": 1,
"formPath": "https://raw.githubusercontent.com/sebastianbarry/nrel-openpath-deploy-configs/surveys-info-and-surveys-data/survey-resources/data-json/time-use-survey-form-v9.json",
"labelTemplate": {
"en": "{ erea, plural, =0 {} other {# Employment/Education, } }{ da, plural, =0 {} other {# Domestic, } }",
"es": "{ erea, plural, =0 {} other {# Empleo/Educación, } }{ da, plural, =0 {} other {# Actividades domesticas, }}"
},
"labelVars": {
"da": {
"key": "Domestic_activities",
"type": "length"
},
"erea": {
"key": "Employment_related_a_Education_activities",
"type": "length"
}
},
"version": 9
}
},
"trip-labels": "ENKETO"
},
"display_config": {
"use_imperial": false
},
"profile_controls": {
"support_upload": true,
"trip_end_notification": false
},
"admin_dashboard": {
"overview_users": true,
"overview_active_users": true,
"overview_trips": true,
"overview_signup_trends": true,
"overview_trips_trend": true,
"data_uuids": true,
"data_trips": true,
"data_trips_columns_exclude": [],
"additional_trip_columns": [],
"data_uuids_columns_exclude": [],
"token_generate": true,
"token_prefix": "nrelop",
"map_heatmap": true,
"map_bubble": true,
"map_trip_lines": true,
"push_send": true,
"options_uuids": true,
"options_emails": true
}
}
211 changes: 211 additions & 0 deletions www/__mocks__/fakeLabels.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
{
"MODE": [
{
"value": "walk",
"baseMode": "WALKING",
"met_equivalent": "WALKING",
"kgCo2PerKm": 0
},
{
"value": "e-bike",
"baseMode": "E_BIKE",
"met": {
"ALL": {
"range": [
0,
-1
],
"mets": 4.9
}
},
"kgCo2PerKm": 0.00728
},
{
"value": "bike",
"baseMode": "BICYCLING",
"met_equivalent": "BICYCLING",
"kgCo2PerKm": 0
},
{
"value": "bikeshare",
"baseMode": "BICYCLING",
"met_equivalent": "BICYCLING",
"kgCo2PerKm": 0
},
{
"value": "scootershare",
"baseMode": "E_SCOOTER",
"met_equivalent": "IN_VEHICLE",
"kgCo2PerKm": 0.00894
},
{
"value": "drove_alone",
"baseMode": "CAR",
"met_equivalent": "IN_VEHICLE",
"kgCo2PerKm": 0.22031
},
{
"value": "shared_ride",
"baseMode": "CAR",
"met_equivalent": "IN_VEHICLE",
"kgCo2PerKm": 0.11015
},
{
"value": "hybrid_drove_alone",
"baseMode": "CAR",
"met_equivalent": "IN_VEHICLE",
"kgCo2PerKm": 0.127
},
{
"value": "hybrid_shared_ride",
"baseMode": "CAR",
"met_equivalent": "IN_VEHICLE",
"kgCo2PerKm": 0.0635
},
{
"value": "e_car_drove_alone",
"baseMode": "E_CAR",
"met_equivalent": "IN_VEHICLE",
"kgCo2PerKm": 0.08216
},
{
"value": "e_car_shared_ride",
"baseMode": "E_CAR",
"met_equivalent": "IN_VEHICLE",
"kgCo2PerKm": 0.04108
},
{
"value": "taxi",
"baseMode": "TAXI",
"met_equivalent": "IN_VEHICLE",
"kgCo2PerKm": 0.30741
},
{
"value": "bus",
"baseMode": "BUS",
"met_equivalent": "IN_VEHICLE",
"kgCo2PerKm": 0.20727
},
{
"value": "train",
"baseMode": "TRAIN",
"met_equivalent": "IN_VEHICLE",
"kgCo2PerKm": 0.12256
},
{
"value": "free_shuttle",
"baseMode": "BUS",
"met_equivalent": "IN_VEHICLE",
"kgCo2PerKm": 0.20727
},
{
"value": "air",
"baseMode": "AIR",
"met_equivalent": "IN_VEHICLE",
"kgCo2PerKm": 0.09975
},
{
"value": "not_a_trip",
"baseMode": "UNKNOWN",
"met_equivalent": "UNKNOWN",
"kgCo2PerKm": 0
},
{
"value": "other",
"baseMode": "OTHER",
"met_equivalent": "UNKNOWN",
"kgCo2PerKm": 0
}
],
"PURPOSE": [
{
"value": "home"
},
{
"value": "work"
},
{
"value": "at_work"
},
{
"value": "school"
},
{
"value": "transit_transfer"
},
{
"value": "shopping"
},
{
"value": "meal"
},
{
"value": "pick_drop_person"
},
{
"value": "pick_drop_item"
},
{
"value": "personal_med"
},
{
"value": "access_recreation"
},
{
"value": "exercise"
},
{
"value": "entertainment"
},
{
"value": "religious"
},
{
"value": "other"
}
],
"REPLACED_MODE": [
{
"value": "no_travel"
},
{
"value": "walk"
},
{
"value": "bike"
},
{
"value": "bikeshare"
},
{
"value": "scootershare"
},
{
"value": "drove_alone"
},
{
"value": "shared_ride"
},
{
"value": "e_car_drove_alone"
},
{
"value": "e_car_shared_ride"
},
{
"value": "taxi"
},
{
"value": "bus"
},
{
"value": "train"
},
{
"value": "free_shuttle"
},
{
"value": "other"
}
]
}
64 changes: 64 additions & 0 deletions www/__tests__/footprintHelper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { initCustomDatasetHelper } from '../js/metrics/CustomMetricsHelper';
import {
getFootprintForMetrics,
mtokm,
setUseCustomFootprint,
} from '../js/metrics/footprintHelper';
import { getConfig } from '../js/config/dynamicConfig';
import { mockBEMUserCache } from '../__mocks__/cordovaMocks';
import { mockLogger } from '../__mocks__/globalMocks';
import fakeLabels from '../__mocks__/fakeLabels.json';

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;

const metrics = [
{ key: 'WALKING', values: 3000 },
{ key: 'BICYCLING', values: 6500 },
{ key: 'CAR', values: 50000 },
{ key: 'LIGHT_RAIL', values: 30000 },
{ key: 'Unicycle', values: 5000 },
];

it('gets footprint for metrics (not custom, fallback 0', () => {
expect(getFootprintForMetrics(metrics, 0)).toBe(10.534493474207583);
});

it('gets footprint for metrics (not custom, fallback 0.1', () => {
expect(getFootprintForMetrics(metrics, 0.1)).toBe(10.534493474207583 + 0.5);
});

const custom_metrics = [
{ key: 'walk', values: 3000 },
{ key: 'bike', values: 6500 },
{ key: 'drove_alone', values: 10000 },
{ key: 'scootershare', values: 25000 },
{ key: 'unicycle', values: 5000 },
];

it('gets footprint for metrics (custom, fallback 0', async () => {
initCustomDatasetHelper(getConfig());
setUseCustomFootprint();
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 () => {
initCustomDatasetHelper(getConfig());
setUseCustomFootprint();
await new Promise((r) => setTimeout(r, 500));
expect(getFootprintForMetrics(custom_metrics, 0.1)).toBe(2.4266 + 0.5);
});
Loading

0 comments on commit 9118133

Please sign in to comment.