Skip to content

Commit

Permalink
add useImperialConfig test
Browse files Browse the repository at this point in the history
  • Loading branch information
jiji14 committed May 29, 2024
1 parent 66464ca commit a963381
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions www/__tests__/useImperialConfig.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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');
});
});

0 comments on commit a963381

Please sign in to comment.