summaryrefslogtreecommitdiff
path: root/spec/frontend/releases/components/releases_pagination_spec.js
blob: b8c69b0ea70b4b51ba1ceff0606814be52de5f57 (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
import { GlKeysetPagination } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import Vue from 'vue';
import Vuex from 'vuex';
import { historyPushState } from '~/lib/utils/common_utils';
import ReleasesPagination from '~/releases/components/releases_pagination.vue';
import createStore from '~/releases/stores';
import createIndexModule from '~/releases/stores/modules/index';

jest.mock('~/lib/utils/common_utils', () => ({
  ...jest.requireActual('~/lib/utils/common_utils'),
  historyPushState: jest.fn(),
}));

Vue.use(Vuex);

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

  const cursors = {
    startCursor: 'startCursor',
    endCursor: 'endCursor',
  };

  const projectPath = 'my/project';

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

    indexModule.state.pageInfo = pageInfo;

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

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

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

  const findGlKeysetPagination = () => wrapper.findComponent(GlKeysetPagination);
  const findPrevButton = () => findGlKeysetPagination().find('[data-testid="prevButton"]');
  const findNextButton = () => findGlKeysetPagination().find('[data-testid="nextButton"]');

  const expectDisabledPrev = () => {
    expect(findPrevButton().attributes().disabled).toBe('disabled');
  };
  const expectEnabledPrev = () => {
    expect(findPrevButton().attributes().disabled).toBe(undefined);
  };
  const expectDisabledNext = () => {
    expect(findNextButton().attributes().disabled).toBe('disabled');
  };
  const expectEnabledNext = () => {
    expect(findNextButton().attributes().disabled).toBe(undefined);
  };

  describe('when there is only one page of results', () => {
    beforeEach(() => {
      createComponent({
        hasPreviousPage: false,
        hasNextPage: false,
      });
    });

    it('does not render a GlKeysetPagination', () => {
      expect(findGlKeysetPagination().exists()).toBe(false);
    });
  });

  describe('when there is a next page, but not a previous page', () => {
    beforeEach(() => {
      createComponent({
        hasPreviousPage: false,
        hasNextPage: true,
      });
    });

    it('renders a disabled "Prev" button', () => {
      expectDisabledPrev();
    });

    it('renders an enabled "Next" button', () => {
      expectEnabledNext();
    });
  });

  describe('when there is a previous page, but not a next page', () => {
    beforeEach(() => {
      createComponent({
        hasPreviousPage: true,
        hasNextPage: false,
      });
    });

    it('renders a enabled "Prev" button', () => {
      expectEnabledPrev();
    });

    it('renders an disabled "Next" button', () => {
      expectDisabledNext();
    });
  });

  describe('when there is both a previous page and a next page', () => {
    beforeEach(() => {
      createComponent({
        hasPreviousPage: true,
        hasNextPage: true,
      });
    });

    it('renders a enabled "Prev" button', () => {
      expectEnabledPrev();
    });

    it('renders an enabled "Next" button', () => {
      expectEnabledNext();
    });
  });

  describe('button behavior', () => {
    beforeEach(() => {
      createComponent({
        hasPreviousPage: true,
        hasNextPage: true,
        ...cursors,
      });
    });

    describe('next button behavior', () => {
      beforeEach(() => {
        findNextButton().trigger('click');
      });

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

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

    describe('previous button behavior', () => {
      beforeEach(() => {
        findPrevButton().trigger('click');
      });

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

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