github.com/hernad/nomad@v1.6.112/ui/tests/unit/utils/format-duration-test.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  import { module, test } from 'qunit';
     7  import formatDuration from 'nomad-ui/utils/format-duration';
     8  
     9  module('Unit | Util | formatDuration', function () {
    10    test('When all units have values, all units are displayed', function (assert) {
    11      const expectation =
    12        '39 years 1 month 13 days 23h 31m 30s 987ms 654µs 400ns';
    13      assert.equal(formatDuration(1234567890987654321), expectation, expectation);
    14    });
    15  
    16    test('Any unit without values gets dropped from the display', function (assert) {
    17      const expectation = '14 days 6h 56m 890ms 980µs';
    18      assert.equal(formatDuration(1234560890980000), expectation, expectation);
    19    });
    20  
    21    test('The units option allows for units coarser than nanoseconds', function (assert) {
    22      const expectation1 = '1s 200ms';
    23      const expectation2 = '20m';
    24      const expectation3 = '1 month 1 day';
    25      assert.equal(formatDuration(1200, 'ms'), expectation1, expectation1);
    26      assert.equal(formatDuration(1200, 's'), expectation2, expectation2);
    27      assert.equal(formatDuration(32, 'd'), expectation3, expectation3);
    28    });
    29  
    30    test('When duration is 0, 0 is shown in terms of the units provided to the function', function (assert) {
    31      assert.equal(formatDuration(0), '0ns', 'formatDuration(0) -> 0ns');
    32      assert.equal(
    33        formatDuration(0, 'year'),
    34        '0 years',
    35        'formatDuration(0, "year") -> 0 years'
    36      );
    37    });
    38  
    39    test('The longForm option expands suffixes to words', function (assert) {
    40      const expectation1 = '3 seconds 20ms';
    41      const expectation2 = '5 hours 59 minutes';
    42      assert.equal(formatDuration(3020, 'ms', true), expectation1, expectation1);
    43      assert.equal(
    44        formatDuration(60 * 5 + 59, 'm', true),
    45        expectation2,
    46        expectation2
    47      );
    48    });
    49  });