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

     1  /* eslint-disable jest/no-conditional-expect */
     2  /**
     3   * Copyright 2021 The Vitess Authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  import { formatTimeseriesMap, RATES_INTERVAL, RATES_MAX_SPAN } from './tabletDebugVars';
    19  
    20  /*
    21   * These tests are (at the time of writing) one of the few places we
    22   * use snapshot testing (or "golden tests"), since hand-writing
    23   * timeseries data is tedious, and `formatTimeseriesMap`'s implementation
    24   * is unlikely to change very often.
    25   */
    26  describe('formatTimeseriesMap', () => {
    27      const tests: {
    28          name: string;
    29          input: Parameters<typeof formatTimeseriesMap>;
    30      }[] = [
    31          {
    32              name: 'empty input',
    33              input: [{}],
    34          },
    35          {
    36              name: 'timespan < max span',
    37              input: [
    38                  {
    39                      All: [1, 2, 3],
    40                  },
    41              ],
    42          },
    43          {
    44              name: 'multiple series',
    45              input: [
    46                  {
    47                      All: [3, 4, 5],
    48                      First: [1, 2, 3],
    49                      Second: [4, 5, 6],
    50                  },
    51              ],
    52          },
    53      ];
    54  
    55      test.each(tests.map(Object.values))('%s', (name: string, input: Parameters<typeof formatTimeseriesMap>) => {
    56          Date.now = jest.fn(() => 1000000000000);
    57  
    58          const result = formatTimeseriesMap(...input);
    59  
    60          // Who wants to define an `expected` array of 180 items inline?
    61          // This is an ideal situation for snapshot testing.
    62          expect(result).toMatchSnapshot({}, name);
    63  
    64          // We can trust our snapshots... but additional validation doesn't hurt. B-)
    65          const [inputData, endAt] = input;
    66          const endAtExpected = typeof endAt === 'number' ? endAt : Date.now();
    67          const startAtExpected = endAtExpected - ((179 * 60) / 5) * 1000;
    68  
    69          const inputKeys = Object.keys(inputData);
    70          const outputKeys = Object.keys(result);
    71  
    72          if (inputKeys.length) {
    73              expect(outputKeys).toEqual(inputKeys);
    74          } else {
    75              expect(outputKeys).toEqual(['All']);
    76          }
    77  
    78          Object.entries(result).forEach(([seriesName, seriesData]) => {
    79              expect(seriesData.length).toEqual(RATES_MAX_SPAN / RATES_INTERVAL);
    80              expect(seriesData[0].x).toEqual(startAtExpected);
    81              expect(seriesData[seriesData.length - 1].x).toEqual(endAtExpected);
    82          });
    83      });
    84  });