summaryrefslogtreecommitdiff
path: root/spec/frontend/packages_and_registries/infrastructure_registry/components/list/stores/actions_spec.js
blob: d596f2dae333df714eedc3934c2df96f189b590e (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
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import testAction from 'helpers/vuex_action_helper';
import Api from '~/api';
import createFlash from '~/flash';
import { MISSING_DELETE_PATH_ERROR } from '~/packages_and_registries/infrastructure_registry/list/constants';
import * as actions from '~/packages_and_registries/infrastructure_registry/list/stores/actions';
import * as types from '~/packages_and_registries/infrastructure_registry/list/stores/mutation_types';
import { DELETE_PACKAGE_ERROR_MESSAGE } from '~/packages_and_registries/shared/constants';

jest.mock('~/flash.js');
jest.mock('~/api.js');

describe('Actions Package list store', () => {
  const headers = 'bar';
  let mock;

  beforeEach(() => {
    Api.projectPackages = jest.fn().mockResolvedValue({ data: 'foo', headers });
    Api.groupPackages = jest.fn().mockResolvedValue({ data: 'baz', headers });
    mock = new MockAdapter(axios);
  });

  afterEach(() => {
    mock.restore();
  });

  describe('requestPackagesList', () => {
    const sorting = {
      sort: 'asc',
      orderBy: 'version',
    };

    const filter = [];
    it('should fetch the project packages list when isGroupPage is false', async () => {
      await testAction(
        actions.requestPackagesList,
        undefined,
        { config: { isGroupPage: false, resourceId: 1 }, sorting, filter },
        [],
        [
          { type: 'setLoading', payload: true },
          { type: 'receivePackagesListSuccess', payload: { data: 'foo', headers } },
          { type: 'setLoading', payload: false },
        ],
      );
      expect(Api.projectPackages).toHaveBeenCalledWith(1, {
        params: { page: 1, per_page: 20, sort: sorting.sort, order_by: sorting.orderBy },
      });
    });

    it('should fetch the group packages list when  isGroupPage is true', async () => {
      await testAction(
        actions.requestPackagesList,
        undefined,
        { config: { isGroupPage: true, resourceId: 2 }, sorting, filter },
        [],
        [
          { type: 'setLoading', payload: true },
          { type: 'receivePackagesListSuccess', payload: { data: 'baz', headers } },
          { type: 'setLoading', payload: false },
        ],
      );
      expect(Api.groupPackages).toHaveBeenCalledWith(2, {
        params: { page: 1, per_page: 20, sort: sorting.sort, order_by: sorting.orderBy },
      });
    });

    it('should fetch packages of a certain type when a filter with a type is present', async () => {
      const packageType = 'maven';

      await testAction(
        actions.requestPackagesList,
        undefined,
        {
          config: { isGroupPage: false, resourceId: 1 },
          sorting,
          filter: [{ type: 'type', value: { data: 'maven' } }],
        },
        [],
        [
          { type: 'setLoading', payload: true },
          { type: 'receivePackagesListSuccess', payload: { data: 'foo', headers } },
          { type: 'setLoading', payload: false },
        ],
      );
      expect(Api.projectPackages).toHaveBeenCalledWith(1, {
        params: {
          page: 1,
          per_page: 20,
          sort: sorting.sort,
          order_by: sorting.orderBy,
          package_type: packageType,
        },
      });
    });

    it('should create flash on API error', async () => {
      Api.projectPackages = jest.fn().mockRejectedValue();
      await testAction(
        actions.requestPackagesList,
        undefined,
        { config: { isGroupPage: false, resourceId: 2 }, sorting, filter },
        [],
        [
          { type: 'setLoading', payload: true },
          { type: 'setLoading', payload: false },
        ],
      );
      expect(createFlash).toHaveBeenCalled();
    });

    it('should force the terraform_module type when forceTerraform is true', async () => {
      await testAction(
        actions.requestPackagesList,
        undefined,
        { config: { isGroupPage: false, resourceId: 1, forceTerraform: true }, sorting, filter },
        [],
        [
          { type: 'setLoading', payload: true },
          { type: 'receivePackagesListSuccess', payload: { data: 'foo', headers } },
          { type: 'setLoading', payload: false },
        ],
      );
      expect(Api.projectPackages).toHaveBeenCalledWith(1, {
        params: {
          page: 1,
          per_page: 20,
          sort: sorting.sort,
          order_by: sorting.orderBy,
          package_type: 'terraform_module',
        },
      });
    });
  });

  describe('receivePackagesListSuccess', () => {
    it('should set received packages', () => {
      const data = 'foo';

      return testAction(
        actions.receivePackagesListSuccess,
        { data, headers },
        null,
        [
          { type: types.SET_PACKAGE_LIST_SUCCESS, payload: data },
          { type: types.SET_PAGINATION, payload: headers },
        ],
        [],
      );
    });
  });

  describe('setInitialState', () => {
    it('should commit setInitialState', () => {
      return testAction(
        actions.setInitialState,
        '1',
        null,
        [{ type: types.SET_INITIAL_STATE, payload: '1' }],
        [],
      );
    });
  });

  describe('setLoading', () => {
    it('should commit set main loading', () => {
      return testAction(
        actions.setLoading,
        true,
        null,
        [{ type: types.SET_MAIN_LOADING, payload: true }],
        [],
      );
    });
  });

  describe('requestDeletePackage', () => {
    const payload = {
      _links: {
        delete_api_path: 'foo',
      },
    };
    it('should perform a delete operation on _links.delete_api_path', () => {
      mock.onDelete(payload._links.delete_api_path).replyOnce(200);
      Api.projectPackages = jest.fn().mockResolvedValue({ data: 'foo' });

      return testAction(
        actions.requestDeletePackage,
        payload,
        { pagination: { page: 1 } },
        [],
        [
          { type: 'setLoading', payload: true },
          { type: 'requestPackagesList', payload: { page: 1 } },
        ],
      );
    });

    it('should stop the loading and call create flash on api error', async () => {
      mock.onDelete(payload._links.delete_api_path).replyOnce(400);
      await testAction(
        actions.requestDeletePackage,
        payload,
        null,
        [],
        [
          { type: 'setLoading', payload: true },
          { type: 'setLoading', payload: false },
        ],
      );
      expect(createFlash).toHaveBeenCalled();
    });

    it.each`
      property             | actionPayload
      ${'_links'}          | ${{}}
      ${'delete_api_path'} | ${{ _links: {} }}
    `('should reject and createFlash when $property is missing', ({ actionPayload }) => {
      return testAction(actions.requestDeletePackage, actionPayload, null, [], []).catch((e) => {
        expect(e).toEqual(new Error(MISSING_DELETE_PATH_ERROR));
        expect(createFlash).toHaveBeenCalledWith({
          message: DELETE_PACKAGE_ERROR_MESSAGE,
        });
      });
    });
  });

  describe('setSorting', () => {
    it('should commit SET_SORTING', () => {
      return testAction(
        actions.setSorting,
        'foo',
        null,
        [{ type: types.SET_SORTING, payload: 'foo' }],
        [],
      );
    });
  });

  describe('setFilter', () => {
    it('should commit SET_FILTER', () => {
      return testAction(
        actions.setFilter,
        'foo',
        null,
        [{ type: types.SET_FILTER, payload: 'foo' }],
        [],
      );
    });
  });
});