summaryrefslogtreecommitdiff
path: root/spec/javascripts/vue_shared/components/table_pagination_spec.js
blob: a5c3870b3acaa10cb2081c5b47067e109d4db864 (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
import Vue from 'vue';
import paginationComp from '~/vue_shared/components/table_pagination';
import '~/lib/utils/common_utils';

describe('Pagination component', () => {
  let component;
  let PaginationComponent;

  const changeChanges = {
    one: '',
  };

  const change = (one) => {
    changeChanges.one = one;
  };

  beforeEach(() => {
    PaginationComponent = Vue.extend(paginationComp);
  });

  it('should render and start at page 1', () => {
    component = new PaginationComponent({
      propsData: {
        pageInfo: {
          totalPages: 10,
          nextPage: 2,
          previousPage: '',
        },
        change,
      },
    }).$mount();

    expect(component.$el.classList).toContain('gl-pagination');

    component.changePage({ target: { innerText: '1' } });

    expect(changeChanges.one).toEqual(1);
  });

  it('should go to the previous page', () => {
    component = new PaginationComponent({
      propsData: {
        pageInfo: {
          totalPages: 10,
          nextPage: 3,
          previousPage: 1,
        },
        change,
      },
    }).$mount();

    component.changePage({ target: { innerText: 'Prev' } });

    expect(changeChanges.one).toEqual(1);
  });

  it('should go to the next page', () => {
    component = new PaginationComponent({
      propsData: {
        pageInfo: {
          totalPages: 10,
          nextPage: 5,
          previousPage: 3,
        },
        change,
      },
    }).$mount();

    component.changePage({ target: { innerText: 'Next' } });

    expect(changeChanges.one).toEqual(5);
  });

  it('should go to the last page', () => {
    component = new PaginationComponent({
      propsData: {
        pageInfo: {
          totalPages: 10,
          nextPage: 5,
          previousPage: 3,
        },
        change,
      },
    }).$mount();

    component.changePage({ target: { innerText: 'Last >>' } });

    expect(changeChanges.one).toEqual(10);
  });

  it('should go to the first page', () => {
    component = new PaginationComponent({
      propsData: {
        pageInfo: {
          totalPages: 10,
          nextPage: 5,
          previousPage: 3,
        },
        change,
      },
    }).$mount();

    component.changePage({ target: { innerText: '<< First' } });

    expect(changeChanges.one).toEqual(1);
  });

  it('should do nothing', () => {
    component = new PaginationComponent({
      propsData: {
        pageInfo: {
          totalPages: 10,
          nextPage: 2,
          previousPage: '',
        },
        change,
      },
    }).$mount();

    component.changePage({ target: { innerText: '...' } });

    expect(changeChanges.one).toEqual(1);
  });
});

describe('paramHelper', () => {
  it('can parse url parameters correctly', () => {
    window.history.pushState({}, null, '?scope=all&p=2');

    const scope = gl.utils.getParameterByName('scope');
    const p = gl.utils.getParameterByName('p');

    expect(scope).toEqual('all');
    expect(p).toEqual('2');
  });

  it('returns null if param not in url', () => {
    window.history.pushState({}, null, '?p=2');

    const scope = gl.utils.getParameterByName('scope');
    const p = gl.utils.getParameterByName('p');

    expect(scope).toEqual(null);
    expect(p).toEqual('2');
  });
});