Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ci_resources_page_spec.js « pages « components « catalog « ci « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6fb5eed0d934e987f86a632809f7cb27f4060dc7 (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import Vue from 'vue';
import VueApollo from 'vue-apollo';

import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import createMockApollo from 'helpers/mock_apollo_helper';
import { createAlert } from '~/alert';

import CatalogHeader from '~/ci/catalog/components/list/catalog_header.vue';
import CiResourcesList from '~/ci/catalog/components/list/ci_resources_list.vue';
import CiResourcesPage from '~/ci/catalog/components/pages/ci_resources_page.vue';
import CatalogSearch from '~/ci/catalog/components/list/catalog_search.vue';
import CatalogTabs from '~/ci/catalog/components/list/catalog_tabs.vue';
import CatalogListSkeletonLoader from '~/ci/catalog/components/list/catalog_list_skeleton_loader.vue';
import EmptyState from '~/ci/catalog/components/list/empty_state.vue';

import { cacheConfig, resolvers } from '~/ci/catalog/graphql/settings';
import { DEFAULT_SORT_VALUE, SCOPE } from '~/ci/catalog/constants';
import typeDefs from '~/ci/catalog/graphql/typedefs.graphql';

import getCatalogResources from '~/ci/catalog/graphql/queries/get_ci_catalog_resources.query.graphql';
import getCatalogResourcesCount from '~/ci/catalog/graphql/queries/get_ci_catalog_resources_count.query.graphql';

import {
  emptyCatalogResponseBody,
  catalogResponseBody,
  catalogResourcesCountResponseBody,
} from '../../mock';

Vue.use(VueApollo);
jest.mock('~/alert');

describe('CiResourcesPage', () => {
  let wrapper;
  let catalogResourcesResponse;
  let catalogResourcesCountResponse;

  const defaultQueryVariables = {
    first: 20,
    scope: SCOPE.all,
    searchTerm: null,
    sortValue: DEFAULT_SORT_VALUE,
  };

  const createComponent = () => {
    const handlers = [
      [getCatalogResources, catalogResourcesResponse],
      [getCatalogResourcesCount, catalogResourcesCountResponse],
    ];
    const mockApollo = createMockApollo(handlers, resolvers, { cacheConfig, typeDefs });

    wrapper = shallowMountExtended(CiResourcesPage, {
      apolloProvider: mockApollo,
    });

    return waitForPromises();
  };

  const findCatalogHeader = () => wrapper.findComponent(CatalogHeader);
  const findCatalogSearch = () => wrapper.findComponent(CatalogSearch);
  const findCatalogTabs = () => wrapper.findComponent(CatalogTabs);
  const findCiResourcesList = () => wrapper.findComponent(CiResourcesList);
  const findLoadingState = () => wrapper.findComponent(CatalogListSkeletonLoader);
  const findEmptyState = () => wrapper.findComponent(EmptyState);

  beforeEach(() => {
    catalogResourcesResponse = jest.fn();
    catalogResourcesCountResponse = jest.fn();
    catalogResourcesCountResponse.mockResolvedValue(catalogResourcesCountResponseBody);
  });

  describe('when initial queries are loading', () => {
    beforeEach(() => {
      createComponent();
    });

    it('shows a loading icon and no list', () => {
      expect(findLoadingState().exists()).toBe(true);
      expect(findEmptyState().exists()).toBe(false);
      expect(findCiResourcesList().exists()).toBe(false);
    });
  });

  describe('when queries have loaded', () => {
    it('renders the Catalog Header', async () => {
      await createComponent();

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

    describe('and there are no resources', () => {
      beforeEach(async () => {
        catalogResourcesResponse.mockResolvedValue(emptyCatalogResponseBody);

        await createComponent();
      });

      it('renders the empty state', () => {
        expect(findEmptyState().exists()).toBe(true);
      });

      it('renders the search', () => {
        expect(findCatalogSearch().exists()).toBe(true);
      });

      it('renders the tabs', () => {
        expect(findCatalogTabs().exists()).toBe(true);
      });

      it('does not render the list', () => {
        expect(findCiResourcesList().exists()).toBe(false);
      });
    });

    describe('and there are resources', () => {
      const { nodes, pageInfo } = catalogResponseBody.data.ciCatalogResources;

      beforeEach(async () => {
        catalogResourcesResponse.mockResolvedValue(catalogResponseBody);

        await createComponent();
      });

      it('renders the resources list', () => {
        expect(findLoadingState().exists()).toBe(false);
        expect(findEmptyState().exists()).toBe(false);
        expect(findCiResourcesList().exists()).toBe(true);
      });

      it('renders the catalog tabs', () => {
        expect(findCatalogTabs().exists()).toBe(true);
      });

      it('updates the scope after switching tabs', async () => {
        await findCatalogTabs().vm.$emit('setScope', SCOPE.namespaces);

        expect(catalogResourcesResponse).toHaveBeenCalledWith({
          ...defaultQueryVariables,
          scope: SCOPE.namespaces,
        });

        await findCatalogTabs().vm.$emit('setScope', SCOPE.all);

        expect(catalogResourcesResponse).toHaveBeenCalledWith({
          ...defaultQueryVariables,
          scope: SCOPE.all,
        });
      });

      it('passes down props to the resources list', () => {
        expect(findCiResourcesList().props()).toMatchObject({
          currentPage: 1,
          resources: nodes,
          pageInfo,
          totalCount: 0,
        });
      });

      it('renders the search and sort', () => {
        expect(findCatalogSearch().exists()).toBe(true);
      });
    });
  });

  describe('pagination', () => {
    it.each`
      eventName
      ${'onPrevPage'}
      ${'onNextPage'}
    `('refetch query with new params when receiving $eventName', async ({ eventName }) => {
      const { pageInfo } = catalogResponseBody.data.ciCatalogResources;

      catalogResourcesResponse.mockResolvedValue(catalogResponseBody);
      await createComponent();

      expect(catalogResourcesResponse).toHaveBeenCalledTimes(1);

      await findCiResourcesList().vm.$emit(eventName);

      expect(catalogResourcesResponse).toHaveBeenCalledTimes(2);

      if (eventName === 'onNextPage') {
        expect(catalogResourcesResponse.mock.calls[1][0]).toEqual({
          ...defaultQueryVariables,
          after: pageInfo.endCursor,
        });
      } else {
        expect(catalogResourcesResponse.mock.calls[1][0]).toEqual({
          ...defaultQueryVariables,
          before: pageInfo.startCursor,
          last: 20,
          first: null,
          scope: SCOPE.all,
        });
      }
    });
  });

  describe('search and sort', () => {
    describe('on initial load', () => {
      beforeEach(async () => {
        catalogResourcesResponse.mockResolvedValue(catalogResponseBody);
        await createComponent();
      });

      it('calls the query without search or sort', () => {
        expect(catalogResourcesResponse).toHaveBeenCalledTimes(1);
        expect(catalogResourcesResponse.mock.calls[0][0]).toEqual({
          ...defaultQueryVariables,
        });
      });
    });

    describe('when sorting changes', () => {
      const newSort = 'MOST_AWESOME_ASC';

      beforeEach(async () => {
        catalogResourcesResponse.mockResolvedValue(catalogResponseBody);
        await createComponent();
        await findCatalogSearch().vm.$emit('update-sorting', newSort);
      });

      it('passes it to the graphql query', () => {
        expect(catalogResourcesResponse).toHaveBeenCalledTimes(2);
        expect(catalogResourcesResponse.mock.calls[1][0]).toEqual({
          ...defaultQueryVariables,
          sortValue: newSort,
        });
      });
    });

    describe('when search component emits a new search term', () => {
      const newSearch = 'sloths';

      describe('and there are no results', () => {
        beforeEach(async () => {
          catalogResourcesResponse.mockResolvedValue(emptyCatalogResponseBody);
          await createComponent();
        });

        it('renders the empty state and passes down the search query', async () => {
          await findCatalogSearch().vm.$emit('update-search-term', newSearch);
          await waitForPromises();

          expect(findEmptyState().exists()).toBe(true);
          expect(findEmptyState().props().searchTerm).toBe(newSearch);
        });
      });

      describe('and there are results', () => {
        beforeEach(async () => {
          catalogResourcesResponse.mockResolvedValue(catalogResponseBody);
          await createComponent();
          await findCatalogSearch().vm.$emit('update-search-term', newSearch);
        });

        it('passes it to the graphql query', () => {
          expect(catalogResourcesResponse).toHaveBeenCalledTimes(2);
          expect(catalogResourcesResponse.mock.calls[1][0]).toEqual({
            ...defaultQueryVariables,
            searchTerm: newSearch,
          });
        });
      });
    });
  });

  describe('pages count', () => {
    describe('when the fetchMore call succeeds', () => {
      beforeEach(async () => {
        catalogResourcesResponse.mockResolvedValue(catalogResponseBody);

        await createComponent();
      });

      it('increments and drecrements the page count correctly', async () => {
        expect(findCiResourcesList().props().currentPage).toBe(1);

        findCiResourcesList().vm.$emit('onNextPage');
        await waitForPromises();

        expect(findCiResourcesList().props().currentPage).toBe(2);

        await findCiResourcesList().vm.$emit('onPrevPage');
        await waitForPromises();

        expect(findCiResourcesList().props().currentPage).toBe(1);
      });
    });

    describe.each`
      event                   | payload
      ${'update-search-term'} | ${'cat'}
      ${'update-sorting'}     | ${'CREATED_ASC'}
    `('when $event event is emitted', ({ event, payload }) => {
      beforeEach(async () => {
        catalogResourcesResponse.mockResolvedValue(catalogResponseBody);
        await createComponent();
      });

      it('resets the page count', async () => {
        expect(findCiResourcesList().props().currentPage).toBe(1);

        findCiResourcesList().vm.$emit('onNextPage');
        await waitForPromises();

        expect(findCiResourcesList().props().currentPage).toBe(2);

        await findCatalogSearch().vm.$emit(event, payload);
        await waitForPromises();

        expect(findCiResourcesList().props().currentPage).toBe(1);
      });
    });

    describe('when the fetchMore call fails', () => {
      const errorMessage = 'there was an error';

      describe('for next page', () => {
        beforeEach(async () => {
          catalogResourcesResponse.mockResolvedValueOnce(catalogResponseBody);
          catalogResourcesResponse.mockRejectedValue({ message: errorMessage });

          await createComponent();
        });

        it('does not increment the page and calls createAlert', async () => {
          expect(findCiResourcesList().props().currentPage).toBe(1);

          findCiResourcesList().vm.$emit('onNextPage');
          await waitForPromises();

          expect(findCiResourcesList().props().currentPage).toBe(1);
          expect(createAlert).toHaveBeenCalledWith({ message: errorMessage, variant: 'danger' });
        });
      });

      describe('for previous page', () => {
        beforeEach(async () => {
          // Initial query
          catalogResourcesResponse.mockResolvedValueOnce(catalogResponseBody);
          // When clicking on next
          catalogResourcesResponse.mockResolvedValueOnce(catalogResponseBody);
          // when clicking on previous
          catalogResourcesResponse.mockRejectedValue({ message: errorMessage });

          await createComponent();
        });

        it('does not decrement the page and calls createAlert', async () => {
          expect(findCiResourcesList().props().currentPage).toBe(1);

          findCiResourcesList().vm.$emit('onNextPage');
          await waitForPromises();

          expect(findCiResourcesList().props().currentPage).toBe(2);

          findCiResourcesList().vm.$emit('onPrevPage');
          await waitForPromises();

          expect(findCiResourcesList().props().currentPage).toBe(2);
          expect(createAlert).toHaveBeenCalledWith({ message: errorMessage, variant: 'danger' });
        });
      });
    });
  });
});