summaryrefslogtreecommitdiff
path: root/spec/frontend/packages_and_registries/container_registry/explorer/components/details_page/tags_list_spec.js
blob: 9a42c82d7e05aa9beb48c31d4de1b636f569ca3a (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
import { GlButton, GlKeysetPagination } from '@gitlab/ui';
import { shallowMount, createLocalVue } from '@vue/test-utils';
import { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import EmptyTagsState from '~/packages_and_registries/container_registry/explorer/components/details_page/empty_state.vue';
import component from '~/packages_and_registries/container_registry/explorer/components/details_page/tags_list.vue';
import TagsListRow from '~/packages_and_registries/container_registry/explorer/components/details_page/tags_list_row.vue';
import TagsLoader from '~/packages_and_registries/container_registry/explorer/components/details_page/tags_loader.vue';
import {
  TAGS_LIST_TITLE,
  REMOVE_TAGS_BUTTON_TITLE,
} from '~/packages_and_registries/container_registry/explorer/constants/index';
import getContainerRepositoryTagsQuery from '~/packages_and_registries/container_registry/explorer/graphql/queries/get_container_repository_tags.query.graphql';
import { tagsMock, imageTagsMock, tagsPageInfo } from '../../mock_data';

const localVue = createLocalVue();

describe('Tags List', () => {
  let wrapper;
  let apolloProvider;
  const tags = [...tagsMock];
  const readOnlyTags = tags.map((t) => ({ ...t, canDelete: false }));

  const findTagsListRow = () => wrapper.findAll(TagsListRow);
  const findDeleteButton = () => wrapper.find(GlButton);
  const findListTitle = () => wrapper.find('[data-testid="list-title"]');
  const findPagination = () => wrapper.find(GlKeysetPagination);
  const findEmptyState = () => wrapper.find(EmptyTagsState);
  const findTagsLoader = () => wrapper.find(TagsLoader);

  const waitForApolloRequestRender = async () => {
    await waitForPromises();
    await nextTick();
  };

  const mountComponent = ({
    propsData = { isMobile: false, id: 1 },
    resolver = jest.fn().mockResolvedValue(imageTagsMock()),
  } = {}) => {
    localVue.use(VueApollo);

    const requestHandlers = [[getContainerRepositoryTagsQuery, resolver]];

    apolloProvider = createMockApollo(requestHandlers);
    wrapper = shallowMount(component, {
      localVue,
      apolloProvider,
      propsData,
      provide() {
        return {
          config: {},
        };
      },
    });
  };

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

  describe('List title', () => {
    it('exists', async () => {
      mountComponent();

      await waitForApolloRequestRender();

      expect(findListTitle().exists()).toBe(true);
    });

    it('has the correct text', async () => {
      mountComponent();

      await waitForApolloRequestRender();

      expect(findListTitle().text()).toBe(TAGS_LIST_TITLE);
    });
  });

  describe('delete button', () => {
    it.each`
      inputTags       | isMobile | isVisible
      ${tags}         | ${false} | ${true}
      ${tags}         | ${true}  | ${false}
      ${readOnlyTags} | ${false} | ${false}
      ${readOnlyTags} | ${true}  | ${false}
    `(
      'is $isVisible that delete button exists when tags is $inputTags and isMobile is $isMobile',
      async ({ inputTags, isMobile, isVisible }) => {
        mountComponent({
          propsData: { tags: inputTags, isMobile, id: 1 },
          resolver: jest.fn().mockResolvedValue(imageTagsMock(inputTags)),
        });

        await waitForApolloRequestRender();

        expect(findDeleteButton().exists()).toBe(isVisible);
      },
    );

    it('has the correct text', async () => {
      mountComponent();

      await waitForApolloRequestRender();

      expect(findDeleteButton().text()).toBe(REMOVE_TAGS_BUTTON_TITLE);
    });

    it('has the correct props', async () => {
      mountComponent();
      await waitForApolloRequestRender();

      expect(findDeleteButton().attributes()).toMatchObject({
        category: 'secondary',
        variant: 'danger',
      });
    });

    it.each`
      disabled | doSelect | buttonDisabled
      ${true}  | ${false} | ${'true'}
      ${true}  | ${true}  | ${'true'}
      ${false} | ${false} | ${'true'}
      ${false} | ${true}  | ${undefined}
    `(
      'is $buttonDisabled that the button is disabled when the component disabled state is $disabled and is $doSelect that the user selected a tag',
      async ({ disabled, buttonDisabled, doSelect }) => {
        mountComponent({ propsData: { tags, disabled, isMobile: false, id: 1 } });

        await waitForApolloRequestRender();

        if (doSelect) {
          findTagsListRow().at(0).vm.$emit('select');
          await nextTick();
        }

        expect(findDeleteButton().attributes('disabled')).toBe(buttonDisabled);
      },
    );

    it('click event emits a deleted event with selected items', async () => {
      mountComponent();

      await waitForApolloRequestRender();

      findTagsListRow().at(0).vm.$emit('select');
      findDeleteButton().vm.$emit('click');

      expect(wrapper.emitted('delete')[0][0][0].name).toBe(tags[0].name);
    });
  });

  describe('list rows', () => {
    it('one row exist for each tag', async () => {
      mountComponent();

      await waitForApolloRequestRender();

      expect(findTagsListRow()).toHaveLength(tags.length);
    });

    it('the correct props are bound to it', async () => {
      mountComponent({ propsData: { disabled: true, id: 1 } });

      await waitForApolloRequestRender();

      const rows = findTagsListRow();

      expect(rows.at(0).attributes()).toMatchObject({
        first: 'true',
        disabled: 'true',
      });
    });

    describe('events', () => {
      it('select event update the selected items', async () => {
        mountComponent();

        await waitForApolloRequestRender();

        findTagsListRow().at(0).vm.$emit('select');

        await nextTick();

        expect(findTagsListRow().at(0).attributes('selected')).toBe('true');
      });

      it('delete event emit a delete event', async () => {
        mountComponent();

        await waitForApolloRequestRender();

        findTagsListRow().at(0).vm.$emit('delete');
        expect(wrapper.emitted('delete')[0][0][0].name).toBe(tags[0].name);
      });
    });
  });

  describe('when the list of tags is empty', () => {
    const resolver = jest.fn().mockResolvedValue(imageTagsMock([]));

    it('has the empty state', async () => {
      mountComponent({ resolver });

      await waitForApolloRequestRender();

      expect(findEmptyState().exists()).toBe(true);
    });

    it('does not show the loader', async () => {
      mountComponent({ resolver });

      await waitForApolloRequestRender();

      expect(findTagsLoader().exists()).toBe(false);
    });

    it('does not show the list', async () => {
      mountComponent({ resolver });

      await waitForApolloRequestRender();

      expect(findTagsListRow().exists()).toBe(false);
      expect(findListTitle().exists()).toBe(false);
    });
  });

  describe('pagination', () => {
    it('exists', async () => {
      mountComponent();

      await waitForApolloRequestRender();

      expect(findPagination().exists()).toBe(true);
    });

    it('is hidden when loading', () => {
      mountComponent();

      expect(findPagination().exists()).toBe(false);
    });

    it('is hidden when there are no more pages', async () => {
      mountComponent({ resolver: jest.fn().mockResolvedValue(imageTagsMock([])) });

      await waitForApolloRequestRender();

      expect(findPagination().exists()).toBe(false);
    });

    it('is wired to the correct pagination props', async () => {
      mountComponent();

      await waitForApolloRequestRender();

      expect(findPagination().props()).toMatchObject({
        hasNextPage: tagsPageInfo.hasNextPage,
        hasPreviousPage: tagsPageInfo.hasPreviousPage,
      });
    });

    it('fetch next page when user clicks next', async () => {
      const resolver = jest.fn().mockResolvedValue(imageTagsMock());
      mountComponent({ resolver });

      await waitForApolloRequestRender();

      findPagination().vm.$emit('next');

      expect(resolver).toHaveBeenCalledWith(
        expect.objectContaining({ after: tagsPageInfo.endCursor }),
      );
    });

    it('fetch previous page when user clicks prev', async () => {
      const resolver = jest.fn().mockResolvedValue(imageTagsMock());
      mountComponent({ resolver });

      await waitForApolloRequestRender();

      findPagination().vm.$emit('prev');

      expect(resolver).toHaveBeenCalledWith(
        expect.objectContaining({ first: null, before: tagsPageInfo.startCursor }),
      );
    });
  });

  describe('loading state', () => {
    it.each`
      isImageLoading | queryExecuting | loadingVisible
      ${true}        | ${true}        | ${true}
      ${true}        | ${false}       | ${true}
      ${false}       | ${true}        | ${true}
      ${false}       | ${false}       | ${false}
    `(
      'when the isImageLoading is $isImageLoading, and is $queryExecuting that the query is still executing is $loadingVisible that the loader is shown',
      async ({ isImageLoading, queryExecuting, loadingVisible }) => {
        mountComponent({ propsData: { isImageLoading, isMobile: false, id: 1 } });

        if (!queryExecuting) {
          await waitForApolloRequestRender();
        }

        expect(findTagsLoader().exists()).toBe(loadingVisible);
        expect(findTagsListRow().exists()).toBe(!loadingVisible);
        expect(findListTitle().exists()).toBe(!loadingVisible);
        expect(findPagination().exists()).toBe(!loadingVisible);
      },
    );
  });
});