summaryrefslogtreecommitdiff
path: root/spec/frontend/releases/components/releases_pagination_rest_spec.js
blob: 7d45176967bc930c0429b52f8ec844ebf5b12587 (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
import { GlPagination } from '@gitlab/ui';
import { mount, createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';
import * as commonUtils from '~/lib/utils/common_utils';
import ReleasesPaginationRest from '~/releases/components/releases_pagination_rest.vue';
import createStore from '~/releases/stores';
import createIndexModule from '~/releases/stores/modules/index';

commonUtils.historyPushState = jest.fn();

const localVue = createLocalVue();
localVue.use(Vuex);

describe('~/releases/components/releases_pagination_rest.vue', () => {
  let wrapper;
  let indexModule;

  const projectId = 19;

  const createComponent = (pageInfo) => {
    indexModule = createIndexModule({ projectId });

    indexModule.state.restPageInfo = pageInfo;

    indexModule.actions.fetchReleases = jest.fn();

    wrapper = mount(ReleasesPaginationRest, {
      store: createStore({
        modules: {
          index: indexModule,
        },
        featureFlags: {},
      }),
      localVue,
    });
  };

  const findGlPagination = () => wrapper.find(GlPagination);

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

  describe('when a page number is clicked', () => {
    const newPage = 2;

    beforeEach(() => {
      createComponent({
        perPage: 20,
        page: 1,
        total: 40,
        totalPages: 2,
        nextPage: 2,
      });

      findGlPagination().vm.$emit('input', newPage);
    });

    it('calls fetchReleases with the correct page', () => {
      expect(indexModule.actions.fetchReleases.mock.calls).toEqual([
        [expect.anything(), { page: newPage }],
      ]);
    });

    it('calls historyPushState with the new URL', () => {
      expect(commonUtils.historyPushState.mock.calls).toEqual([
        [expect.stringContaining(`?page=${newPage}`)],
      ]);
    });
  });
});