table_pagination_spec.js 3.2 KB
Newer Older
1 2 3
import Vue from 'vue';
import paginationComp from '~/vue_shared/components/table_pagination';
import '~/lib/utils/common_utils';
4 5 6

describe('Pagination component', () => {
  let component;
7
  let PaginationComponent;
8 9 10 11 12

  const changeChanges = {
    one: '',
  };

13
  const change = (one) => {
14 15 16
    changeChanges.one = one;
  };

17 18 19
  beforeEach(() => {
    PaginationComponent = Vue.extend(paginationComp);
  });
20

21 22
  it('should render and start at page 1', () => {
    component = new PaginationComponent({
23 24 25 26 27 28 29 30
      propsData: {
        pageInfo: {
          totalPages: 10,
          nextPage: 2,
          previousPage: '',
        },
        change,
      },
31
    }).$mount();
32 33 34

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

R
Regis 已提交
35
    component.changePage({ target: { innerText: '1' } });
36 37 38

    expect(changeChanges.one).toEqual(1);
  });
R
Regis 已提交
39

R
Regis 已提交
40
  it('should go to the previous page', () => {
41
    component = new PaginationComponent({
R
Regis 已提交
42 43 44
      propsData: {
        pageInfo: {
          totalPages: 10,
R
Regis 已提交
45 46
          nextPage: 3,
          previousPage: 1,
R
Regis 已提交
47 48 49
        },
        change,
      },
50
    }).$mount();
R
Regis 已提交
51

R
Regis 已提交
52
    component.changePage({ target: { innerText: 'Prev' } });
R
Regis 已提交
53

R
Regis 已提交
54 55
    expect(changeChanges.one).toEqual(1);
  });
R
Regis 已提交
56

R
Regis 已提交
57
  it('should go to the next page', () => {
58
    component = new PaginationComponent({
R
Regis 已提交
59 60 61 62 63 64 65 66
      propsData: {
        pageInfo: {
          totalPages: 10,
          nextPage: 5,
          previousPage: 3,
        },
        change,
      },
67
    }).$mount();
R
Regis 已提交
68

R
Regis 已提交
69
    component.changePage({ target: { innerText: 'Next' } });
R
Regis 已提交
70 71

    expect(changeChanges.one).toEqual(5);
R
Regis 已提交
72
  });
R
Regis 已提交
73 74

  it('should go to the last page', () => {
75
    component = new PaginationComponent({
R
Regis 已提交
76 77 78 79 80 81 82 83
      propsData: {
        pageInfo: {
          totalPages: 10,
          nextPage: 5,
          previousPage: 3,
        },
        change,
      },
84
    }).$mount();
R
Regis 已提交
85

86
    component.changePage({ target: { innerText: 'Last »' } });
R
Regis 已提交
87 88 89 90 91

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

  it('should go to the first page', () => {
92
    component = new PaginationComponent({
R
Regis 已提交
93 94 95 96 97 98 99 100
      propsData: {
        pageInfo: {
          totalPages: 10,
          nextPage: 5,
          previousPage: 3,
        },
        change,
      },
101
    }).$mount();
R
Regis 已提交
102

103
    component.changePage({ target: { innerText: '« First' } });
R
Regis 已提交
104 105 106 107 108

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

  it('should do nothing', () => {
109
    component = new PaginationComponent({
R
Regis 已提交
110 111 112 113 114 115 116 117
      propsData: {
        pageInfo: {
          totalPages: 10,
          nextPage: 2,
          previousPage: '',
        },
        change,
      },
118
    }).$mount();
R
Regis 已提交
119

R
Regis 已提交
120
    component.changePage({ target: { innerText: '...' } });
R
Regis 已提交
121 122 123

    expect(changeChanges.one).toEqual(1);
  });
124
});
R
Regis 已提交
125 126 127 128 129

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

130 131
    const scope = gl.utils.getParameterByName('scope');
    const p = gl.utils.getParameterByName('p');
R
Regis 已提交
132 133 134 135 136 137 138 139

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

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

140 141
    const scope = gl.utils.getParameterByName('scope');
    const p = gl.utils.getParameterByName('p');
R
Regis 已提交
142 143 144 145 146

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