summaryrefslogtreecommitdiff
path: root/spec/frontend/registry/explorer/pages/list_spec.js
blob: b24422adb033d98c94fa46f79bdde83e4d30a277 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { shallowMount } from '@vue/test-utils';
import { GlSkeletonLoader, GlSprintf, GlAlert, GlSearchBoxByClick } from '@gitlab/ui';
import waitForPromises from 'helpers/wait_for_promises';
import Tracking from '~/tracking';
import component from '~/registry/explorer/pages/list.vue';
import CliCommands from '~/registry/explorer/components/list_page/cli_commands.vue';
import GroupEmptyState from '~/registry/explorer/components/list_page/group_empty_state.vue';
import ProjectEmptyState from '~/registry/explorer/components/list_page/project_empty_state.vue';
import RegistryHeader from '~/registry/explorer/components/list_page/registry_header.vue';
import ImageList from '~/registry/explorer/components/list_page/image_list.vue';
import TitleArea from '~/vue_shared/components/registry/title_area.vue';
import { createStore } from '~/registry/explorer/stores/';
import {
  SET_MAIN_LOADING,
  SET_IMAGES_LIST_SUCCESS,
  SET_PAGINATION,
  SET_INITIAL_STATE,
} from '~/registry/explorer/stores/mutation_types';
import {
  DELETE_IMAGE_SUCCESS_MESSAGE,
  DELETE_IMAGE_ERROR_MESSAGE,
  IMAGE_REPOSITORY_LIST_LABEL,
  SEARCH_PLACEHOLDER_TEXT,
} from '~/registry/explorer/constants';
import { imagesListResponse } from '../mock_data';
import { GlModal, GlEmptyState } from '../stubs';
import { $toast } from '../../shared/mocks';

describe('List Page', () => {
  let wrapper;
  let dispatchSpy;
  let store;

  const findDeleteModal = () => wrapper.find(GlModal);
  const findSkeletonLoader = () => wrapper.find(GlSkeletonLoader);

  const findEmptyState = () => wrapper.find(GlEmptyState);

  const findCliCommands = () => wrapper.find(CliCommands);
  const findProjectEmptyState = () => wrapper.find(ProjectEmptyState);
  const findGroupEmptyState = () => wrapper.find(GroupEmptyState);
  const findRegistryHeader = () => wrapper.find(RegistryHeader);

  const findDeleteAlert = () => wrapper.find(GlAlert);
  const findImageList = () => wrapper.find(ImageList);
  const findListHeader = () => wrapper.find('[data-testid="listHeader"]');
  const findSearchBox = () => wrapper.find(GlSearchBoxByClick);
  const findEmptySearchMessage = () => wrapper.find('[data-testid="emptySearch"]');

  const mountComponent = ({ mocks } = {}) => {
    wrapper = shallowMount(component, {
      store,
      stubs: {
        GlModal,
        GlEmptyState,
        GlSprintf,
        RegistryHeader,
        TitleArea,
      },
      mocks: {
        $toast,
        $route: {
          name: 'foo',
        },
        ...mocks,
      },
    });
  };

  beforeEach(() => {
    store = createStore();
    dispatchSpy = jest.spyOn(store, 'dispatch');
    dispatchSpy.mockResolvedValue();
    store.commit(SET_IMAGES_LIST_SUCCESS, imagesListResponse.data);
    store.commit(SET_PAGINATION, imagesListResponse.headers);
  });

  afterEach(() => {
    wrapper.destroy();
  });

  describe('API calls', () => {
    it.each`
      imageList                  | name         | called
      ${[]}                      | ${'foo'}     | ${['requestImagesList']}
      ${imagesListResponse.data} | ${undefined} | ${['requestImagesList']}
      ${imagesListResponse.data} | ${'foo'}     | ${undefined}
    `(
      'with images equal $imageList and name $name dispatch calls $called',
      ({ imageList, name, called }) => {
        store.commit(SET_IMAGES_LIST_SUCCESS, imageList);
        dispatchSpy.mockClear();
        mountComponent({ mocks: { $route: { name } } });

        expect(dispatchSpy.mock.calls[0]).toEqual(called);
      },
    );
  });

  it('contains registry header', () => {
    mountComponent();
    expect(findRegistryHeader().exists()).toBe(true);
  });

  describe('connection error', () => {
    const config = {
      characterError: true,
      containersErrorImage: 'foo',
      helpPagePath: 'bar',
    };

    beforeEach(() => {
      store.commit(SET_INITIAL_STATE, config);
      mountComponent();
    });

    afterEach(() => {
      store.commit(SET_INITIAL_STATE, {});
    });

    it('should show an empty state', () => {
      expect(findEmptyState().exists()).toBe(true);
    });

    it('empty state should have an svg-path', () => {
      expect(findEmptyState().attributes('svg-path')).toBe(config.containersErrorImage);
    });

    it('empty state should have a description', () => {
      expect(findEmptyState().html()).toContain('connection error');
    });

    it('should not show the loading or default state', () => {
      expect(findSkeletonLoader().exists()).toBe(false);
      expect(findImageList().exists()).toBe(false);
    });
  });

  describe('isLoading is true', () => {
    beforeEach(() => {
      store.commit(SET_MAIN_LOADING, true);
      mountComponent();
    });

    afterEach(() => store.commit(SET_MAIN_LOADING, false));

    it('shows the skeleton loader', () => {
      expect(findSkeletonLoader().exists()).toBe(true);
    });

    it('imagesList is not visible', () => {
      expect(findImageList().exists()).toBe(false);
    });

    it('cli commands is not visible', () => {
      expect(findCliCommands().exists()).toBe(false);
    });
  });

  describe('list is empty', () => {
    beforeEach(() => {
      store.commit(SET_IMAGES_LIST_SUCCESS, []);
      mountComponent();
      return waitForPromises();
    });

    it('cli commands is not visible', () => {
      expect(findCliCommands().exists()).toBe(false);
    });

    it('project empty state is visible', () => {
      expect(findProjectEmptyState().exists()).toBe(true);
    });

    describe('is group page is true', () => {
      beforeEach(() => {
        store.commit(SET_INITIAL_STATE, { isGroupPage: true });
        mountComponent();
      });

      afterEach(() => {
        store.commit(SET_INITIAL_STATE, { isGroupPage: undefined });
      });

      it('group empty state is visible', () => {
        expect(findGroupEmptyState().exists()).toBe(true);
      });

      it('cli commands is not visible', () => {
        expect(findCliCommands().exists()).toBe(false);
      });

      it('list header is not visible', () => {
        expect(findListHeader().exists()).toBe(false);
      });
    });
  });

  describe('list is not empty', () => {
    describe('unfiltered state', () => {
      beforeEach(() => {
        mountComponent();
      });

      it('quick start is visible', () => {
        expect(findCliCommands().exists()).toBe(true);
      });

      it('list component is visible', () => {
        expect(findImageList().exists()).toBe(true);
      });

      it('list header is  visible', () => {
        const header = findListHeader();
        expect(header.exists()).toBe(true);
        expect(header.text()).toBe(IMAGE_REPOSITORY_LIST_LABEL);
      });

      describe('delete image', () => {
        const itemToDelete = { path: 'bar' };
        it('should call deleteItem when confirming deletion', () => {
          dispatchSpy.mockResolvedValue();
          findImageList().vm.$emit('delete', itemToDelete);
          expect(wrapper.vm.itemToDelete).toEqual(itemToDelete);
          findDeleteModal().vm.$emit('ok');
          expect(store.dispatch).toHaveBeenCalledWith(
            'requestDeleteImage',
            wrapper.vm.itemToDelete,
          );
        });

        it('should show a success alert when delete request is successful', () => {
          dispatchSpy.mockResolvedValue();
          findImageList().vm.$emit('delete', itemToDelete);
          expect(wrapper.vm.itemToDelete).toEqual(itemToDelete);
          return wrapper.vm.handleDeleteImage().then(() => {
            const alert = findDeleteAlert();
            expect(alert.exists()).toBe(true);
            expect(alert.text().replace(/\s\s+/gm, ' ')).toBe(
              DELETE_IMAGE_SUCCESS_MESSAGE.replace('%{title}', wrapper.vm.itemToDelete.path),
            );
          });
        });

        it('should show an error alert when delete request fails', () => {
          dispatchSpy.mockRejectedValue();
          findImageList().vm.$emit('delete', itemToDelete);
          expect(wrapper.vm.itemToDelete).toEqual(itemToDelete);
          return wrapper.vm.handleDeleteImage().then(() => {
            const alert = findDeleteAlert();
            expect(alert.exists()).toBe(true);
            expect(alert.text().replace(/\s\s+/gm, ' ')).toBe(
              DELETE_IMAGE_ERROR_MESSAGE.replace('%{title}', wrapper.vm.itemToDelete.path),
            );
          });
        });
      });
    });

    describe('search', () => {
      it('has a search box element', () => {
        mountComponent();
        const searchBox = findSearchBox();
        expect(searchBox.exists()).toBe(true);
        expect(searchBox.attributes('placeholder')).toBe(SEARCH_PLACEHOLDER_TEXT);
      });

      it('performs a search', () => {
        mountComponent();
        findSearchBox().vm.$emit('submit', 'foo');
        expect(store.dispatch).toHaveBeenCalledWith('requestImagesList', {
          name: 'foo',
        });
      });

      it('when search result is empty displays an empty search message', () => {
        mountComponent();
        store.commit(SET_IMAGES_LIST_SUCCESS, []);
        return wrapper.vm.$nextTick().then(() => {
          expect(findEmptySearchMessage().exists()).toBe(true);
        });
      });
    });

    describe('pagination', () => {
      it('pageChange event triggers the appropriate store function', () => {
        mountComponent();
        findImageList().vm.$emit('pageChange', 2);
        expect(store.dispatch).toHaveBeenCalledWith('requestImagesList', {
          pagination: { page: 2 },
          name: wrapper.vm.search,
        });
      });
    });
  });

  describe('modal', () => {
    beforeEach(() => {
      mountComponent();
    });

    it('exists', () => {
      expect(findDeleteModal().exists()).toBe(true);
    });

    it('contains a description with the path of the item to delete', () => {
      findImageList().vm.$emit('delete', { path: 'foo' });
      return wrapper.vm.$nextTick().then(() => {
        expect(findDeleteModal().html()).toContain('foo');
      });
    });
  });

  describe('tracking', () => {
    beforeEach(() => {
      mountComponent();
    });

    const testTrackingCall = action => {
      expect(Tracking.event).toHaveBeenCalledWith(undefined, action, {
        label: 'registry_repository_delete',
      });
    };

    beforeEach(() => {
      jest.spyOn(Tracking, 'event');
      dispatchSpy.mockResolvedValue();
    });

    it('send an event when delete button is clicked', () => {
      findImageList().vm.$emit('delete', {});
      testTrackingCall('click_button');
    });

    it('send an event when cancel is pressed on modal', () => {
      const deleteModal = findDeleteModal();
      deleteModal.vm.$emit('cancel');
      testTrackingCall('cancel_delete');
    });

    it('send an event when confirm is clicked on modal', () => {
      const deleteModal = findDeleteModal();
      deleteModal.vm.$emit('ok');
      testTrackingCall('confirm_delete');
    });
  });
});