2018-03-17 18:26:18 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-03-11 19:13:27 +05:30
|
|
|
import $ from 'jquery';
|
2022-07-16 23:28:13 +05:30
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2021-03-08 18:12:59 +05:30
|
|
|
import { TEST_HOST } from 'helpers/test_constants';
|
2022-04-04 11:22:00 +05:30
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2018-03-17 18:26:18 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2023-04-23 21:23:45 +05:30
|
|
|
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
|
2020-06-23 00:09:42 +05:30
|
|
|
import { removeParams } from '~/lib/utils/url_utility';
|
2021-03-11 19:13:27 +05:30
|
|
|
import Pager from '~/pager';
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
jest.mock('~/lib/utils/url_utility', () => ({
|
2021-09-30 23:02:18 +05:30
|
|
|
...jest.requireActual('~/lib/utils/url_utility'),
|
2020-06-23 00:09:42 +05:30
|
|
|
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(() => {
|
2022-07-16 23:28:13 +05:30
|
|
|
setHTMLFixture('<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);
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
resetHTMLFixture();
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
2023-04-23 21:23:45 +05:30
|
|
|
axiosMock.onGet(urlRegex).reply(HTTP_STATUS_OK, {
|
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(() => {
|
2022-07-16 23:28:13 +05:30
|
|
|
setHTMLFixture(
|
2018-10-15 14:42:47 +05:30
|
|
|
'<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();
|
|
|
|
});
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it('shows loader while loading next page', async () => {
|
2018-03-17 18:26:18 +05:30
|
|
|
mockSuccess();
|
|
|
|
|
2022-06-21 17:19:12 +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
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await waitForPromises();
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
expect(Pager.$loading.show).toHaveBeenCalled();
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it('hides loader on success', async () => {
|
2018-03-17 18:26:18 +05:30
|
|
|
mockSuccess();
|
|
|
|
|
2022-06-21 17:19:12 +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
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await waitForPromises();
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
expect(Pager.$loading.hide).toHaveBeenCalled();
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it('hides loader on error', async () => {
|
2018-03-17 18:26:18 +05:30
|
|
|
mockError();
|
|
|
|
|
2022-06-21 17:19:12 +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
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await waitForPromises();
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
expect(Pager.$loading.hide).toHaveBeenCalled();
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it('sends request to url with offset and limit params', async () => {
|
2017-08-17 22:00:37 +05:30
|
|
|
Pager.offset = 100;
|
|
|
|
Pager.limit = 20;
|
|
|
|
Pager.getOld();
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await waitForPromises();
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
const [url, params] = axios.get.mock.calls[0];
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(params).toEqual({
|
|
|
|
params: {
|
|
|
|
limit: 20,
|
|
|
|
offset: 100,
|
|
|
|
},
|
2018-03-17 18:26:18 +05:30
|
|
|
});
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
expect(url).toBe('/some_list');
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
it('disables if return count is less than limit', async () => {
|
2019-12-21 20:55:43 +05:30
|
|
|
Pager.offset = 0;
|
|
|
|
Pager.limit = 20;
|
|
|
|
|
|
|
|
mockSuccess(1);
|
2022-06-21 17:19:12 +05:30
|
|
|
jest.spyOn(Pager.$loading, 'hide').mockImplementation(() => {});
|
2019-12-21 20:55:43 +05:30
|
|
|
Pager.getOld();
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await waitForPromises();
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
expect(Pager.$loading.hide).toHaveBeenCalled();
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(Pager.disable).toBe(true);
|
2019-12-21 20:55:43 +05:30
|
|
|
});
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
describe('has data-href attribute from list element', () => {
|
|
|
|
const href = `${TEST_HOST}/some_list.json`;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-07-16 23:28:13 +05:30
|
|
|
setHTMLFixture(`<div class="content_list" data-href="${href}"></div>`);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
2021-04-29 21:17:54 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
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(() => {
|
2022-07-16 23:28:13 +05:30
|
|
|
setHTMLFixture(`<div class="content_list"></div>`);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
2021-04-29 21:17:54 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
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));
|
|
|
|
});
|
|
|
|
});
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
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', () => {
|
2022-07-16 23:28:13 +05:30
|
|
|
setHTMLFixture(
|
2022-06-21 17:19:12 +05:30
|
|
|
`<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));
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
resetHTMLFixture();
|
2022-06-21 17:19:12 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when `container` is not visible', () => {
|
|
|
|
it('does not make API request', () => {
|
2022-07-16 23:28:13 +05:30
|
|
|
setHTMLFixture(
|
2022-06-21 17:19:12 +05:30
|
|
|
`<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();
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
resetHTMLFixture();
|
2022-06-21 17:19:12 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|