2018-02-01 06:12:37 -05:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-02-14 13:09:20 -05:00
|
|
|
import $ from 'jquery';
|
2021-01-14 19:10:45 -05:00
|
|
|
import { TEST_HOST } from 'helpers/test_constants';
|
2018-02-01 06:12:37 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2020-05-20 08:07:52 -04:00
|
|
|
import { removeParams } from '~/lib/utils/url_utility';
|
2021-02-14 13:09:20 -05:00
|
|
|
import Pager from '~/pager';
|
2020-05-20 08:07:52 -04:00
|
|
|
|
|
|
|
jest.mock('~/lib/utils/url_utility', () => ({
|
2021-07-09 05:09:53 -04:00
|
|
|
...jest.requireActual('~/lib/utils/url_utility'),
|
2020-05-20 08:07:52 -04:00
|
|
|
removeParams: jest.fn().mockName('removeParams'),
|
|
|
|
}));
|
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>');
|
2020-05-20 08:07:52 -04:00
|
|
|
jest.spyOn($.fn, 'endlessScroll').mockImplementation();
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
window.history.replaceState({}, null, originalHref);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get initial offset from query parameter', () => {
|
|
|
|
window.history.replaceState({}, null, '?offset=100');
|
|
|
|
Pager.init();
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-02-26 21:05:50 -05:00
|
|
|
expect(Pager.offset).toBe(100);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getOld', () => {
|
2018-02-01 07:26:55 -05:00
|
|
|
const urlRegex = /(.*)some_list(.*)$/;
|
2018-02-01 06:12:37 -05:00
|
|
|
|
2019-10-11 05:06:43 -04:00
|
|
|
function mockSuccess(count = 0) {
|
2018-04-24 17:35:32 -04:00
|
|
|
axiosMock.onGet(urlRegex).reply(200, {
|
2019-10-11 05:06:43 -04:00
|
|
|
count,
|
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>',
|
|
|
|
);
|
2020-05-20 08:07:52 -04:00
|
|
|
jest.spyOn(axios, 'get');
|
2018-02-01 06:12:37 -05:00
|
|
|
|
2018-02-01 07:26:55 -05:00
|
|
|
Pager.init();
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
it('shows loader while loading next page', (done) => {
|
2018-02-01 06:12:37 -05:00
|
|
|
mockSuccess();
|
|
|
|
|
2020-05-20 08:07:52 -04:00
|
|
|
jest.spyOn(Pager.loading, 'show').mockImplementation(() => {});
|
2017-02-26 21:05:50 -05:00
|
|
|
Pager.getOld();
|
2018-02-01 06:12:37 -05:00
|
|
|
|
2020-05-20 08:07:52 -04:00
|
|
|
setImmediate(() => {
|
2018-02-01 06:12:37 -05:00
|
|
|
expect(Pager.loading.show).toHaveBeenCalled();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
it('hides loader on success', (done) => {
|
2018-02-01 06:12:37 -05:00
|
|
|
mockSuccess();
|
|
|
|
|
2020-05-20 08:07:52 -04:00
|
|
|
jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {});
|
2017-02-26 21:05:50 -05:00
|
|
|
Pager.getOld();
|
2018-02-01 06:12:37 -05:00
|
|
|
|
2020-05-20 08:07:52 -04:00
|
|
|
setImmediate(() => {
|
2018-02-01 06:12:37 -05:00
|
|
|
expect(Pager.loading.hide).toHaveBeenCalled();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
it('hides loader on error', (done) => {
|
2018-02-01 06:12:37 -05:00
|
|
|
mockError();
|
|
|
|
|
2020-05-20 08:07:52 -04:00
|
|
|
jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {});
|
2017-02-26 21:05:50 -05:00
|
|
|
Pager.getOld();
|
2018-02-01 06:12:37 -05:00
|
|
|
|
2020-05-20 08:07:52 -04:00
|
|
|
setImmediate(() => {
|
2018-02-01 06:12:37 -05:00
|
|
|
expect(Pager.loading.hide).toHaveBeenCalled();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
|
|
|
|
2020-12-23 19:10:25 -05: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
|
|
|
|
2020-05-20 08:07:52 -04:00
|
|
|
setImmediate(() => {
|
|
|
|
const [url, params] = axios.get.mock.calls[0];
|
2018-02-01 07:26:55 -05:00
|
|
|
|
|
|
|
expect(params).toEqual({
|
|
|
|
params: {
|
|
|
|
limit: 20,
|
|
|
|
offset: 100,
|
|
|
|
},
|
|
|
|
});
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-02-01 07:26:55 -05:00
|
|
|
expect(url).toBe('/some_list');
|
2018-02-01 06:12:37 -05:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
2019-10-11 05:06:43 -04:00
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
it('disables if return count is less than limit', (done) => {
|
2019-10-11 05:06:43 -04:00
|
|
|
Pager.offset = 0;
|
|
|
|
Pager.limit = 20;
|
|
|
|
|
|
|
|
mockSuccess(1);
|
2020-05-20 08:07:52 -04:00
|
|
|
jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {});
|
2019-10-11 05:06:43 -04:00
|
|
|
Pager.getOld();
|
|
|
|
|
2020-05-20 08:07:52 -04:00
|
|
|
setImmediate(() => {
|
2019-10-11 05:06:43 -04:00
|
|
|
expect(Pager.loading.hide).toHaveBeenCalled();
|
|
|
|
expect(Pager.disable).toBe(true);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2021-03-25 20:09:22 -04:00
|
|
|
|
|
|
|
describe('has data-href attribute from list element', () => {
|
|
|
|
const href = `${TEST_HOST}/some_list.json`;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
setFixtures(`<div class="content_list" data-href="${href}"></div>`);
|
|
|
|
});
|
|
|
|
|
|
|
|
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(() => {
|
|
|
|
setFixtures(`<div class="content_list"></div>`);
|
|
|
|
});
|
|
|
|
|
|
|
|
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));
|
|
|
|
});
|
|
|
|
});
|
2017-02-26 21:05:50 -05:00
|
|
|
});
|
|
|
|
});
|