debian-mirror-gitlab/spec/frontend/pager_spec.js

169 lines
4 KiB
JavaScript
Raw Normal View History

2018-10-15 14:42:47 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import Pager from '~/pager';
2020-06-23 00:09:42 +05:30
import { removeParams } from '~/lib/utils/url_utility';
2020-07-28 23:09:34 +05:30
import { TEST_HOST } from 'jest/helpers/test_constants';
2020-06-23 00:09:42 +05:30
jest.mock('~/lib/utils/url_utility', () => ({
removeParams: jest.fn().mockName('removeParams'),
}));
2017-08-17 22:00:37 +05:30
describe('pager', () => {
2018-10-15 14:42:47 +05:30
let axiosMock;
beforeEach(() => {
axiosMock = new MockAdapter(axios);
});
afterEach(() => {
axiosMock.restore();
});
2017-08-17 22:00:37 +05:30
describe('init', () => {
const originalHref = window.location.href;
beforeEach(() => {
setFixtures('<div class="content_list"></div><div class="loading"></div>');
2020-06-23 00:09:42 +05:30
jest.spyOn($.fn, 'endlessScroll').mockImplementation();
2017-08-17 22:00:37 +05:30
});
afterEach(() => {
window.history.replaceState({}, null, originalHref);
});
it('should use data-href attribute from list element', () => {
2020-07-28 23:09:34 +05:30
const href = `${TEST_HOST}/some_list.json`;
2017-08-17 22:00:37 +05:30
setFixtures(`<div class="content_list" data-href="${href}"></div>`);
Pager.init();
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
expect(Pager.url).toBe(href);
});
it('should use current url if data-href attribute not provided', () => {
2020-07-28 23:09:34 +05:30
const href = `${TEST_HOST}/some_list`;
2020-06-23 00:09:42 +05:30
removeParams.mockReturnValue(href);
2017-08-17 22:00:37 +05:30
Pager.init();
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
expect(Pager.url).toBe(href);
});
it('should get initial offset from query parameter', () => {
window.history.replaceState({}, null, '?offset=100');
Pager.init();
2018-12-13 13:39:08 +05:30
2017-08-17 22:00:37 +05:30
expect(Pager.offset).toBe(100);
});
it('keeps extra query parameters from url', () => {
window.history.replaceState({}, null, '?filter=test&offset=100');
2020-07-28 23:09:34 +05:30
const href = `${TEST_HOST}/some_list?filter=test`;
2020-06-23 00:09:42 +05:30
removeParams.mockReturnValue(href);
2017-08-17 22:00:37 +05:30
Pager.init();
2018-12-13 13:39:08 +05:30
2018-10-15 14:42:47 +05:30
expect(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(.*)$/;
2019-12-21 20:55:43 +05:30
function mockSuccess(count = 0) {
2018-10-15 14:42:47 +05:30
axiosMock.onGet(urlRegex).reply(200, {
2019-12-21 20:55:43 +05:30
count,
2018-03-17 18:26:18 +05:30
html: '',
});
}
function mockError() {
2018-10-15 14:42:47 +05:30
axiosMock.onGet(urlRegex).networkError();
2018-03-17 18:26:18 +05:30
}
2017-08-17 22:00:37 +05:30
beforeEach(() => {
2018-10-15 14:42:47 +05:30
setFixtures(
'<div class="content_list" data-href="/some_list"></div><div class="loading"></div>',
);
2020-06-23 00:09:42 +05:30
jest.spyOn(axios, 'get');
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
Pager.init();
});
2018-10-15 14:42:47 +05:30
it('shows loader while loading next page', done => {
2018-03-17 18:26:18 +05:30
mockSuccess();
2020-06-23 00:09:42 +05:30
jest.spyOn(Pager.loading, 'show').mockImplementation(() => {});
2017-08-17 22:00:37 +05:30
Pager.getOld();
2018-03-17 18:26:18 +05:30
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2018-03-17 18:26:18 +05:30
expect(Pager.loading.show).toHaveBeenCalled();
done();
});
2017-08-17 22:00:37 +05:30
});
2018-10-15 14:42:47 +05:30
it('hides loader on success', done => {
2018-03-17 18:26:18 +05:30
mockSuccess();
2020-06-23 00:09:42 +05:30
jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {});
2017-08-17 22:00:37 +05:30
Pager.getOld();
2018-03-17 18:26:18 +05:30
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2018-03-17 18:26:18 +05:30
expect(Pager.loading.hide).toHaveBeenCalled();
done();
});
2017-08-17 22:00:37 +05:30
});
2018-10-15 14:42:47 +05:30
it('hides loader on error', done => {
2018-03-17 18:26:18 +05:30
mockError();
2020-06-23 00:09:42 +05:30
jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {});
2017-08-17 22:00:37 +05:30
Pager.getOld();
2018-03-17 18:26:18 +05:30
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2018-03-17 18:26:18 +05:30
expect(Pager.loading.hide).toHaveBeenCalled();
done();
});
2017-08-17 22:00:37 +05:30
});
2018-10-15 14:42:47 +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
2020-06-23 00:09:42 +05:30
setImmediate(() => {
const [url, params] = axios.get.mock.calls[0];
2018-03-17 18:26:18 +05:30
expect(params).toEqual({
params: {
limit: 20,
offset: 100,
},
});
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
expect(url).toBe('/some_list');
done();
});
2017-08-17 22:00:37 +05:30
});
2019-12-21 20:55:43 +05:30
it('disables if return count is less than limit', done => {
Pager.offset = 0;
Pager.limit = 20;
mockSuccess(1);
2020-06-23 00:09:42 +05:30
jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {});
2019-12-21 20:55:43 +05:30
Pager.getOld();
2020-06-23 00:09:42 +05:30
setImmediate(() => {
2019-12-21 20:55:43 +05:30
expect(Pager.loading.hide).toHaveBeenCalled();
expect(Pager.disable).toBe(true);
done();
});
});
2017-08-17 22:00:37 +05:30
});
});