summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/store/utils_spec.js
blob: d322d45457e88a0260d9c184e5d49aaecb22e0f9 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {
  normalizeMetric,
  uniqMetricsId,
  parseEnvironmentsResponse,
  removeLeadingSlash,
} from '~/monitoring/stores/utils';

const projectPath = 'gitlab-org/gitlab-test';

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, metricId: 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);
    });
  });
});

describe('parseEnvironmentsResponse', () => {
  [
    {
      input: null,
      output: [],
    },
    {
      input: undefined,
      output: [],
    },
    {
      input: [],
      output: [],
    },
    {
      input: [
        {
          id: '1',
          name: 'env-1',
        },
      ],
      output: [
        {
          id: 1,
          name: 'env-1',
          metrics_path: `${projectPath}/environments/1/metrics`,
        },
      ],
    },
    {
      input: [
        {
          id: 'gid://gitlab/Environment/12',
          name: 'env-12',
        },
      ],
      output: [
        {
          id: 12,
          name: 'env-12',
          metrics_path: `${projectPath}/environments/12/metrics`,
        },
      ],
    },
  ].forEach(({ input, output }) => {
    it(`parseEnvironmentsResponse returns ${JSON.stringify(output)} with input ${JSON.stringify(
      input,
    )}`, () => {
      expect(parseEnvironmentsResponse(input, projectPath)).toEqual(output);
    });
  });
});

describe('removeLeadingSlash', () => {
  [
    { input: null, output: '' },
    { input: '', output: '' },
    { input: 'gitlab-org', output: 'gitlab-org' },
    { input: 'gitlab-org/gitlab', output: 'gitlab-org/gitlab' },
    { input: '/gitlab-org/gitlab', output: 'gitlab-org/gitlab' },
    { input: '////gitlab-org/gitlab', output: 'gitlab-org/gitlab' },
  ].forEach(({ input, output }) => {
    it(`removeLeadingSlash returns ${output} with input ${input}`, () => {
      expect(removeLeadingSlash(input)).toEqual(output);
    });
  });
});