Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add new fn getElapsedHours (#19892)
- Loading branch information
1 parent
6be73f1
commit 79c2532
Showing
3 changed files
with
56 additions
and
11 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,37 @@ | ||
import { getElapsedDays, getElapsedHours, getElapsedMinutes } from './date'; | ||
|
||
const ONE_MINUTE_MS = 60 * 1000; | ||
const ONE_HOUR_MS = 60 * ONE_MINUTE_MS; | ||
const ONE_DAY_MS = 24 * ONE_HOUR_MS; | ||
|
||
describe('util/date', () => { | ||
const Jan1 = new Date(new Date().getFullYear(), 0, 1); | ||
|
||
it('returns elapsed days', () => { | ||
const elapsedDays = Math.floor( | ||
(new Date().getTime() - new Date(Jan1).getTime()) / ONE_DAY_MS | ||
); | ||
expect(getElapsedDays(Jan1.toDateString())).toBe(elapsedDays); | ||
}); | ||
|
||
it('returns elapsed minutes', () => { | ||
const elapsedMinutes = Math.floor( | ||
(new Date().getTime() - new Date(Jan1).getTime()) / ONE_MINUTE_MS | ||
); | ||
expect(getElapsedMinutes(new Date(Jan1))).toBe(elapsedMinutes); | ||
}); | ||
|
||
describe('getElapsedHours', () => { | ||
it('returns elapsed hours', () => { | ||
const elapsedHours = Math.floor( | ||
(new Date().getTime() - new Date(Jan1).getTime()) / ONE_HOUR_MS | ||
); | ||
expect(getElapsedHours(Jan1.toISOString())).toBe(elapsedHours); // ISOstring | ||
expect(getElapsedHours(Jan1)).toBe(elapsedHours); // JS Date | ||
}); | ||
|
||
it('throws when invalid date is passed', () => { | ||
expect(getElapsedHours(new Date('invalid_date_string'))).toBe(0); | ||
}); | ||
}); | ||
}); |
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
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