Skip to content

Commit

Permalink
await instead of timeout
Browse files Browse the repository at this point in the history
in testing the initCustomDatasets function, I was calling it and then setting a timeout to wait for it to run, now I await getting the config, then await initialization

double checked the Jest tests and running in the emulator - still going smoothly!
  • Loading branch information
Abby Wheelis committed Nov 19, 2023
1 parent b040691 commit 51f6ece
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
8 changes: 4 additions & 4 deletions www/__tests__/customMetricsHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ global.fetch = (url: string) =>
}) as any;

it('gets the custom mets', async () => {
initCustomDatasetHelper(getConfig());
await new Promise((r) => setTimeout(r, 800));
const appConfig = await getConfig();
await initCustomDatasetHelper(appConfig);
expect(getCustomMETs()).toMatchObject({
walk: {},
bike: {},
Expand All @@ -38,8 +38,8 @@ it('gets the custom mets', async () => {
});

it('gets the custom footprint', async () => {
initCustomDatasetHelper(getConfig());
await new Promise((r) => setTimeout(r, 800));
const appConfig = await getConfig();
await initCustomDatasetHelper(appConfig);
expect(getCustomFootprint()).toMatchObject({
walk: {},
bike: {},
Expand Down
16 changes: 8 additions & 8 deletions www/__tests__/footprintHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,25 @@ const custom_metrics = [
];

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

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

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

it('gets the highest footprint for distance, custom', async () => {
initCustomDatasetHelper(getConfig());
await new Promise((r) => setTimeout(r, 500));
const appConfig = await getConfig();
await initCustomDatasetHelper(appConfig);
expect(getHighestFootprintForDistance(12345)).toBe(0.30741 * (12345 / 1000));
});
4 changes: 2 additions & 2 deletions www/__tests__/metHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ it('gets met for mode and speed', () => {
});

it('gets custom met for mode and speed', async () => {
initCustomDatasetHelper(getConfig());
await new Promise((r) => setTimeout(r, 500));
const appConfig = await getConfig();
await initCustomDatasetHelper(appConfig);
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);
Expand Down
11 changes: 5 additions & 6 deletions www/js/metrics/customMetricsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,11 @@ const populateCustomFootprints = function () {
export const initCustomDatasetHelper = async function (newConfig) {
try {
logDebug('initializing custom datasets with config' + newConfig);
getLabelOptions(newConfig).then((labelOptions) => {
console.log('In custom metrics, label options: ', labelOptions);
_labelOptions = labelOptions;
populateCustomMETs();
populateCustomFootprints();
});
const labelOptions = await getLabelOptions(newConfig);
console.log('In custom metrics, label options: ', labelOptions);
_labelOptions = labelOptions;
populateCustomMETs();
populateCustomFootprints();
} catch (e) {
setTimeout(() => {
displayError(e, 'Error while initializing custom dataset helper');
Expand Down

0 comments on commit 51f6ece

Please sign in to comment.