summaryrefslogtreecommitdiff
path: root/spec/javascripts/monitoring/store/utils_spec.js
blob: 98388ac19f802bb08c7da9f363a1c377298a9cc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { groupQueriesByChartInfo, normalizeMetric, uniqMetricsId } from '~/monitoring/stores/utils';

describe('groupQueriesByChartInfo', () => {
  let input;
  let output;

  it('groups metrics with the same chart title and y_axis label', () => {
    input = [
      { title: 'title', y_label: 'MB', queries: [{}] },
      { title: 'title', y_label: 'MB', queries: [{}] },
      { title: 'new title', y_label: 'MB', queries: [{}] },
    ];

    output = [
      {
        title: 'title',
        y_label: 'MB',
        queries: [{ metricId: null }, { metricId: null }],
      },
      { title: 'new title', y_label: 'MB', queries: [{ metricId: null }] },
    ];

    expect(groupQueriesByChartInfo(input)).toEqual(output);
  });

  // Functionality associated with the /additional_metrics endpoint
  it("associates a chart's stringified metric_id with the metric", () => {
    input = [{ id: 3, title: 'new title', y_label: 'MB', queries: [{}] }];
    output = [{ id: 3, title: 'new title', y_label: 'MB', queries: [{ metricId: '3' }] }];

    expect(groupQueriesByChartInfo(input)).toEqual(output);
  });

  // Functionality associated with the /metrics_dashboard endpoint
  it('aliases a stringified metrics_id on the metric to the metricId key', () => {
    input = [{ title: 'new title', y_label: 'MB', queries: [{ metric_id: 3 }] }];
    output = [{ title: 'new title', y_label: 'MB', queries: [{ metricId: '3', metric_id: 3 }] }];

    expect(groupQueriesByChartInfo(input)).toEqual(output);
  });
});

describe('normalizeMetric', () => {
  [
    { args: [], expected: 'undefined_undefined' },
    { args: [undefined], expected: 'undefined_undefined' },
    { args: [{ id: 'something' }], expected: 'undefined_something' },
    { args: [{ id: 45 }], expected: 'undefined_45' },
    { args: [{ metric_id: 5 }], expected: '5_undefined' },
    { args: [{ metric_id: 'something' }], expected: 'something_undefined' },
    {
      args: [{ metric_id: 5, id: 'system_metrics_kubernetes_container_memory_total' }],
      expected: '5_system_metrics_kubernetes_container_memory_total',
    },
  ].forEach(({ args, expected }) => {
    it(`normalizes metric to "${expected}" with args=${JSON.stringify(args)}`, () => {
      expect(normalizeMetric(...args)).toEqual({ metric_id: expected });
    });
  });
});

describe('uniqMetricsId', () => {
  [
    { input: { id: 1 }, expected: 'undefined_1' },
    { input: { metric_id: 2 }, expected: '2_undefined' },
    { input: { metric_id: 2, id: 21 }, expected: '2_21' },
    { input: { metric_id: 22, id: 1 }, expected: '22_1' },
    { input: { metric_id: 'aaa', id: '_a' }, expected: 'aaa__a' },
  ].forEach(({ input, expected }) => {
    it(`creates unique metric ID with ${JSON.stringify(input)}`, () => {
      expect(uniqMetricsId(input)).toEqual(expected);
    });
  });
});