summaryrefslogtreecommitdiff
path: root/spec/frontend/milestones/stores/actions_spec.js
blob: a62b0c49a806e06e972714f343ecae4e26791761 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import testAction from 'helpers/vuex_action_helper';
import createState from '~/milestones/stores/state';
import * as actions from '~/milestones/stores/actions';
import * as types from '~/milestones/stores/mutation_types';

let mockProjectMilestonesReturnValue;
let mockGroupMilestonesReturnValue;
let mockProjectSearchReturnValue;

jest.mock('~/api', () => ({
  // `__esModule: true` is required when mocking modules with default exports:
  // https://jestjs.io/docs/en/jest-object#jestmockmodulename-factory-options
  __esModule: true,
  default: {
    projectMilestones: () => mockProjectMilestonesReturnValue,
    projectSearch: () => mockProjectSearchReturnValue,
    groupMilestones: () => mockGroupMilestonesReturnValue,
  },
}));

describe('Milestone combobox Vuex store actions', () => {
  let state;

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

  describe('setProjectId', () => {
    it(`commits ${types.SET_PROJECT_ID} with the new project ID`, () => {
      const projectId = '4';
      testAction(actions.setProjectId, projectId, state, [
        { type: types.SET_PROJECT_ID, payload: projectId },
      ]);
    });
  });

  describe('setGroupId', () => {
    it(`commits ${types.SET_GROUP_ID} with the new group ID`, () => {
      const groupId = '123';
      testAction(actions.setGroupId, groupId, state, [
        { type: types.SET_GROUP_ID, payload: groupId },
      ]);
    });
  });

  describe('setGroupMilestonesAvailable', () => {
    it(`commits ${types.SET_GROUP_MILESTONES_AVAILABLE} with the boolean indicating if group milestones are available (Premium)`, () => {
      state.groupMilestonesAvailable = true;
      testAction(actions.setGroupMilestonesAvailable, state.groupMilestonesAvailable, state, [
        { type: types.SET_GROUP_MILESTONES_AVAILABLE, payload: state.groupMilestonesAvailable },
      ]);
    });
  });

  describe('setSelectedMilestones', () => {
    it(`commits ${types.SET_SELECTED_MILESTONES} with the new selected milestones name`, () => {
      const selectedMilestones = ['v1.2.3'];
      testAction(actions.setSelectedMilestones, selectedMilestones, state, [
        { type: types.SET_SELECTED_MILESTONES, payload: selectedMilestones },
      ]);
    });
  });

  describe('clearSelectedMilestones', () => {
    it(`commits ${types.CLEAR_SELECTED_MILESTONES} with the new selected milestones name`, () => {
      testAction(actions.clearSelectedMilestones, null, state, [
        { type: types.CLEAR_SELECTED_MILESTONES },
      ]);
    });
  });

  describe('toggleMilestones', () => {
    const selectedMilestone = 'v1.2.3';
    it(`commits ${types.ADD_SELECTED_MILESTONE} with the new selected milestone name`, () => {
      testAction(actions.toggleMilestones, selectedMilestone, state, [
        { type: types.ADD_SELECTED_MILESTONE, payload: selectedMilestone },
      ]);
    });

    it(`commits ${types.REMOVE_SELECTED_MILESTONE} with the new selected milestone name`, () => {
      state.selectedMilestones = [selectedMilestone];
      testAction(actions.toggleMilestones, selectedMilestone, state, [
        { type: types.REMOVE_SELECTED_MILESTONE, payload: selectedMilestone },
      ]);
    });
  });

  describe('search', () => {
    describe('when project has license to add group milestones', () => {
      it(`commits ${types.SET_SEARCH_QUERY} with the new search query to search for project and group milestones`, () => {
        const getters = {
          groupMilestonesEnabled: () => true,
        };

        const searchQuery = 'v1.0';
        testAction(
          actions.search,
          searchQuery,
          { ...state, ...getters },
          [{ type: types.SET_SEARCH_QUERY, payload: searchQuery }],
          [{ type: 'searchProjectMilestones' }, { type: 'searchGroupMilestones' }],
        );
      });
    });

    describe('when project does not have license to add group milestones', () => {
      it(`commits ${types.SET_SEARCH_QUERY} with the new search query to search for project milestones`, () => {
        const searchQuery = 'v1.0';
        testAction(
          actions.search,
          searchQuery,
          state,
          [{ type: types.SET_SEARCH_QUERY, payload: searchQuery }],
          [{ type: 'searchProjectMilestones' }],
        );
      });
    });
  });

  describe('searchProjectMilestones', () => {
    describe('when the search is successful', () => {
      const projectSearchApiResponse = { data: [{ title: 'v1.0' }] };

      beforeEach(() => {
        mockProjectSearchReturnValue = Promise.resolve(projectSearchApiResponse);
      });

      it(`commits ${types.REQUEST_START}, ${types.RECEIVE_PROJECT_MILESTONES_SUCCESS} with the response from the API, and ${types.REQUEST_FINISH}`, () => {
        return testAction(actions.searchProjectMilestones, undefined, state, [
          { type: types.REQUEST_START },
          { type: types.RECEIVE_PROJECT_MILESTONES_SUCCESS, payload: projectSearchApiResponse },
          { type: types.REQUEST_FINISH },
        ]);
      });
    });

    describe('when the search fails', () => {
      const error = new Error('Something went wrong!');

      beforeEach(() => {
        mockProjectSearchReturnValue = Promise.reject(error);
      });

      it(`commits ${types.REQUEST_START}, ${types.RECEIVE_PROJECT_MILESTONES_ERROR} with the error object, and ${types.REQUEST_FINISH}`, () => {
        return testAction(actions.searchProjectMilestones, undefined, state, [
          { type: types.REQUEST_START },
          { type: types.RECEIVE_PROJECT_MILESTONES_ERROR, payload: error },
          { type: types.REQUEST_FINISH },
        ]);
      });
    });
  });

  describe('searchGroupMilestones', () => {
    describe('when the search is successful', () => {
      const groupSearchApiResponse = { data: [{ title: 'group-v1.0' }] };

      beforeEach(() => {
        mockGroupMilestonesReturnValue = Promise.resolve(groupSearchApiResponse);
      });

      it(`commits ${types.REQUEST_START}, ${types.RECEIVE_GROUP_MILESTONES_SUCCESS} with the response from the API, and ${types.REQUEST_FINISH}`, () => {
        return testAction(actions.searchGroupMilestones, undefined, state, [
          { type: types.REQUEST_START },
          { type: types.RECEIVE_GROUP_MILESTONES_SUCCESS, payload: groupSearchApiResponse },
          { type: types.REQUEST_FINISH },
        ]);
      });
    });

    describe('when the search fails', () => {
      const error = new Error('Something went wrong!');

      beforeEach(() => {
        mockGroupMilestonesReturnValue = Promise.reject(error);
      });

      it(`commits ${types.REQUEST_START}, ${types.RECEIVE_GROUP_MILESTONES_ERROR} with the error object, and ${types.REQUEST_FINISH}`, () => {
        return testAction(actions.searchGroupMilestones, undefined, state, [
          { type: types.REQUEST_START },
          { type: types.RECEIVE_GROUP_MILESTONES_ERROR, payload: error },
          { type: types.REQUEST_FINISH },
        ]);
      });
    });
  });

  describe('fetchMilestones', () => {
    describe('when project has license to add group milestones', () => {
      it(`dispatchs fetchProjectMilestones and fetchGroupMilestones`, () => {
        const getters = {
          groupMilestonesEnabled: () => true,
        };

        testAction(
          actions.fetchMilestones,
          undefined,
          { ...state, ...getters },
          [],
          [{ type: 'fetchProjectMilestones' }, { type: 'fetchGroupMilestones' }],
        );
      });
    });

    describe('when project does not have license to add group milestones', () => {
      it(`dispatchs fetchProjectMilestones`, () => {
        testAction(
          actions.fetchMilestones,
          undefined,
          state,
          [],
          [{ type: 'fetchProjectMilestones' }],
        );
      });
    });
  });

  describe('fetchProjectMilestones', () => {
    describe('when the fetch is successful', () => {
      const projectMilestonesApiResponse = { data: [{ title: 'v1.0' }] };

      beforeEach(() => {
        mockProjectMilestonesReturnValue = Promise.resolve(projectMilestonesApiResponse);
      });

      it(`commits ${types.REQUEST_START}, ${types.RECEIVE_PROJECT_MILESTONES_SUCCESS} with the response from the API, and ${types.REQUEST_FINISH}`, () => {
        return testAction(actions.fetchProjectMilestones, undefined, state, [
          { type: types.REQUEST_START },
          { type: types.RECEIVE_PROJECT_MILESTONES_SUCCESS, payload: projectMilestonesApiResponse },
          { type: types.REQUEST_FINISH },
        ]);
      });
    });

    describe('when the fetch fails', () => {
      const error = new Error('Something went wrong!');

      beforeEach(() => {
        mockProjectMilestonesReturnValue = Promise.reject(error);
      });

      it(`commits ${types.REQUEST_START}, ${types.RECEIVE_PROJECT_MILESTONES_ERROR} with the error object, and ${types.REQUEST_FINISH}`, () => {
        return testAction(actions.fetchProjectMilestones, undefined, state, [
          { type: types.REQUEST_START },
          { type: types.RECEIVE_PROJECT_MILESTONES_ERROR, payload: error },
          { type: types.REQUEST_FINISH },
        ]);
      });
    });
  });

  describe('fetchGroupMilestones', () => {
    describe('when the fetch is successful', () => {
      const groupMilestonesApiResponse = { data: [{ title: 'group-v1.0' }] };

      beforeEach(() => {
        mockGroupMilestonesReturnValue = Promise.resolve(groupMilestonesApiResponse);
      });

      it(`commits ${types.REQUEST_START}, ${types.RECEIVE_GROUP_MILESTONES_SUCCESS} with the response from the API, and ${types.REQUEST_FINISH}`, () => {
        return testAction(actions.fetchGroupMilestones, undefined, state, [
          { type: types.REQUEST_START },
          { type: types.RECEIVE_GROUP_MILESTONES_SUCCESS, payload: groupMilestonesApiResponse },
          { type: types.REQUEST_FINISH },
        ]);
      });
    });

    describe('when the fetch fails', () => {
      const error = new Error('Something went wrong!');

      beforeEach(() => {
        mockGroupMilestonesReturnValue = Promise.reject(error);
      });

      it(`commits ${types.REQUEST_START}, ${types.RECEIVE_GROUP_MILESTONES_ERROR} with the error object, and ${types.REQUEST_FINISH}`, () => {
        return testAction(actions.fetchGroupMilestones, undefined, state, [
          { type: types.REQUEST_START },
          { type: types.RECEIVE_GROUP_MILESTONES_ERROR, payload: error },
          { type: types.REQUEST_FINISH },
        ]);
      });
    });
  });
});