gitlab-org--gitlab-foss/spec/frontend/pager_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

240 lines
6.1 KiB
JavaScript
Raw Normal View History

2018-02-01 06:12:37 -05:00
import MockAdapter from 'axios-mock-adapter';
import $ from 'jquery';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import { TEST_HOST } from 'helpers/test_constants';
import waitForPromises from 'helpers/wait_for_promises';
2018-02-01 06:12:37 -05:00
import axios from '~/lib/utils/axios_utils';
import { removeParams } from '~/lib/utils/url_utility';
import Pager from '~/pager';
jest.mock('~/lib/utils/url_utility', () => ({
...jest.requireActual('~/lib/utils/url_utility'),
removeParams: jest.fn().mockName('removeParams'),
}));
describe('pager', () => {
2018-04-24 17:35:32 -04:00
let axiosMock;
beforeEach(() => {
axiosMock = new MockAdapter(axios);
});
afterEach(() => {
axiosMock.restore();
});
describe('init', () => {
const originalHref = window.location.href;
beforeEach(() => {
setHTMLFixture('<div class="content_list"></div><div class="loading"></div>');
jest.spyOn($.fn, 'endlessScroll').mockImplementation();
});
afterEach(() => {
window.history.replaceState({}, null, originalHref);
resetHTMLFixture();
});
it('should get initial offset from query parameter', () => {
window.history.replaceState({}, null, '?offset=100');
Pager.init();
2018-10-09 14:03:09 -04: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
function mockSuccess(count = 0) {
2018-04-24 17:35:32 -04:00
axiosMock.onGet(urlRegex).reply(200, {
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
}
beforeEach(() => {
setHTMLFixture(
2018-04-24 17:35:32 -04:00
'<div class="content_list" data-href="/some_list"></div><div class="loading"></div>',
);
jest.spyOn(axios, 'get');
2018-02-01 06:12:37 -05:00
2018-02-01 07:26:55 -05:00
Pager.init();
});
afterEach(() => {
resetHTMLFixture();
});
it('shows loader while loading next page', async () => {
2018-02-01 06:12:37 -05:00
mockSuccess();
jest.spyOn(Pager.$loading, 'show').mockImplementation(() => {});
Pager.getOld();
2018-02-01 06:12:37 -05:00
await waitForPromises();
2018-02-01 06:12:37 -05:00
expect(Pager.$loading.show).toHaveBeenCalled();
});
it('hides loader on success', async () => {
2018-02-01 06:12:37 -05:00
mockSuccess();
jest.spyOn(Pager.$loading, 'hide').mockImplementation(() => {});
Pager.getOld();
2018-02-01 06:12:37 -05:00
await waitForPromises();
2018-02-01 06:12:37 -05:00
expect(Pager.$loading.hide).toHaveBeenCalled();
});
it('hides loader on error', async () => {
2018-02-01 06:12:37 -05:00
mockError();
jest.spyOn(Pager.$loading, 'hide').mockImplementation(() => {});
Pager.getOld();
2018-02-01 06:12:37 -05:00
await waitForPromises();
2018-02-01 06:12:37 -05:00
expect(Pager.$loading.hide).toHaveBeenCalled();
});
it('sends request to url with offset and limit params', async () => {
Pager.offset = 100;
Pager.limit = 20;
Pager.getOld();
2018-02-01 06:12:37 -05:00
await waitForPromises();
2018-02-01 07:26:55 -05:00
const [url, params] = axios.get.mock.calls[0];
2018-10-09 14:03:09 -04:00
expect(params).toEqual({
params: {
limit: 20,
offset: 100,
},
2018-02-01 06:12:37 -05:00
});
expect(url).toBe('/some_list');
});
it('disables if return count is less than limit', async () => {
Pager.offset = 0;
Pager.limit = 20;
mockSuccess(1);
jest.spyOn(Pager.$loading, 'hide').mockImplementation(() => {});
Pager.getOld();
await waitForPromises();
expect(Pager.$loading.hide).toHaveBeenCalled();
expect(Pager.disable).toBe(true);
});
describe('has data-href attribute from list element', () => {
const href = `${TEST_HOST}/some_list.json`;
beforeEach(() => {
setHTMLFixture(`<div class="content_list" data-href="${href}"></div>`);
});
afterEach(() => {
resetHTMLFixture();
});
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(() => {
setHTMLFixture(`<div class="content_list"></div>`);
});
afterEach(() => {
resetHTMLFixture();
});
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));
});
});
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', () => {
setHTMLFixture(
`<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));
resetHTMLFixture();
});
});
describe('when `container` is not visible', () => {
it('does not make API request', () => {
setHTMLFixture(
`<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();
resetHTMLFixture();
});
});
});
});
});