github.com/hernad/nomad@v1.6.112/ui/tests/unit/utils/units-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 * as units from 'nomad-ui/utils/units';
     8  
     9  function table(fn, cases) {
    10    cases.forEach((testCase) => {
    11      test(testCase.name || testCase.out, function (assert) {
    12        assert.deepEqual(fn.apply(null, testCase.in), testCase.out);
    13      });
    14    });
    15  }
    16  
    17  module('Unit | Util | units#formatBytes', function () {
    18    table.call(this, units.formatBytes, [
    19      { in: [null], out: '0 Bytes', name: 'formats null as 0 bytes' },
    20      { in: [undefined], out: '0 Bytes', name: 'formats undefined as 0 bytes' },
    21      { in: [0], out: '0 Bytes', name: 'formats x < 1024 as bytes' },
    22      { in: [100], out: '100 Bytes' },
    23      { in: [1023], out: '1,023 Bytes' },
    24      { in: [1024], out: '1 KiB', name: 'formats 1024 <= x < 1024^2 as KiB' },
    25      { in: [1024 ** 2 - 1024 * 0.01], out: '1,023.99 KiB' },
    26      {
    27        in: [1024 ** 2],
    28        out: '1 MiB',
    29        name: 'formats 1024^2 <= x < 1024^3 as MiB',
    30      },
    31      { in: [1024 ** 2 * 1.016], out: '1.02 MiB' },
    32      {
    33        in: [1024 ** 3],
    34        out: '1 GiB',
    35        name: 'formats 1024^3 <= x < 1024^4 as GiB',
    36      },
    37      { in: [1024 ** 3 * 512.5], out: '512.5 GiB' },
    38      {
    39        in: [1024 ** 4],
    40        out: '1 TiB',
    41        name: 'formats 1024^4 <= x < 1024^5 as TiB',
    42      },
    43      { in: [1024 ** 4 * 2.1234], out: '2.12 TiB' },
    44      { in: [1024 ** 5], out: '1 PiB', name: 'formats x > 1024^5 as PiB' },
    45      { in: [1024 ** 5 * 4000], out: '4,000 PiB' },
    46      {
    47        in: [1024 ** 2, 'MiB'],
    48        out: '1 TiB',
    49        name: 'accepts a starting unit size as an optional argument',
    50      },
    51      {
    52        in: [1024 ** 2 * -1],
    53        out: '-1 MiB',
    54        name: 'negative values are still reduced',
    55      },
    56    ]);
    57  });
    58  
    59  module('Unit | Util | units#formatScheduledBytes', function () {
    60    table.call(this, units.formatScheduledBytes, [
    61      { in: [null], out: '0 Bytes', name: 'formats null as 0 bytes' },
    62      { in: [undefined], out: '0 Bytes', name: 'formats undefined as 0 bytes' },
    63      { in: [0], out: '0 Bytes', name: 'formats x < 1024 as bytes' },
    64      {
    65        in: [1024 ** 3],
    66        out: '1,024 MiB',
    67        name: 'max unit is MiB, just like Nomad expects in job specs',
    68      },
    69      {
    70        in: [1024 ** 3 + 1024 ** 2 * 0.6],
    71        out: '1,025 MiB',
    72        name: 'MiBs are rounded to whole numbers',
    73      },
    74      {
    75        in: [2000, 'MiB'],
    76        out: '2,000 MiB',
    77        name: 'accepts a starting unit size as an optional argument',
    78      },
    79      {
    80        in: [1024 ** 3 * -1],
    81        out: '-1,024 MiB',
    82        name: 'negative values are still reduced',
    83      },
    84    ]);
    85  });
    86  
    87  module('Unit | Util | units#formatHertz', function () {
    88    table.call(this, units.formatHertz, [
    89      { in: [null], out: '0 Hz', name: 'formats null as 0 Hz' },
    90      { in: [undefined], out: '0 Hz', name: 'formats undefined as 0 Hz' },
    91      { in: [0], out: '0 Hz', name: 'formats x < 1000 as Hz' },
    92      { in: [999], out: '999 Hz' },
    93      { in: [1000], out: '1 KHz', name: 'formats 1000 <= x < 1000^2 as KHz' },
    94      { in: [1000 ** 2 - 10], out: '999.99 KHz' },
    95      {
    96        in: [1000 ** 2],
    97        out: '1 MHz',
    98        name: 'formats 1000^2 <= x < 1000^3 as MHz',
    99      },
   100      { in: [1000 ** 2 * 5.234], out: '5.23 MHz' },
   101      {
   102        in: [1000 ** 3],
   103        out: '1 GHz',
   104        name: 'formats 1000^3 <= x < 1000^4 as GHz',
   105      },
   106      { in: [1000 ** 3 * 500.238], out: '500.24 GHz' },
   107      {
   108        in: [1000 ** 4],
   109        out: '1 THz',
   110        name: 'formats 1000^4 <= x < 1000^5 as THz',
   111      },
   112      { in: [1000 ** 4 * 12], out: '12 THz' },
   113      { in: [1000 ** 5], out: '1 PHz', name: 'formats x > 1000^5 as PHz' },
   114      { in: [1000 ** 5 * 34567.89], out: '34,567.89 PHz' },
   115      {
   116        in: [2000, 'KHz'],
   117        out: '2 MHz',
   118        name: 'accepts a starting unit size as an optional argument',
   119      },
   120      {
   121        in: [1000 ** 3 * -1],
   122        out: '-1 GHz',
   123        name: 'negative values are still reduced',
   124      },
   125    ]);
   126  });
   127  
   128  module('Unit | Util | units#formatScheduledHertz', function () {
   129    table.call(this, units.formatScheduledHertz, [
   130      { in: [null], out: '0 Hz', name: 'formats null as 0 Hz' },
   131      { in: [undefined], out: '0 Hz', name: 'formats undefined as 0 Hz' },
   132      { in: [0], out: '0 Hz', name: 'formats x < 1024 as Hz' },
   133      {
   134        in: [1000 ** 3],
   135        out: '1,000 MHz',
   136        name: 'max unit is MHz, just like Nomad expects in job specs',
   137      },
   138      {
   139        in: [1000 ** 3 + 1000 ** 2 * 0.6],
   140        out: '1,001 MHz',
   141        name: 'MHz are rounded to whole numbers',
   142      },
   143      {
   144        in: [2000, 'MHz'],
   145        out: '2,000 MHz',
   146        name: 'accepts a starting unit size as an optional argument',
   147      },
   148      {
   149        in: [1000 ** 3 * -1],
   150        out: '-1,000 MHz',
   151        name: 'negative values are still reduced',
   152      },
   153    ]);
   154  });
   155  
   156  module('Unit | Util | units#reduceBytes', function () {
   157    table.call(this, units.reduceBytes, [
   158      {
   159        in: [],
   160        out: [0, 'Bytes'],
   161        name: 'No args behavior results in valid output',
   162      },
   163      { in: [1024 ** 6], out: [1024, 'PiB'], name: 'Max default unit is PiB' },
   164      {
   165        in: [1024 ** 6 * 1.12345],
   166        out: [1024 * 1.12345, 'PiB'],
   167        name: 'Returned numbers are not formatted',
   168      },
   169      {
   170        in: [1024 ** 2 * 2, 'KiB'],
   171        out: [2048, 'KiB'],
   172        name: 'Reduction ends when the specified max unit is reached',
   173      },
   174      {
   175        in: [1024 ** 2, 'MiB', 'KiB'],
   176        out: [1024, 'MiB'],
   177        name: 'accepts a starting unit size as an optional argument',
   178      },
   179      {
   180        in: [1024 ** 3 * -1],
   181        out: [-1, 'GiB'],
   182        name: 'negative values are still reduced',
   183      },
   184    ]);
   185  });
   186  
   187  module('Unit | Util | units#reduceHertz', function () {
   188    table.call(this, units.reduceHertz, [
   189      {
   190        in: [],
   191        out: [0, 'Hz'],
   192        name: 'No args behavior results in valid output',
   193      },
   194      { in: [1000 ** 6], out: [1000, 'PHz'], name: 'Max default unit is PHz' },
   195      {
   196        in: [1000 ** 6 * 1.12345],
   197        out: [1000 * 1.12345, 'PHz'],
   198        name: 'Returned numbers are not formatted',
   199      },
   200      {
   201        in: [1000 ** 4 * 2, 'GHz'],
   202        out: [2000, 'GHz'],
   203        name: 'Reduction ends when the specified max unit is reached',
   204      },
   205      {
   206        in: [1000 * 2, 'THz', 'MHz'],
   207        out: [2, 'GHz'],
   208        name: 'accepts a starting unit size as an optional argument',
   209      },
   210      {
   211        in: [1000 ** 3 * -1],
   212        out: [-1, 'GHz'],
   213        name: 'negative values are still reduced',
   214      },
   215    ]);
   216  });