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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
import MockAdapter from 'axios-mock-adapter';
import $ from 'jquery';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import { TEST_HOST } from 'helpers/test_constants';
import waitForPromises from 'helpers/wait_for_promises';
import axios from '~/lib/utils/axios_utils';
import { removeParams } from '~/lib/utils/url_utility';
import Pager from '~/pager';
jest.mock('~/lib/utils/url_utility', () => ({
...jest.requireActual('~/lib/utils/url_utility'),
removeParams: jest.fn().mockName('removeParams'),
}));
describe('pager', () => {
let axiosMock;
beforeEach(() => {
axiosMock = new MockAdapter(axios);
});
afterEach(() => {
axiosMock.restore();
});
describe('init', () => {
const originalHref = window.location.href;
beforeEach(() => {
setHTMLFixture('<div class="content_list"></div><div class="loading"></div>');
jest.spyOn($.fn, 'endlessScroll').mockImplementation();
});
afterEach(() => {
window.history.replaceState({}, null, originalHref);
resetHTMLFixture();
});
it('should get initial offset from query parameter', () => {
window.history.replaceState({}, null, '?offset=100');
Pager.init();
expect(Pager.offset).toBe(100);
});
});
describe('getOld', () => {
const urlRegex = /(.*)some_list(.*)$/;
function mockSuccess(count = 0) {
axiosMock.onGet(urlRegex).reply(200, {
count,
html: '',
});
}
function mockError() {
axiosMock.onGet(urlRegex).networkError();
}
beforeEach(() => {
setHTMLFixture(
'<div class="content_list" data-href="/some_list"></div><div class="loading"></div>',
);
jest.spyOn(axios, 'get');
Pager.init();
});
afterEach(() => {
resetHTMLFixture();
});
it('shows loader while loading next page', async () => {
mockSuccess();
jest.spyOn(Pager.$loading, 'show').mockImplementation(() => {});
Pager.getOld();
await waitForPromises();
expect(Pager.$loading.show).toHaveBeenCalled();
});
it('hides loader on success', async () => {
mockSuccess();
jest.spyOn(Pager.$loading, 'hide').mockImplementation(() => {});
Pager.getOld();
await waitForPromises();
expect(Pager.$loading.hide).toHaveBeenCalled();
});
it('hides loader on error', async () => {
mockError();
jest.spyOn(Pager.$loading, 'hide').mockImplementation(() => {});
Pager.getOld();
await waitForPromises();
expect(Pager.$loading.hide).toHaveBeenCalled();
});
it('sends request to url with offset and limit params', async () => {
Pager.offset = 100;
Pager.limit = 20;
Pager.getOld();
await waitForPromises();
const [url, params] = axios.get.mock.calls[0];
expect(params).toEqual({
params: {
limit: 20,
offset: 100,
},
});
expect(url).toBe('/some_list');
});
it('disables if return count is less than limit', async () => {
Pager.offset = 0;
Pager.limit = 20;
mockSuccess(1);
jest.spyOn(Pager.$loading, 'hide').mockImplementation(() => {});
Pager.getOld();
await waitForPromises();
expect(Pager.$loading.hide).toHaveBeenCalled();
expect(Pager.disable).toBe(true);
});
describe('has data-href attribute from list element', () => {
const href = `${TEST_HOST}/some_list.json`;
beforeEach(() => {
setHTMLFixture(`<div class="content_list" data-href="${href}"></div>`);
});
afterEach(() => {
resetHTMLFixture();
});
it('should use data-href attribute', () => {
Pager.getOld();
expect(axios.get).toHaveBeenCalledWith(href, expect.any(Object));
});
it('should not use current url', () => {
Pager.getOld();
expect(removeParams).not.toHaveBeenCalled();
expect(removeParams).not.toHaveBeenCalledWith(href);
});
});
describe('no data-href attribute attribute provided from list element', () => {
beforeEach(() => {
setHTMLFixture(`<div class="content_list"></div>`);
});
afterEach(() => {
resetHTMLFixture();
});
it('should use current url', () => {
const href = `${TEST_HOST}/some_list`;
removeParams.mockReturnValue(href);
Pager.getOld();
expect(axios.get).toHaveBeenCalledWith(href, expect.any(Object));
});
it('keeps extra query parameters from url', () => {
window.history.replaceState({}, null, '?filter=test&offset=100');
const href = `${TEST_HOST}/some_list?filter=test`;
removeParams.mockReturnValue(href);
Pager.getOld();
expect(removeParams).toHaveBeenCalledWith(['limit', 'offset']);
expect(axios.get).toHaveBeenCalledWith(href, expect.any(Object));
});
});
describe('when `container` is passed', () => {
const href = '/some_list';
const container = '#js-pager';
let endlessScrollCallback;
beforeEach(() => {
jest.spyOn(axios, 'get');
jest.spyOn($.fn, 'endlessScroll').mockImplementation(({ callback }) => {
endlessScrollCallback = callback;
});
});
describe('when `container` is visible', () => {
it('makes API request', () => {
setHTMLFixture(
`<div id="js-pager"><div class="content_list" data-href="${href}"></div></div>`,
);
Pager.init({ container });
endlessScrollCallback();
expect(axios.get).toHaveBeenCalledWith(href, expect.any(Object));
resetHTMLFixture();
});
});
describe('when `container` is not visible', () => {
it('does not make API request', () => {
setHTMLFixture(
`<div id="js-pager" style="display: none;"><div class="content_list" data-href="${href}"></div></div>`,
);
Pager.init({ container });
endlessScrollCallback();
expect(axios.get).not.toHaveBeenCalled();
resetHTMLFixture();
});
});
});
});
});
|