2019-03-13 00:04:16 +00:00
|
|
|
import { module, test } from 'qunit';
|
2018-05-30 08:03:32 +00:00
|
|
|
import formatDuration from 'nomad-ui/utils/format-duration';
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
module('Unit | Util | formatDuration', function () {
|
|
|
|
test('When all units have values, all units are displayed', function (assert) {
|
2021-12-28 16:08:12 +00:00
|
|
|
const expectation =
|
|
|
|
'39 years 1 month 13 days 23h 31m 30s 987ms 654µs 400ns';
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.equal(formatDuration(1234567890987654321), expectation, expectation);
|
|
|
|
});
|
2018-05-30 08:03:32 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('Any unit without values gets dropped from the display', function (assert) {
|
2019-03-13 00:04:16 +00:00
|
|
|
const expectation = '14 days 6h 56m 890ms 980µs';
|
|
|
|
assert.equal(formatDuration(1234560890980000), expectation, expectation);
|
|
|
|
});
|
2018-05-30 08:03:32 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('The units option allows for units coarser than nanoseconds', function (assert) {
|
2019-03-13 00:04:16 +00:00
|
|
|
const expectation1 = '1s 200ms';
|
|
|
|
const expectation2 = '20m';
|
|
|
|
const expectation3 = '1 month 1 day';
|
|
|
|
assert.equal(formatDuration(1200, 'ms'), expectation1, expectation1);
|
|
|
|
assert.equal(formatDuration(1200, 's'), expectation2, expectation2);
|
|
|
|
assert.equal(formatDuration(32, 'd'), expectation3, expectation3);
|
|
|
|
});
|
2018-05-30 08:03:32 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('When duration is 0, 0 is shown in terms of the units provided to the function', function (assert) {
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.equal(formatDuration(0), '0ns', 'formatDuration(0) -> 0ns');
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.equal(
|
|
|
|
formatDuration(0, 'year'),
|
|
|
|
'0 years',
|
|
|
|
'formatDuration(0, "year") -> 0 years'
|
|
|
|
);
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2018-09-25 03:01:42 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('The longForm option expands suffixes to words', function (assert) {
|
2019-03-13 00:04:16 +00:00
|
|
|
const expectation1 = '3 seconds 20ms';
|
|
|
|
const expectation2 = '5 hours 59 minutes';
|
|
|
|
assert.equal(formatDuration(3020, 'ms', true), expectation1, expectation1);
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.equal(
|
|
|
|
formatDuration(60 * 5 + 59, 'm', true),
|
|
|
|
expectation2,
|
|
|
|
expectation2
|
|
|
|
);
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2018-09-25 03:01:42 +00:00
|
|
|
});
|