vitess.io/vitess@v0.16.2/web/vtadmin/src/util/formatBytes.test.ts (about)

     1  /**
     2   * Copyright 2021 The Vitess Authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  import { formatBytes } from './formatBytes';
    17  
    18  describe('formatBytes', () => {
    19      const tests: {
    20          name: string;
    21          params: Parameters<typeof formatBytes>;
    22          expected: string | null;
    23      }[] = [
    24          {
    25              name: 'handles numeric inputs',
    26              params: [1024],
    27              expected: '1 KiB',
    28          },
    29          {
    30              name: 'handles string inputs',
    31              params: ['1024'],
    32              expected: '1 KiB',
    33          },
    34          {
    35              name: 'handles undefined inputs',
    36              params: [undefined],
    37              expected: null,
    38          },
    39          {
    40              name: 'handles null inputs',
    41              params: [null],
    42              expected: null,
    43          },
    44          {
    45              name: 'handles NaN inputs',
    46              params: ['not-a-number'],
    47              expected: null,
    48          },
    49          {
    50              name: 'uses default precision',
    51              params: [1234],
    52              expected: '1.21 KiB',
    53          },
    54          {
    55              name: 'uses units parameter if defined',
    56              params: [1234567890, 'MiB'],
    57              expected: '1,177.38 MiB',
    58          },
    59      ];
    60  
    61      test.each(tests.map(Object.values))(
    62          '%s',
    63          (name: string, params: Parameters<typeof formatBytes>, expected: string | null) => {
    64              expect(formatBytes(...params)).toEqual(expected);
    65          }
    66      );
    67  });