From a963381e37653f1e1fe729bcaffb63757aa56b13 Mon Sep 17 00:00:00 2001 From: Jijeong Lee Date: Wed, 29 May 2024 04:42:48 -0700 Subject: [PATCH] add useImperialConfig test --- www/__tests__/useImperialConfig.test.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/www/__tests__/useImperialConfig.test.ts b/www/__tests__/useImperialConfig.test.ts index 593498aae..33c354271 100644 --- a/www/__tests__/useImperialConfig.test.ts +++ b/www/__tests__/useImperialConfig.test.ts @@ -1,14 +1,22 @@ -import { convertDistance, convertSpeed, formatForDisplay } from '../js/config/useImperialConfig'; +import React from 'react'; +import { + convertDistance, + convertSpeed, + formatForDisplay, + useImperialConfig, +} from '../js/config/useImperialConfig'; // This mock is required, or else the test will dive into the import chain of useAppConfig.ts and fail when it gets to the root jest.mock('../js/useAppConfig', () => { return jest.fn(() => ({ - appConfig: { + display_config: { use_imperial: false, }, loading: false, })); }); +jest.spyOn(React, 'useState').mockImplementation((initialValue) => [initialValue, jest.fn()]); +jest.spyOn(React, 'useEffect').mockImplementation((effect: () => void) => effect()); describe('formatForDisplay', () => { it('should round to the nearest integer when value is >= 100', () => { @@ -53,3 +61,15 @@ describe('convertSpeed', () => { expect(convertSpeed(6.7056, true)).toBeCloseTo(15); // Approximately 15 mph }); }); + +describe('useImperialConfig', () => { + it('returns ImperialConfig with imperial units', () => { + const imperialConfig = useImperialConfig(); + expect(imperialConfig.distanceSuffix).toBe('km'); + expect(imperialConfig.speedSuffix).toBe('kmph'); + expect(imperialConfig.convertDistance(10)).toBe(0.01); + expect(imperialConfig.convertSpeed(20)).toBe(72); + expect(imperialConfig.getFormattedDistance(10)).toBe('0.01'); + expect(imperialConfig.getFormattedSpeed(20)).toBe('72'); + }); +});