diff options
Diffstat (limited to 'spec/frontend/lib/utils/datetime/timeago_utility_spec.js')
-rw-r--r-- | spec/frontend/lib/utils/datetime/timeago_utility_spec.js | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/spec/frontend/lib/utils/datetime/timeago_utility_spec.js b/spec/frontend/lib/utils/datetime/timeago_utility_spec.js index 1ef7047d959..74ce8175357 100644 --- a/spec/frontend/lib/utils/datetime/timeago_utility_spec.js +++ b/spec/frontend/lib/utils/datetime/timeago_utility_spec.js @@ -1,18 +1,9 @@ +import { DATE_ONLY_FORMAT } from '~/lib/utils/datetime/constants'; import { getTimeago, localTimeAgo, timeFor, duration } from '~/lib/utils/datetime/timeago_utility'; import { s__ } from '~/locale'; import '~/commons/bootstrap'; describe('TimeAgo utils', () => { - let oldGon; - - afterEach(() => { - window.gon = oldGon; - }); - - beforeEach(() => { - oldGon = window.gon; - }); - describe('getTimeago', () => { describe('with User Setting timeDisplayRelative: true', () => { beforeEach(() => { @@ -34,15 +25,37 @@ describe('TimeAgo utils', () => { window.gon = { time_display_relative: false }; }); - it.each([ + const defaultFormatExpectations = [ [new Date().toISOString(), 'Jul 6, 2020, 12:00 AM'], [new Date(), 'Jul 6, 2020, 12:00 AM'], [new Date().getTime(), 'Jul 6, 2020, 12:00 AM'], // Slightly different behaviour when `null` is passed :see_no_evil` [null, 'Jan 1, 1970, 12:00 AM'], - ])('formats date `%p` as `%p`', (date, result) => { + ]; + + it.each(defaultFormatExpectations)('formats date `%p` as `%p`', (date, result) => { expect(getTimeago().format(date)).toEqual(result); }); + + describe('with unknown format', () => { + it.each(defaultFormatExpectations)( + 'uses default format and formats date `%p` as `%p`', + (date, result) => { + expect(getTimeago('non_existent').format(date)).toEqual(result); + }, + ); + }); + + describe('with DATE_ONLY_FORMAT', () => { + it.each([ + [new Date().toISOString(), 'Jul 6, 2020'], + [new Date(), 'Jul 6, 2020'], + [new Date().getTime(), 'Jul 6, 2020'], + [null, 'Jan 1, 1970'], + ])('formats date `%p` as `%p`', (date, result) => { + expect(getTimeago(DATE_ONLY_FORMAT).format(date)).toEqual(result); + }); + }); }); }); |