summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/utils/datetime
diff options
context:
space:
mode:
Diffstat (limited to 'spec/frontend/lib/utils/datetime')
-rw-r--r--spec/frontend/lib/utils/datetime/date_calculation_utility_spec.js17
-rw-r--r--spec/frontend/lib/utils/datetime/date_format_utility_spec.js12
-rw-r--r--spec/frontend/lib/utils/datetime/timeago_utility_spec.js50
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 =