summaryrefslogtreecommitdiff
path: root/spec/frontend/logs/stores/mutations_spec.js
blob: dcb358c7d5b281b7ff2f38446d55950e6f1862b2 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import mutations from '~/logs/stores/mutations';
import * as types from '~/logs/stores/mutation_types';

import logsPageState from '~/logs/stores/state';
import {
  mockEnvName,
  mockEnvironments,
  mockPods,
  mockPodName,
  mockLogsResult,
  mockSearch,
} from '../mock_data';

describe('Logs Store Mutations', () => {
  let state;

  beforeEach(() => {
    state = logsPageState();
  });

  it('ensures mutation types are correctly named', () => {
    Object.keys(types).forEach(k => {
      expect(k).toEqual(types[k]);
    });
  });

  describe('SET_PROJECT_ENVIRONMENT', () => {
    it('sets the environment', () => {
      mutations[types.SET_PROJECT_ENVIRONMENT](state, mockEnvName);
      expect(state.environments.current).toEqual(mockEnvName);
    });
  });

  describe('SET_SEARCH', () => {
    it('sets the search', () => {
      mutations[types.SET_SEARCH](state, mockSearch);
      expect(state.search).toEqual(mockSearch);
    });
  });

  describe('REQUEST_ENVIRONMENTS_DATA', () => {
    it('inits data', () => {
      mutations[types.REQUEST_ENVIRONMENTS_DATA](state);
      expect(state.environments.options).toEqual([]);
      expect(state.environments.isLoading).toEqual(true);
    });
  });

  describe('RECEIVE_ENVIRONMENTS_DATA_SUCCESS', () => {
    it('receives environments data and stores it as options', () => {
      expect(state.environments.options).toEqual([]);

      mutations[types.RECEIVE_ENVIRONMENTS_DATA_SUCCESS](state, mockEnvironments);

      expect(state.environments.options).toEqual(mockEnvironments);
      expect(state.environments.isLoading).toEqual(false);
    });
  });

  describe('RECEIVE_ENVIRONMENTS_DATA_ERROR', () => {
    it('captures an error loading environments', () => {
      mutations[types.RECEIVE_ENVIRONMENTS_DATA_ERROR](state);

      expect(state.environments).toEqual({
        options: [],
        isLoading: false,
        current: null,
      });
    });
  });

  describe('REQUEST_LOGS_DATA', () => {
    it('starts loading for logs', () => {
      mutations[types.REQUEST_LOGS_DATA](state);

      expect(state.logs).toEqual(
        expect.objectContaining({
          lines: [],
          isLoading: true,
          isComplete: false,
        }),
      );
    });
  });

  describe('RECEIVE_LOGS_DATA_SUCCESS', () => {
    it('receives logs lines', () => {
      mutations[types.RECEIVE_LOGS_DATA_SUCCESS](state, mockLogsResult);

      expect(state.logs).toEqual(
        expect.objectContaining({
          lines: mockLogsResult,
          isLoading: false,
          isComplete: true,
        }),
      );
    });
  });

  describe('RECEIVE_LOGS_DATA_ERROR', () => {
    it('receives log data error and stops loading', () => {
      mutations[types.RECEIVE_LOGS_DATA_ERROR](state);

      expect(state.logs).toEqual(
        expect.objectContaining({
          lines: [],
          isLoading: false,
          isComplete: true,
        }),
      );
    });
  });

  describe('SET_CURRENT_POD_NAME', () => {
    it('set current pod name', () => {
      mutations[types.SET_CURRENT_POD_NAME](state, mockPodName);

      expect(state.pods.current).toEqual(mockPodName);
    });
  });

  describe('SET_TIME_RANGE', () => {
    it('sets a default range', () => {
      expect(state.timeRange.current).toEqual(expect.any(Object));
    });

    it('sets a time range', () => {
      const mockRange = {
        start: '2020-01-10T18:00:00.000Z',
        end: '2020-01-10T10:00:00.000Z',
      };
      mutations[types.SET_TIME_RANGE](state, mockRange);

      expect(state.timeRange.current).toEqual(mockRange);
    });
  });

  describe('REQUEST_PODS_DATA', () => {
    it('receives log data error and stops loading', () => {
      mutations[types.REQUEST_PODS_DATA](state);

      expect(state.pods).toEqual(
        expect.objectContaining({
          options: [],
        }),
      );
    });
  });
  describe('RECEIVE_PODS_DATA_SUCCESS', () => {
    it('receives pods data success', () => {
      mutations[types.RECEIVE_PODS_DATA_SUCCESS](state, mockPods);

      expect(state.pods).toEqual(
        expect.objectContaining({
          options: mockPods,
        }),
      );
    });
  });
  describe('RECEIVE_PODS_DATA_ERROR', () => {
    it('receives pods data error', () => {
      mutations[types.RECEIVE_PODS_DATA_ERROR](state);

      expect(state.pods).toEqual(
        expect.objectContaining({
          options: [],
        }),
      );
    });
  });
});