summaryrefslogtreecommitdiff
path: root/spec/frontend/monitoring/store/mutations_spec.js
blob: fdad290a8d6b427a4e35299648b45c20485cdf72 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import mutations from '~/monitoring/stores/mutations';
import * as types from '~/monitoring/stores/mutation_types';
import state from '~/monitoring/stores/state';
import {
  metricsGroupsAPIResponse,
  deploymentData,
  metricsDashboardResponse,
  dashboardGitResponse,
} from '../mock_data';
import { uniqMetricsId } from '~/monitoring/stores/utils';

describe('Monitoring mutations', () => {
  let stateCopy;
  beforeEach(() => {
    stateCopy = state();
  });
  describe('RECEIVE_METRICS_DATA_SUCCESS', () => {
    let groups;
    beforeEach(() => {
      stateCopy.dashboard.panel_groups = [];
      groups = metricsGroupsAPIResponse;
    });
    it('adds a key to the group', () => {
      mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups);
      expect(stateCopy.dashboard.panel_groups[0].key).toBe('system-metrics-kubernetes--0');
    });
    it('normalizes values', () => {
      mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups);
      const expectedLabel = 'Pod average';
      const { label, query_range } = stateCopy.dashboard.panel_groups[0].metrics[0].metrics[0];
      expect(label).toEqual(expectedLabel);
      expect(query_range.length).toBeGreaterThan(0);
    });
    it('contains one group, which it has two panels and one metrics property', () => {
      mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups);
      expect(stateCopy.dashboard.panel_groups).toBeDefined();
      expect(stateCopy.dashboard.panel_groups.length).toEqual(1);
      expect(stateCopy.dashboard.panel_groups[0].panels.length).toEqual(2);
      expect(stateCopy.dashboard.panel_groups[0].panels[0].metrics.length).toEqual(1);
      expect(stateCopy.dashboard.panel_groups[0].panels[1].metrics.length).toEqual(1);
    });
    it('assigns queries a metric id', () => {
      mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups);
      expect(stateCopy.dashboard.panel_groups[0].metrics[0].queries[0].metricId).toEqual(
        '17_system_metrics_kubernetes_container_memory_average',
      );
    });
    describe('dashboard endpoint', () => {
      const dashboardGroups = metricsDashboardResponse.dashboard.panel_groups;
      it('aliases group panels to metrics for backwards compatibility', () => {
        mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, dashboardGroups);
        expect(stateCopy.dashboard.panel_groups[0].metrics[0]).toBeDefined();
      });
      it('aliases panel metrics to queries for backwards compatibility', () => {
        mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, dashboardGroups);
        expect(stateCopy.dashboard.panel_groups[0].metrics[0].queries).toBeDefined();
      });
    });
  });

  describe('RECEIVE_DEPLOYMENTS_DATA_SUCCESS', () => {
    it('stores the deployment data', () => {
      stateCopy.deploymentData = [];
      mutations[types.RECEIVE_DEPLOYMENTS_DATA_SUCCESS](stateCopy, deploymentData);
      expect(stateCopy.deploymentData).toBeDefined();
      expect(stateCopy.deploymentData.length).toEqual(3);
      expect(typeof stateCopy.deploymentData[0]).toEqual('object');
    });
  });
  describe('SET_ENDPOINTS', () => {
    it('should set all the endpoints', () => {
      mutations[types.SET_ENDPOINTS](stateCopy, {
        metricsEndpoint: 'additional_metrics.json',
        environmentsEndpoint: 'environments.json',
        deploymentsEndpoint: 'deployments.json',
        dashboardEndpoint: 'dashboard.json',
        projectPath: '/gitlab-org/gitlab-foss',
      });
      expect(stateCopy.metricsEndpoint).toEqual('additional_metrics.json');
      expect(stateCopy.environmentsEndpoint).toEqual('environments.json');
      expect(stateCopy.deploymentsEndpoint).toEqual('deployments.json');
      expect(stateCopy.dashboardEndpoint).toEqual('dashboard.json');
      expect(stateCopy.projectPath).toEqual('/gitlab-org/gitlab-foss');
    });
  });
  describe('SET_QUERY_RESULT', () => {
    const metricId = 12;
    const id = 'system_metrics_kubernetes_container_memory_total';
    const result = [
      {
        values: [[0, 1], [1, 1], [1, 3]],
      },
    ];
    beforeEach(() => {
      const dashboardGroups = metricsDashboardResponse.dashboard.panel_groups;
      mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, dashboardGroups);
    });
    it('clears empty state', () => {
      mutations[types.SET_QUERY_RESULT](stateCopy, {
        metricId,
        result,
      });
      expect(stateCopy.showEmptyState).toBe(false);
    });
    it('sets metricsWithData value', () => {
      const uniqId = uniqMetricsId({
        metric_id: metricId,
        id,
      });
      mutations[types.SET_QUERY_RESULT](stateCopy, {
        metricId: uniqId,
        result,
      });
      expect(stateCopy.metricsWithData).toEqual([uniqId]);
    });
    it('does not store empty results', () => {
      mutations[types.SET_QUERY_RESULT](stateCopy, {
        metricId,
        result: [],
      });
      expect(stateCopy.metricsWithData).toEqual([]);
    });
  });
  describe('SET_ALL_DASHBOARDS', () => {
    it('stores `undefined` dashboards as an empty array', () => {
      mutations[types.SET_ALL_DASHBOARDS](stateCopy, undefined);

      expect(stateCopy.allDashboards).toEqual([]);
    });

    it('stores `null` dashboards as an empty array', () => {
      mutations[types.SET_ALL_DASHBOARDS](stateCopy, null);

      expect(stateCopy.allDashboards).toEqual([]);
    });

    it('stores dashboards loaded from the git repository', () => {
      mutations[types.SET_ALL_DASHBOARDS](stateCopy, dashboardGitResponse);
      expect(stateCopy.allDashboards).toEqual(dashboardGitResponse);
    });
  });
});