summaryrefslogtreecommitdiff
path: root/spec/javascripts/pager_spec.js
blob: fd9b83e3514ab8ea716626d2eddbab69638a7a2e (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
/* global fixture */
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import * as utils from '~/lib/utils/url_utility';
import Pager from '~/pager';

describe('pager', () => {
  describe('init', () => {
    const originalHref = window.location.href;

    beforeEach(() => {
      setFixtures('<div class="content_list"></div><div class="loading"></div>');
    });

    afterEach(() => {
      window.history.replaceState({}, null, originalHref);
    });

    it('should use data-href attribute from list element', () => {
      const href = `${gl.TEST_HOST}/some_list.json`;
      setFixtures(`<div class="content_list" data-href="${href}"></div>`);
      Pager.init();
      expect(Pager.url).toBe(href);
    });

    it('should use current url if data-href attribute not provided', () => {
      const href = `${gl.TEST_HOST}/some_list`;
      spyOn(utils, 'removeParams').and.returnValue(href);
      Pager.init();
      expect(Pager.url).toBe(href);
    });

    it('should get initial offset from query parameter', () => {
      window.history.replaceState({}, null, '?offset=100');
      Pager.init();
      expect(Pager.offset).toBe(100);
    });

    it('keeps extra query parameters from url', () => {
      window.history.replaceState({}, null, '?filter=test&offset=100');
      const href = `${gl.TEST_HOST}/some_list?filter=test`;
      spyOn(utils, 'removeParams').and.returnValue(href);
      Pager.init();
      expect(utils.removeParams).toHaveBeenCalledWith(['limit', 'offset']);
      expect(Pager.url).toEqual(href);
    });
  });

  describe('getOld', () => {
    const urlRegex = /\/some_list(.*)$/;
    let mock;

    function mockSuccess() {
      mock.onGet(urlRegex).reply(200, {
        count: 20,
        html: '',
      });
    }

    function mockError() {
      mock.onGet(urlRegex).networkError();
    }

    beforeEach(() => {
      setFixtures('<div class="content_list" data-href="/some_list"></div><div class="loading"></div>');
      spyOn(axios, 'get').and.callThrough();

      Pager.init();

      mock = new MockAdapter(axios);
    });

    afterEach(() => {
      mock.restore();
    });

    it('shows loader while loading next page', (done) => {
      mockSuccess();

      spyOn(Pager.loading, 'show');
      Pager.getOld();

      setTimeout(() => {
        expect(Pager.loading.show).toHaveBeenCalled();

        done();
      });
    });

    it('hides loader on success', (done) => {
      mockSuccess();

      spyOn(Pager.loading, 'hide');
      Pager.getOld();

      setTimeout(() => {
        expect(Pager.loading.hide).toHaveBeenCalled();

        done();
      });
    });

    it('hides loader on error', (done) => {
      mockError();

      spyOn(Pager.loading, 'hide');
      Pager.getOld();

      setTimeout(() => {
        expect(Pager.loading.hide).toHaveBeenCalled();

        done();
      });
    });

    it('sends request to url with offset and limit params', (done) => {
      Pager.offset = 100;
      Pager.limit = 20;
      Pager.getOld();

      setTimeout(() => {
        const [url, params] = $.ajax.calls.argsFor(0);
        console.log(url, params);
        // expect(data).toBe('limit=20&offset=100');
        // expect(url).toBe('/some_list');

        done();
      });
    });
  });
});