diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-20 10:00:54 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-20 10:00:54 +0000 |
commit | 3cccd102ba543e02725d247893729e5c73b38295 (patch) | |
tree | f36a04ec38517f5deaaacb5acc7d949688d1e187 /spec/frontend/lib/utils/datetime | |
parent | 205943281328046ef7b4528031b90fbda70c75ac (diff) | |
download | gitlab-ce-3cccd102ba543e02725d247893729e5c73b38295.tar.gz |
Add latest changes from gitlab-org/gitlab@14-10-stable-eev14.10.0-rc42
Diffstat (limited to 'spec/frontend/lib/utils/datetime')
3 files changed, 78 insertions, 1 deletions
diff --git a/spec/frontend/lib/utils/datetime/date_calculation_utility_spec.js b/spec/frontend/lib/utils/datetime/date_calculation_utility_spec.js new file mode 100644 index 00000000000..47bb512cbb5 --- /dev/null +++ b/spec/frontend/lib/utils/datetime/date_calculation_utility_spec.js @@ -0,0 +1,17 @@ +import { newDateAsLocaleTime } from '~/lib/utils/datetime/date_calculation_utility'; + +describe('newDateAsLocaleTime', () => { + it.each` + string | expected + ${'2022-03-22'} | ${new Date('2022-03-22T00:00:00.000Z')} + ${'2022-03-22T00:00:00.000Z'} | ${new Date('2022-03-22T00:00:00.000Z')} + ${2022} | ${null} + ${[]} | ${null} + ${{}} | ${null} + ${true} | ${null} + ${null} | ${null} + ${undefined} | ${null} + `('returns $expected given $string', ({ string, expected }) => { + expect(newDateAsLocaleTime(string)).toEqual(expected); + }); +}); diff --git a/spec/frontend/lib/utils/datetime/date_format_utility_spec.js b/spec/frontend/lib/utils/datetime/date_format_utility_spec.js index 1adc70450e8..018ae12c908 100644 --- a/spec/frontend/lib/utils/datetime/date_format_utility_spec.js +++ b/spec/frontend/lib/utils/datetime/date_format_utility_spec.js @@ -133,3 +133,15 @@ describe('formatTimeAsSummary', () => { expect(utils.formatTimeAsSummary({ [unit]: value })).toBe(result); }); }); + +describe('durationTimeFormatted', () => { + it.each` + duration | expectedOutput + ${87} | ${'00:01:27'} + ${141} | ${'00:02:21'} + ${12} | ${'00:00:12'} + ${60} | ${'00:01:00'} + `('returns $expectedOutput when provided $duration', ({ duration, expectedOutput }) => { + expect(utils.durationTimeFormatted(duration)).toBe(expectedOutput); + }); +}); diff --git a/spec/frontend/lib/utils/datetime/timeago_utility_spec.js b/spec/frontend/lib/utils/datetime/timeago_utility_spec.js index 2314ec678d3..1ef7047d959 100644 --- a/spec/frontend/lib/utils/datetime/timeago_utility_spec.js +++ b/spec/frontend/lib/utils/datetime/timeago_utility_spec.js @@ -1,4 +1,4 @@ -import { getTimeago, localTimeAgo, timeFor } from '~/lib/utils/datetime/timeago_utility'; +import { getTimeago, localTimeAgo, timeFor, duration } from '~/lib/utils/datetime/timeago_utility'; import { s__ } from '~/locale'; import '~/commons/bootstrap'; @@ -66,6 +66,54 @@ describe('TimeAgo utils', () => { }); }); + describe('duration', () => { + const ONE_DAY = 24 * 60 * 60; + + it.each` + secs | formatted + ${0} | ${'0 seconds'} + ${30} | ${'30 seconds'} + ${59} | ${'59 seconds'} + ${60} | ${'1 minute'} + ${-60} | ${'1 minute'} + ${2 * 60} | ${'2 minutes'} + ${60 * 60} | ${'1 hour'} + ${2 * 60 * 60} | ${'2 hours'} + ${ONE_DAY} | ${'1 day'} + ${2 * ONE_DAY} | ${'2 days'} + ${7 * ONE_DAY} | ${'1 week'} + ${14 * ONE_DAY} | ${'2 weeks'} + ${31 * ONE_DAY} | ${'1 month'} + ${61 * ONE_DAY} | ${'2 months'} + ${365 * ONE_DAY} | ${'1 year'} + ${365 * 2 * ONE_DAY} | ${'2 years'} + `('formats $secs as "$formatted"', ({ secs, formatted }) => { + const ms = secs * 1000; + + expect(duration(ms)).toBe(formatted); + }); + + // `duration` can be used to format Rails month durations. + // Ensure formatting for quantities such as `2.months.to_i` + // based on ActiveSupport::Duration::SECONDS_PER_MONTH. + // See: https://api.rubyonrails.org/classes/ActiveSupport/Duration.html + const SECONDS_PER_MONTH = 2629746; // 1.month.to_i + + it.each` + duration | secs | formatted + ${'1.month'} | ${SECONDS_PER_MONTH} | ${'1 month'} + ${'2.months'} | ${SECONDS_PER_MONTH * 2} | ${'2 months'} + ${'3.months'} | ${SECONDS_PER_MONTH * 3} | ${'3 months'} + `( + 'formats ActiveSupport::Duration of `$duration` ($secs) as "$formatted"', + ({ secs, formatted }) => { + const ms = secs * 1000; + + expect(duration(ms)).toBe(formatted); + }, + ); + }); + describe('localTimeAgo', () => { beforeEach(() => { document.body.innerHTML = |