summaryrefslogtreecommitdiff
path: root/spec/frontend/feature_flags/store/index/mutations_spec.js
blob: b9354196c68beef21d6e956dc8a58f0d103748ff (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { mapToScopesViewModel } from '~/feature_flags/store/helpers';
import * as types from '~/feature_flags/store/index/mutation_types';
import mutations from '~/feature_flags/store/index/mutations';
import state from '~/feature_flags/store/index/state';
import { parseIntPagination, normalizeHeaders } from '~/lib/utils/common_utils';
import { getRequestData, rotateData, featureFlag } from '../../mock_data';

describe('Feature flags store Mutations', () => {
  let stateCopy;

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

  describe('SET_FEATURE_FLAGS_OPTIONS', () => {
    it('should set provided options', () => {
      mutations[types.SET_FEATURE_FLAGS_OPTIONS](stateCopy, { page: '1', scope: 'all' });

      expect(stateCopy.options).toEqual({ page: '1', scope: 'all' });
    });
  });
  describe('REQUEST_FEATURE_FLAGS', () => {
    it('should set isLoading to true', () => {
      mutations[types.REQUEST_FEATURE_FLAGS](stateCopy);

      expect(stateCopy.isLoading).toEqual(true);
    });
  });

  describe('RECEIVE_FEATURE_FLAGS_SUCCESS', () => {
    const headers = {
      'x-next-page': '2',
      'x-page': '1',
      'X-Per-Page': '2',
      'X-Prev-Page': '',
      'X-TOTAL': '37',
      'X-Total-Pages': '5',
    };

    beforeEach(() => {
      mutations[types.RECEIVE_FEATURE_FLAGS_SUCCESS](stateCopy, { data: getRequestData, headers });
    });

    it('should set isLoading to false', () => {
      expect(stateCopy.isLoading).toEqual(false);
    });

    it('should set hasError to false', () => {
      expect(stateCopy.hasError).toEqual(false);
    });

    it('should set featureFlags with the transformed data', () => {
      const expected = getRequestData.feature_flags.map((flag) => ({
        ...flag,
        scopes: mapToScopesViewModel(flag.scopes || []),
      }));

      expect(stateCopy.featureFlags).toEqual(expected);
    });

    it('should set count with the given data', () => {
      expect(stateCopy.count).toEqual(37);
    });

    it('should set pagination', () => {
      expect(stateCopy.pageInfo).toEqual(parseIntPagination(normalizeHeaders(headers)));
    });
  });

  describe('RECEIVE_FEATURE_FLAGS_ERROR', () => {
    beforeEach(() => {
      mutations[types.RECEIVE_FEATURE_FLAGS_ERROR](stateCopy);
    });

    it('should set isLoading to false', () => {
      expect(stateCopy.isLoading).toEqual(false);
    });

    it('should set hasError to true', () => {
      expect(stateCopy.hasError).toEqual(true);
    });
  });

  describe('REQUEST_ROTATE_INSTANCE_ID', () => {
    beforeEach(() => {
      mutations[types.REQUEST_ROTATE_INSTANCE_ID](stateCopy);
    });

    it('should set isRotating to true', () => {
      expect(stateCopy.isRotating).toBe(true);
    });

    it('should set hasRotateError to false', () => {
      expect(stateCopy.hasRotateError).toBe(false);
    });
  });

  describe('RECEIVE_ROTATE_INSTANCE_ID_SUCCESS', () => {
    beforeEach(() => {
      mutations[types.RECEIVE_ROTATE_INSTANCE_ID_SUCCESS](stateCopy, { data: rotateData });
    });

    it('should set the instance id to the received data', () => {
      expect(stateCopy.instanceId).toBe(rotateData.token);
    });

    it('should set isRotating to false', () => {
      expect(stateCopy.isRotating).toBe(false);
    });

    it('should set hasRotateError to false', () => {
      expect(stateCopy.hasRotateError).toBe(false);
    });
  });

  describe('RECEIVE_ROTATE_INSTANCE_ID_ERROR', () => {
    beforeEach(() => {
      mutations[types.RECEIVE_ROTATE_INSTANCE_ID_ERROR](stateCopy);
    });

    it('should set isRotating to false', () => {
      expect(stateCopy.isRotating).toBe(false);
    });

    it('should set hasRotateError to true', () => {
      expect(stateCopy.hasRotateError).toBe(true);
    });
  });

  describe('UPDATE_FEATURE_FLAG', () => {
    beforeEach(() => {
      stateCopy.featureFlags = getRequestData.feature_flags.map((flag) => ({
        ...flag,
        scopes: mapToScopesViewModel(flag.scopes || []),
      }));
      stateCopy.count = { featureFlags: 1, userLists: 0 };

      mutations[types.UPDATE_FEATURE_FLAG](stateCopy, {
        ...featureFlag,
        scopes: mapToScopesViewModel(featureFlag.scopes || []),
        active: false,
      });
    });

    it('should update the flag with the matching ID', () => {
      expect(stateCopy.featureFlags).toEqual([
        {
          ...featureFlag,
          scopes: mapToScopesViewModel(featureFlag.scopes || []),
          active: false,
        },
      ]);
    });
  });

  describe('RECEIVE_UPDATE_FEATURE_FLAG_SUCCESS', () => {
    const runUpdate = (stateCount, flagState, featureFlagUpdateParams) => {
      stateCopy.featureFlags = getRequestData.feature_flags.map((flag) => ({
        ...flag,
        ...flagState,
        scopes: mapToScopesViewModel(flag.scopes || []),
      }));
      stateCopy.count = stateCount;

      mutations[types.RECEIVE_UPDATE_FEATURE_FLAG_SUCCESS](stateCopy, {
        ...featureFlag,
        ...featureFlagUpdateParams,
      });
    };

    it('updates the flag with the matching ID', () => {
      runUpdate({ all: 1, enabled: 1, disabled: 0 }, { active: true }, { active: false });

      expect(stateCopy.featureFlags).toEqual([
        {
          ...featureFlag,
          scopes: mapToScopesViewModel(featureFlag.scopes || []),
          active: false,
        },
      ]);
    });
  });

  describe('RECEIVE_UPDATE_FEATURE_FLAG_ERROR', () => {
    beforeEach(() => {
      stateCopy.featureFlags = getRequestData.feature_flags.map((flag) => ({
        ...flag,
        scopes: mapToScopesViewModel(flag.scopes || []),
      }));
      mutations[types.RECEIVE_UPDATE_FEATURE_FLAG_ERROR](stateCopy, featureFlag.id);
    });

    it('should update the flag with the matching ID, toggling active', () => {
      expect(stateCopy.featureFlags).toEqual([
        {
          ...featureFlag,
          scopes: mapToScopesViewModel(featureFlag.scopes || []),
          active: false,
        },
      ]);
    });
  });

  describe('RECEIVE_CLEAR_ALERT', () => {
    it('clears the alert', () => {
      stateCopy.alerts = ['a server error'];

      mutations[types.RECEIVE_CLEAR_ALERT](stateCopy, 0);

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

    it('clears the alert at the specified index', () => {
      stateCopy.alerts = ['a server error', 'another error', 'final error'];

      mutations[types.RECEIVE_CLEAR_ALERT](stateCopy, 1);

      expect(stateCopy.alerts).toEqual(['a server error', 'final error']);
    });
  });
});