mutations_spec.js 4.89 KB
Newer Older
1 2 3
import mutations from '~/monitoring/stores/mutations';
import * as types from '~/monitoring/stores/mutation_types';
import state from '~/monitoring/stores/state';
4 5 6 7 8 9
import {
  metricsGroupsAPIResponse,
  deploymentData,
  metricsDashboardResponse,
  dashboardGitResponse,
} from '../mock_data';
10
import { uniqMetricsId } from '~/monitoring/stores/utils';
11 12 13 14 15 16

describe('Monitoring mutations', () => {
  let stateCopy;
  beforeEach(() => {
    stateCopy = state();
  });
17
  describe('RECEIVE_METRICS_DATA_SUCCESS', () => {
18
    let groups;
19
    beforeEach(() => {
20
      stateCopy.dashboard.panel_groups = [];
21
      groups = metricsGroupsAPIResponse;
22
    });
23 24
    it('adds a key to the group', () => {
      mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups);
25
      expect(stateCopy.dashboard.panel_groups[0].key).toBe('system-metrics-kubernetes--0');
26
    });
27
    it('normalizes values', () => {
28
      mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups);
29
      const expectedLabel = 'Pod average';
30
      const { label, query_range } = stateCopy.dashboard.panel_groups[0].panels[0].metrics[0];
31 32
      expect(label).toEqual(expectedLabel);
      expect(query_range.length).toBeGreaterThan(0);
33
    });
34
    it('contains one group, which it has two panels and one metrics property', () => {
35
      mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups);
36
      expect(stateCopy.dashboard.panel_groups).toBeDefined();
37 38 39 40
      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);
41
    });
42
    it('assigns metrics a metric id', () => {
43
      mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, groups);
44
      expect(stateCopy.dashboard.panel_groups[0].panels[0].metrics[0].metricId).toEqual(
45 46
        '17_system_metrics_kubernetes_container_memory_average',
      );
47 48 49
    });
  });

50
  describe('RECEIVE_DEPLOYMENTS_DATA_SUCCESS', () => {
51 52 53 54 55 56 57 58 59 60 61 62 63 64
    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',
65
        dashboardEndpoint: 'dashboard.json',
66
        projectPath: '/gitlab-org/gitlab-foss',
67 68 69 70
      });
      expect(stateCopy.metricsEndpoint).toEqual('additional_metrics.json');
      expect(stateCopy.environmentsEndpoint).toEqual('environments.json');
      expect(stateCopy.deploymentsEndpoint).toEqual('deployments.json');
71
      expect(stateCopy.dashboardEndpoint).toEqual('dashboard.json');
72
      expect(stateCopy.projectPath).toEqual('/gitlab-org/gitlab-foss');
73 74
    });
  });
75 76
  describe('SET_QUERY_RESULT', () => {
    const metricId = 12;
77
    const id = 'system_metrics_kubernetes_container_memory_total';
78 79 80 81 82
    const result = [
      {
        values: [[0, 1], [1, 1], [1, 3]],
      },
    ];
83 84 85 86 87 88 89 90 91 92 93 94
    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', () => {
95 96 97 98
      const uniqId = uniqMetricsId({
        metric_id: metricId,
        id,
      });
99
      mutations[types.SET_QUERY_RESULT](stateCopy, {
100
        metricId: uniqId,
101 102
        result,
      });
103
      expect(stateCopy.metricsWithData).toEqual([uniqId]);
104 105 106 107 108 109 110 111 112
    });
    it('does not store empty results', () => {
      mutations[types.SET_QUERY_RESULT](stateCopy, {
        metricId,
        result: [],
      });
      expect(stateCopy.metricsWithData).toEqual([]);
    });
  });
113
  describe('SET_ALL_DASHBOARDS', () => {
114 115 116 117 118 119 120 121 122 123 124 125 126
    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', () => {
127 128 129 130
      mutations[types.SET_ALL_DASHBOARDS](stateCopy, dashboardGitResponse);
      expect(stateCopy.allDashboards).toEqual(dashboardGitResponse);
    });
  });
131
});