summaryrefslogtreecommitdiff
path: root/spec/frontend/environments/environment_details/pagination_spec.js
blob: 107f3c3dd5e48611641d143d95b9d706e8038cb0 (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
import { GlKeysetPagination } from '@gitlab/ui';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import Pagination from '~/environments/environment_details/pagination.vue';

describe('~/environments/environment_details/pagniation.vue', () => {
  const mockRouter = {
    push: jest.fn(),
  };

  const pageInfo = {
    startCursor: 'eyJpZCI6IjE2In0',
    endCursor: 'eyJpZCI6IjIifQ',
    hasNextPage: true,
    hasPreviousPage: true,
  };
  let wrapper;

  const createWrapper = (pageInfoProp) => {
    return mountExtended(Pagination, {
      propsData: {
        pageInfo: pageInfoProp,
      },
      mocks: {
        $router: mockRouter,
      },
    });
  };

  describe('when neither next nor previous page exists', () => {
    beforeEach(() => {
      const emptyPageInfo = { ...pageInfo, hasPreviousPage: false, hasNextPage: false };
      wrapper = createWrapper(emptyPageInfo);
    });

    it('should not render pagination component', () => {
      expect(wrapper.html()).toBe('');
    });
  });

  describe('when Pagination is rendered for environment details page', () => {
    beforeEach(() => {
      wrapper = createWrapper(pageInfo);
    });

    it('should pass correct props to keyset pagination', () => {
      const glPagination = wrapper.findComponent(GlKeysetPagination);
      expect(glPagination.exists()).toBe(true);
      expect(glPagination.props()).toEqual(expect.objectContaining(pageInfo));
    });

    describe.each([
      {
        testPageInfo: pageInfo,
        expectedAfter: `after=${pageInfo.endCursor}`,
        expectedBefore: `before=${pageInfo.startCursor}`,
      },
      {
        testPageInfo: { ...pageInfo, hasNextPage: true, hasPreviousPage: false },
        expectedAfter: `after=${pageInfo.endCursor}`,
        expectedBefore: '',
      },
      {
        testPageInfo: { ...pageInfo, hasNextPage: false, hasPreviousPage: true },
        expectedAfter: '',
        expectedBefore: `before=${pageInfo.startCursor}`,
      },
    ])(
      'button links generation for $testPageInfo',
      ({ testPageInfo, expectedAfter, expectedBefore }) => {
        beforeEach(() => {
          wrapper = createWrapper(testPageInfo);
        });

        it(`should have button links defined as ${expectedAfter || 'empty'} and
        ${expectedBefore || 'empty'}`, () => {
          const glPagination = wrapper.findComponent(GlKeysetPagination);
          expect(glPagination.props().prevButtonLink).toContain(expectedBefore);
          expect(glPagination.props().nextButtonLink).toContain(expectedAfter);
        });
      },
    );

    describe.each([
      {
        clickEvent: {
          shiftKey: false,
          ctrlKey: false,
          altKey: false,
          metaKey: false,
        },
        isDefaultPrevented: true,
      },
      {
        clickEvent: {
          shiftKey: true,
          ctrlKey: false,
          altKey: false,
          metaKey: false,
        },
        isDefaultPrevented: false,
      },
      {
        clickEvent: {
          shiftKey: false,
          ctrlKey: true,
          altKey: false,
          metaKey: false,
        },
        isDefaultPrevented: false,
      },
      {
        clickEvent: {
          shiftKey: false,
          ctrlKey: false,
          altKey: true,
          metaKey: false,
        },
        isDefaultPrevented: false,
      },
      {
        clickEvent: {
          shiftKey: false,
          ctrlKey: false,
          altKey: false,
          metaKey: true,
        },
        isDefaultPrevented: false,
      },
    ])(
      'when a pagination button is clicked with $clickEvent',
      ({ clickEvent, isDefaultPrevented }) => {
        let clickEventMock;
        beforeEach(() => {
          clickEventMock = { ...clickEvent, preventDefault: jest.fn() };
        });

        it(`should ${isDefaultPrevented ? '' : 'not '}prevent default event`, () => {
          const pagination = wrapper.findComponent(GlKeysetPagination);
          pagination.vm.$emit('click', clickEventMock);
          expect(clickEventMock.preventDefault).toHaveBeenCalledTimes(isDefaultPrevented ? 1 : 0);
        });
      },
    );

    it('should navigate to a correct previous page', () => {
      const pagination = wrapper.findComponent(GlKeysetPagination);
      pagination.vm.$emit('prev', pageInfo.startCursor);
      expect(mockRouter.push).toHaveBeenCalledWith({ query: { before: pageInfo.startCursor } });
    });

    it('should navigate to a correct next page', () => {
      const pagination = wrapper.findComponent(GlKeysetPagination);
      pagination.vm.$emit('next', pageInfo.endCursor);
      expect(mockRouter.push).toHaveBeenCalledWith({ query: { after: pageInfo.endCursor } });
    });
  });
});