summaryrefslogtreecommitdiff
path: root/spec/frontend/runner/components/runner_pagination_spec.js
blob: 59feb32dd2acd577c8e34fbc9ae77f319fe150b3 (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
import { GlPagination } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import RunnerPagination from '~/runner/components/runner_pagination.vue';

const mockStartCursor = 'START_CURSOR';
const mockEndCursor = 'END_CURSOR';

describe('RunnerPagination', () => {
  let wrapper;

  const findPagination = () => wrapper.findComponent(GlPagination);

  const createComponent = ({ page = 1, hasPreviousPage = false, hasNextPage = true } = {}) => {
    wrapper = mount(RunnerPagination, {
      propsData: {
        value: {
          page,
        },
        pageInfo: {
          hasPreviousPage,
          hasNextPage,
          startCursor: mockStartCursor,
          endCursor: mockEndCursor,
        },
      },
    });
  };

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

  describe('When on the first page', () => {
    beforeEach(() => {
      createComponent({
        page: 1,
        hasPreviousPage: false,
        hasNextPage: true,
      });
    });

    it('Contains the current page information', () => {
      expect(findPagination().props('value')).toBe(1);
      expect(findPagination().props('prevPage')).toBe(null);
      expect(findPagination().props('nextPage')).toBe(2);
    });

    it('Shows prev page disabled', () => {
      expect(findPagination().find('[aria-disabled]').text()).toBe('Prev');
    });

    it('Shows next page link', () => {
      expect(findPagination().find('a').text()).toBe('Next');
    });

    it('Goes to the second page', () => {
      findPagination().vm.$emit('input', 2);

      expect(wrapper.emitted('input')[0]).toEqual([
        {
          after: mockEndCursor,
          page: 2,
        },
      ]);
    });
  });

  describe('When in between pages', () => {
    beforeEach(() => {
      createComponent({
        page: 2,
        hasPreviousPage: true,
        hasNextPage: true,
      });
    });

    it('Contains the current page information', () => {
      expect(findPagination().props('value')).toBe(2);
      expect(findPagination().props('prevPage')).toBe(1);
      expect(findPagination().props('nextPage')).toBe(3);
    });

    it('Shows the next and previous pages', () => {
      const links = findPagination().findAll('a');

      expect(links).toHaveLength(2);
      expect(links.at(0).text()).toBe('Prev');
      expect(links.at(1).text()).toBe('Next');
    });

    it('Goes to the last page', () => {
      findPagination().vm.$emit('input', 3);

      expect(wrapper.emitted('input')[0]).toEqual([
        {
          after: mockEndCursor,
          page: 3,
        },
      ]);
    });

    it('Goes to the first page', () => {
      findPagination().vm.$emit('input', 1);

      expect(wrapper.emitted('input')[0]).toEqual([
        {
          before: mockStartCursor,
          page: 1,
        },
      ]);
    });
  });

  describe('When in the last page', () => {
    beforeEach(() => {
      createComponent({
        page: 3,
        hasPreviousPage: true,
        hasNextPage: false,
      });
    });

    it('Contains the current page', () => {
      expect(findPagination().props('value')).toBe(3);
      expect(findPagination().props('prevPage')).toBe(2);
      expect(findPagination().props('nextPage')).toBe(null);
    });

    it('Shows next page link', () => {
      expect(findPagination().find('a').text()).toBe('Prev');
    });

    it('Shows next page disabled', () => {
      expect(findPagination().find('[aria-disabled]').text()).toBe('Next');
    });
  });

  describe('When only one page', () => {
    beforeEach(() => {
      createComponent({
        page: 1,
        hasPreviousPage: false,
        hasNextPage: false,
      });
    });

    it('does not display pagination', () => {
      expect(wrapper.html()).toBe('');
    });

    it('Contains the current page', () => {
      expect(findPagination().props('value')).toBe(1);
    });

    it('Shows no more page buttons', () => {
      expect(findPagination().props('prevPage')).toBe(null);
      expect(findPagination().props('nextPage')).toBe(null);
    });
  });
});