2017-08-17 22:00:37 +05:30
|
|
|
/* global fixture */
|
2018-03-17 18:26:18 +05:30
|
|
|
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';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
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`;
|
2018-03-17 18:26:18 +05:30
|
|
|
spyOn(utils, 'removeParams').and.returnValue(href);
|
2017-08-17 22:00:37 +05:30
|
|
|
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-03-17 18:26:18 +05:30
|
|
|
spyOn(utils, 'removeParams').and.returnValue(href);
|
2017-08-17 22:00:37 +05:30
|
|
|
Pager.init();
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(utils.removeParams).toHaveBeenCalledWith(['limit', 'offset']);
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(Pager.url).toEqual(href);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getOld', () => {
|
2018-03-17 18:26:18 +05:30
|
|
|
const urlRegex = /(.*)some_list(.*)$/;
|
|
|
|
let mock;
|
|
|
|
|
|
|
|
function mockSuccess() {
|
|
|
|
mock.onGet(urlRegex).reply(200, {
|
|
|
|
count: 0,
|
|
|
|
html: '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function mockError() {
|
|
|
|
mock.onGet(urlRegex).networkError();
|
|
|
|
}
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
setFixtures('<div class="content_list" data-href="/some_list"></div><div class="loading"></div>');
|
2018-03-17 18:26:18 +05:30
|
|
|
spyOn(axios, 'get').and.callThrough();
|
|
|
|
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
Pager.init();
|
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows loader while loading next page', (done) => {
|
|
|
|
mockSuccess();
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
spyOn(Pager.loading, 'show');
|
|
|
|
Pager.getOld();
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(Pager.loading.show).toHaveBeenCalled();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('hides loader on success', (done) => {
|
|
|
|
mockSuccess();
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
spyOn(Pager.loading, 'hide');
|
|
|
|
Pager.getOld();
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(Pager.loading.hide).toHaveBeenCalled();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('hides loader on error', (done) => {
|
|
|
|
mockError();
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
spyOn(Pager.loading, 'hide');
|
|
|
|
Pager.getOld();
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(Pager.loading.hide).toHaveBeenCalled();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it('sends request to url with offset and limit params', (done) => {
|
2017-08-17 22:00:37 +05:30
|
|
|
Pager.offset = 100;
|
|
|
|
Pager.limit = 20;
|
|
|
|
Pager.getOld();
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
const [url, params] = axios.get.calls.argsFor(0);
|
|
|
|
|
|
|
|
expect(params).toEqual({
|
|
|
|
params: {
|
|
|
|
limit: 20,
|
|
|
|
offset: 100,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(url).toBe('/some_list');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|