debian-mirror-gitlab/spec/frontend/releases/components/releases_pagination_spec.js

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

127 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-06-21 17:19:12 +05:30
import { mountExtended } from 'helpers/vue_test_utils_helper';
2021-06-08 01:23:25 +05:30
import { historyPushState } from '~/lib/utils/common_utils';
2020-11-24 15:15:51 +05:30
import ReleasesPagination from '~/releases/components/releases_pagination.vue';
2021-06-08 01:23:25 +05:30
jest.mock('~/lib/utils/common_utils', () => ({
...jest.requireActual('~/lib/utils/common_utils'),
historyPushState: jest.fn(),
}));
2020-11-24 15:15:51 +05:30
2022-06-21 17:19:12 +05:30
describe('releases_pagination.vue', () => {
const startCursor = 'startCursor';
const endCursor = 'endCursor';
2020-11-24 15:15:51 +05:30
let wrapper;
2022-06-21 17:19:12 +05:30
let onPrev;
let onNext;
2021-06-08 01:23:25 +05:30
const createComponent = (pageInfo) => {
2022-06-21 17:19:12 +05:30
onPrev = jest.fn();
onNext = jest.fn();
wrapper = mountExtended(ReleasesPagination, {
propsData: {
pageInfo,
},
listeners: {
prev: onPrev,
next: onNext,
},
2021-06-08 01:23:25 +05:30
});
2020-11-24 15:15:51 +05:30
};
afterEach(() => {
wrapper.destroy();
});
2022-06-21 17:19:12 +05:30
const singlePageInfo = {
hasPreviousPage: false,
hasNextPage: false,
startCursor,
endCursor,
2021-06-08 01:23:25 +05:30
};
2022-06-21 17:19:12 +05:30
const onlyNextPageInfo = {
hasPreviousPage: false,
hasNextPage: true,
startCursor,
endCursor,
2021-06-08 01:23:25 +05:30
};
2022-06-21 17:19:12 +05:30
const onlyPrevPageInfo = {
hasPreviousPage: true,
hasNextPage: false,
startCursor,
endCursor,
2021-06-08 01:23:25 +05:30
};
2022-06-21 17:19:12 +05:30
const prevAndNextPageInfo = {
hasPreviousPage: true,
hasNextPage: true,
startCursor,
endCursor,
2021-06-08 01:23:25 +05:30
};
2022-06-21 17:19:12 +05:30
const findPrevButton = () => wrapper.findByTestId('prevButton');
const findNextButton = () => wrapper.findByTestId('nextButton');
describe.each`
description | pageInfo | prevEnabled | nextEnabled
${'when there is only one page of results'} | ${singlePageInfo} | ${false} | ${false}
${'when there is a next page, but not a previous page'} | ${onlyNextPageInfo} | ${false} | ${true}
${'when there is a previous page, but not a next page'} | ${onlyPrevPageInfo} | ${true} | ${false}
${'when there is both a previous and next page'} | ${prevAndNextPageInfo} | ${true} | ${true}
`('component states', ({ description, pageInfo, prevEnabled, nextEnabled }) => {
describe(description, () => {
beforeEach(() => {
createComponent(pageInfo);
2021-06-08 01:23:25 +05:30
});
2022-06-21 17:19:12 +05:30
it(`renders the "Prev" button as ${prevEnabled ? 'enabled' : 'disabled'}`, () => {
expect(findPrevButton().attributes().disabled).toBe(prevEnabled ? undefined : 'disabled');
2021-06-08 01:23:25 +05:30
});
2022-06-21 17:19:12 +05:30
it(`renders the "Next" button as ${nextEnabled ? 'enabled' : 'disabled'}`, () => {
expect(findNextButton().attributes().disabled).toBe(nextEnabled ? undefined : 'disabled');
2021-06-08 01:23:25 +05:30
});
});
});
describe('button behavior', () => {
beforeEach(() => {
2022-06-21 17:19:12 +05:30
createComponent(prevAndNextPageInfo);
2021-06-08 01:23:25 +05:30
});
describe('next button behavior', () => {
beforeEach(() => {
findNextButton().trigger('click');
});
2022-06-21 17:19:12 +05:30
it('emits an "next" event with the "after" cursor', () => {
expect(onNext.mock.calls).toEqual([[endCursor]]);
2021-06-08 01:23:25 +05:30
});
it('calls historyPushState with the new URL', () => {
expect(historyPushState.mock.calls).toEqual([
2022-06-21 17:19:12 +05:30
[expect.stringContaining(`?after=${endCursor}`)],
2021-06-08 01:23:25 +05:30
]);
});
});
2022-06-21 17:19:12 +05:30
describe('prev button behavior', () => {
2021-06-08 01:23:25 +05:30
beforeEach(() => {
findPrevButton().trigger('click');
});
2022-06-21 17:19:12 +05:30
it('emits an "prev" event with the "before" cursor', () => {
expect(onPrev.mock.calls).toEqual([[startCursor]]);
2021-06-08 01:23:25 +05:30
});
it('calls historyPushState with the new URL', () => {
expect(historyPushState.mock.calls).toEqual([
2022-06-21 17:19:12 +05:30
[expect.stringContaining(`?before=${startCursor}`)],
2021-06-08 01:23:25 +05:30
]);
});
2020-11-24 15:15:51 +05:30
});
});
});