Add duration unit format (#2483)

* chore: 🤖 add @formatjs/intl-durationformat to addons/core

* refactor: 💡 replace old duration format with new package

* chore: 🤖 remove intl-unofficial-duration-unit-format

* Add missing copyright headers

* test: 💍 add more test cases

---------

Co-authored-by: cameronperera <cameronperera@users.noreply.github.com>
This commit is contained in:
Cameron Pereraandcameronperera authored and GitHub committed 2024-09-13 09:43:08 -05:00
1 parent e395b1196c
commit 9648e1fa3b
8 files changed
+157 -24

No files matched your search

@@ -13,9 +13,7 @@ module('Integration | Helper | time-remaining', function (hooks) {
setupRenderingTest(hooks);
setupIntl(hooks, 'en-us');
test('it calculates the correct time remaining', async function (assert) {
assert.expect(1);
test('it calculates the correct time remainin with less than 24 hours', async function (assert) {
this.set('expirationTime', new Date(Date.now() + 1000 * 60 * 60 * 24));
await render(hbs`{{time-remaining this.expirationTime}}`);
@@ -23,9 +21,29 @@ module('Integration | Helper | time-remaining', function (hooks) {
assert.strictEqual(this.element.textContent.trim(), '23:59:59 remaining');
});
test('it handles negative time remaining', async function (assert) {
assert.expect(1);
test('it calculates the correct time remaining with more than 24 hours', async function (assert) {
this.set('expirationTime', new Date(Date.now() + 1001 * 60 * 60 * 24));
await render(hbs`{{time-remaining this.expirationTime}}`);
assert.strictEqual(
this.element.textContent.trim(),
'1 day, 0:01:26 remaining',
);
});
test('it calculates the correct time remaining with more than a week', async function (assert) {
this.set('expirationTime', new Date(Date.now() + 1001 * 60 * 60 * 24 * 8));
await render(hbs`{{time-remaining this.expirationTime}}`);
assert.strictEqual(
this.element.textContent.trim(),
'1 wk, 1 day, 0:11:31 remaining',
);
});
test('it handles negative time remaining', async function (assert) {
this.set('expirationTime', new Date(Date.now() - 1000 * 60 * 60 * 24));
await render(hbs`{{time-remaining this.expirationTime}}`);
@@ -0,0 +1,79 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { secondsToDuration } from 'core/utils/seconds-to-duration';
import { module, test } from 'qunit';
module('Unit | Utility | seconds-to-duration', function () {
test('it handles 0 correctly', function (assert) {
const result = secondsToDuration(0);
assert.strictEqual(result.seconds, 0);
assert.strictEqual(result.minutes, 0);
assert.strictEqual(result.hours, 0);
assert.strictEqual(result.days, 0);
assert.strictEqual(result.weeks, 0);
});
test('it converts seconds correctly', function (assert) {
const result = secondsToDuration(45);
assert.strictEqual(result.seconds, 45);
assert.strictEqual(result.minutes, 0);
assert.strictEqual(result.hours, 0);
assert.strictEqual(result.days, 0);
assert.strictEqual(result.weeks, 0);
});
test('it converts minutes correctly', function (assert) {
const result = secondsToDuration(100);
assert.strictEqual(result.seconds, 40);
assert.strictEqual(result.minutes, 1);
assert.strictEqual(result.hours, 0);
assert.strictEqual(result.days, 0);
assert.strictEqual(result.weeks, 0);
});
test('it converts hours correctly', function (assert) {
const result = secondsToDuration(5000);
assert.strictEqual(result.seconds, 20);
assert.strictEqual(result.minutes, 23);
assert.strictEqual(result.hours, 1);
assert.strictEqual(result.days, 0);
assert.strictEqual(result.weeks, 0);
});
test('it converts days correctly', function (assert) {
const result = secondsToDuration(500000);
assert.strictEqual(result.seconds, 20);
assert.strictEqual(result.minutes, 53);
assert.strictEqual(result.hours, 18);
assert.strictEqual(result.days, 5);
assert.strictEqual(result.weeks, 0);
});
test('it converts weeks correctly', function (assert) {
const result = secondsToDuration(800000);
assert.strictEqual(result.seconds, 20);
assert.strictEqual(result.minutes, 13);
assert.strictEqual(result.hours, 6);
assert.strictEqual(result.days, 2);
assert.strictEqual(result.weeks, 1);
});
test('it handles negative values', function (assert) {
const result = secondsToDuration(-800000);
assert.strictEqual(result.seconds, 0);
assert.strictEqual(result.minutes, 0);
assert.strictEqual(result.hours, 0);
assert.strictEqual(result.days, 0);
assert.strictEqual(result.weeks, 0);
});
});