-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Jest testing for useImperialConfig.ts
useImperialConfig.ts - Added exports to the pure functions to get them inside the test file useImperialConfig.test.ts - Added a Jest mock to mock the useAppConfig - Tested use cases for the 4 functions
- Loading branch information
1 parent
80b4794
commit c1a10dd
Showing
2 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { getFormattedDistanceInKm, getFormattedDistanceInMiles, getKmph, getMph } 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: { | ||
use_imperial: false | ||
}, | ||
loading: false | ||
})); | ||
}); | ||
|
||
|
||
describe('getFormattedDistanceInKm', () => { | ||
it('should format distance in kilometers', () => { | ||
expect(getFormattedDistanceInKm('1000')).toBe('1.00'); | ||
expect(getFormattedDistanceInKm('1500')).toBe('1.50'); | ||
expect(getFormattedDistanceInKm('500')).toBe('0.50'); | ||
expect(getFormattedDistanceInKm('0')).toBe('0.0'); | ||
}); | ||
|
||
it('returns NaN for an empty input', () => { | ||
expect(getFormattedDistanceInKm('')).toBe('NaN'); | ||
}); | ||
|
||
it('returns NaN for a non-numeric input', () => { | ||
expect(getFormattedDistanceInKm('abc')).toBe('NaN'); | ||
}); | ||
}); | ||
|
||
describe('getFormattedDistanceInMiles', () => { | ||
it('should format distance in miles', () => { | ||
expect(getFormattedDistanceInMiles('1609.34')).toBe('1.0'); | ||
expect(getFormattedDistanceInMiles('3218.68')).toBe('2.00'); | ||
expect(getFormattedDistanceInMiles('804.672')).toBe('0.50'); | ||
expect(getFormattedDistanceInKm('0')).toBe('0.0'); | ||
}); | ||
|
||
it('returns NaN for an empty input', () => { | ||
expect(getFormattedDistanceInMiles('')).toBe('NaN'); | ||
}); | ||
|
||
it('returns NaN for a non-numeric input', () => { | ||
expect(getFormattedDistanceInMiles('abc')).toBe('NaN'); | ||
}); | ||
}); | ||
|
||
describe('getKmph', () => { | ||
it('should convert meters per second to kilometers per hour', () => { | ||
expect(getKmph('10')).toBe('36.00'); | ||
expect(getKmph('20')).toBe('72.00'); | ||
expect(getKmph('5')).toBe('18.00'); | ||
expect(getKmph('30.0')).toBe('108.00'); | ||
}); | ||
|
||
it('returns 0.00 for an empty input', () => { | ||
expect(getKmph('')).toBe('0.00'); | ||
}); | ||
|
||
it('returns NaN for a non-numeric input', () => { | ||
expect(getKmph('abc')).toBe('NaN'); | ||
}); | ||
}); | ||
|
||
describe('getMph', () => { | ||
it('should convert meters per second to miles per hour', () => { | ||
expect(getMph('10')).toBe('22.37'); | ||
expect(getMph('20')).toBe('44.74'); | ||
expect(getMph('5')).toBe('11.18'); | ||
}); | ||
|
||
it('returns 0.00 for an empty input', () => { | ||
expect(getMph('')).toBe('0.00'); | ||
}); | ||
|
||
it('returns NaN for a non-numeric input', () => { | ||
expect(getMph('abc')).toBe('NaN'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters