2018-04-24 17:35:32 -04:00
|
|
|
import $ from 'jquery';
|
2018-02-01 06:12:37 -05:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-12-15 04:31:58 -05:00
|
|
|
import Pager from '~/pager';
|
2017-02-26 21:05:50 -05:00
|
|
|
|
|
|
|
describe('pager', () => {
|
2018-04-24 17:35:32 -04:00
|
|
|
let axiosMock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
axiosMock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
axiosMock.restore();
|
|
|
|
});
|
|
|
|
|
2017-02-26 21:05:50 -05:00
|
|
|
describe('init', () => {
|
|
|
|
const originalHref = window.location.href;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
setFixtures('<div class="content_list"></div><div class="loading"></div>');
|
2018-04-24 17:35:32 -04:00
|
|
|
spyOn($.fn, 'endlessScroll').and.stub();
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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`;
|
2018-04-13 23:49:11 -04:00
|
|
|
spyOnDependency(Pager, 'removeParams').and.returnValue(href);
|
2017-02-26 21:05:50 -05:00
|
|
|
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`;
|
2018-04-13 23:49:11 -04:00
|
|
|
const removeParams = spyOnDependency(Pager, 'removeParams').and.returnValue(href);
|
2017-02-26 21:05:50 -05:00
|
|
|
Pager.init();
|
2018-04-13 23:49:11 -04:00
|
|
|
expect(removeParams).toHaveBeenCalledWith(['limit', 'offset']);
|
2017-02-26 21:05:50 -05:00
|
|
|
expect(Pager.url).toEqual(href);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getOld', () => {
|
2018-02-01 07:26:55 -05:00
|
|
|
const urlRegex = /(.*)some_list(.*)$/;
|
2018-02-01 06:12:37 -05:00
|
|
|
|
|
|
|
function mockSuccess() {
|
2018-04-24 17:35:32 -04:00
|
|
|
axiosMock.onGet(urlRegex).reply(200, {
|
2018-02-01 07:26:55 -05:00
|
|
|
count: 0,
|
2018-02-01 06:12:37 -05:00
|
|
|
html: '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function mockError() {
|
2018-04-24 17:35:32 -04:00
|
|
|
axiosMock.onGet(urlRegex).networkError();
|
2018-02-01 06:12:37 -05:00
|
|
|
}
|
|
|
|
|
2017-02-26 21:05:50 -05:00
|
|
|
beforeEach(() => {
|
2018-04-24 17:35:32 -04:00
|
|
|
setFixtures(
|
|
|
|
'<div class="content_list" data-href="/some_list"></div><div class="loading"></div>',
|
|
|
|
);
|
2018-02-01 06:12:37 -05:00
|
|
|
spyOn(axios, 'get').and.callThrough();
|
|
|
|
|
2018-02-01 07:26:55 -05:00
|
|
|
Pager.init();
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
|
|
|
|
2018-04-24 17:35:32 -04:00
|
|
|
it('shows loader while loading next page', done => {
|
2018-02-01 06:12:37 -05:00
|
|
|
mockSuccess();
|
|
|
|
|
2017-02-26 21:05:50 -05:00
|
|
|
spyOn(Pager.loading, 'show');
|
|
|
|
Pager.getOld();
|
2018-02-01 06:12:37 -05:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(Pager.loading.show).toHaveBeenCalled();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
|
|
|
|
2018-04-24 17:35:32 -04:00
|
|
|
it('hides loader on success', done => {
|
2018-02-01 06:12:37 -05:00
|
|
|
mockSuccess();
|
|
|
|
|
2017-02-26 21:05:50 -05:00
|
|
|
spyOn(Pager.loading, 'hide');
|
|
|
|
Pager.getOld();
|
2018-02-01 06:12:37 -05:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(Pager.loading.hide).toHaveBeenCalled();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
|
|
|
|
2018-04-24 17:35:32 -04:00
|
|
|
it('hides loader on error', done => {
|
2018-02-01 06:12:37 -05:00
|
|
|
mockError();
|
|
|
|
|
2017-02-26 21:05:50 -05:00
|
|
|
spyOn(Pager.loading, 'hide');
|
|
|
|
Pager.getOld();
|
2018-02-01 06:12:37 -05:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(Pager.loading.hide).toHaveBeenCalled();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
|
|
|
|
2018-04-24 17:35:32 -04:00
|
|
|
it('sends request to url with offset and limit params', done => {
|
2017-02-26 21:05:50 -05:00
|
|
|
Pager.offset = 100;
|
|
|
|
Pager.limit = 20;
|
|
|
|
Pager.getOld();
|
2018-02-01 06:12:37 -05:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2018-02-01 07:26:55 -05:00
|
|
|
const [url, params] = axios.get.calls.argsFor(0);
|
|
|
|
|
|
|
|
expect(params).toEqual({
|
|
|
|
params: {
|
|
|
|
limit: 20,
|
|
|
|
offset: 100,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(url).toBe('/some_list');
|
2018-02-01 06:12:37 -05:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|