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

177 lines
4.6 KiB
JavaScript
Raw Normal View History

2021-06-08 01:23:25 +05:30
import { GlKeysetPagination } from '@gitlab/ui';
2022-04-04 11:22:00 +05:30
import { mount } from '@vue/test-utils';
import Vue from 'vue';
2020-11-24 15:15:51 +05:30
import Vuex from 'vuex';
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
import createStore from '~/releases/stores';
import createIndexModule from '~/releases/stores/modules/index';
jest.mock('~/lib/utils/common_utils', () => ({
...jest.requireActual('~/lib/utils/common_utils'),
historyPushState: jest.fn(),
}));
2020-11-24 15:15:51 +05:30
2022-04-04 11:22:00 +05:30
Vue.use(Vuex);
2020-11-24 15:15:51 +05:30
describe('~/releases/components/releases_pagination.vue', () => {
let wrapper;
2021-06-08 01:23:25 +05:30
let indexModule;
2020-11-24 15:15:51 +05:30
2021-06-08 01:23:25 +05:30
const cursors = {
startCursor: 'startCursor',
endCursor: 'endCursor',
};
const projectPath = 'my/project';
const createComponent = (pageInfo) => {
indexModule = createIndexModule({ projectPath });
indexModule.state.pageInfo = pageInfo;
2020-11-24 15:15:51 +05:30
2021-06-08 01:23:25 +05:30
indexModule.actions.fetchReleases = jest.fn();
wrapper = mount(ReleasesPagination, {
store: createStore({
modules: {
index: indexModule,
},
featureFlags: {},
}),
});
2020-11-24 15:15:51 +05:30
};
afterEach(() => {
wrapper.destroy();
wrapper = null;
});
2021-06-08 01:23:25 +05:30
const findGlKeysetPagination = () => wrapper.findComponent(GlKeysetPagination);
const findPrevButton = () => findGlKeysetPagination().find('[data-testid="prevButton"]');
const findNextButton = () => findGlKeysetPagination().find('[data-testid="nextButton"]');
const expectDisabledPrev = () => {
expect(findPrevButton().attributes().disabled).toBe('disabled');
};
const expectEnabledPrev = () => {
expect(findPrevButton().attributes().disabled).toBe(undefined);
};
const expectDisabledNext = () => {
expect(findNextButton().attributes().disabled).toBe('disabled');
};
const expectEnabledNext = () => {
expect(findNextButton().attributes().disabled).toBe(undefined);
};
describe('when there is only one page of results', () => {
beforeEach(() => {
createComponent({
hasPreviousPage: false,
hasNextPage: false,
});
});
2020-11-24 15:15:51 +05:30
2021-06-08 01:23:25 +05:30
it('does not render a GlKeysetPagination', () => {
expect(findGlKeysetPagination().exists()).toBe(false);
});
});
describe('when there is a next page, but not a previous page', () => {
2020-11-24 15:15:51 +05:30
beforeEach(() => {
2021-06-08 01:23:25 +05:30
createComponent({
hasPreviousPage: false,
hasNextPage: true,
});
2020-11-24 15:15:51 +05:30
});
2021-06-08 01:23:25 +05:30
it('renders a disabled "Prev" button', () => {
expectDisabledPrev();
});
it('renders an enabled "Next" button', () => {
expectEnabledNext();
2020-11-24 15:15:51 +05:30
});
});
2021-06-08 01:23:25 +05:30
describe('when there is a previous page, but not a next page', () => {
2020-11-24 15:15:51 +05:30
beforeEach(() => {
2021-06-08 01:23:25 +05:30
createComponent({
hasPreviousPage: true,
hasNextPage: false,
});
2020-11-24 15:15:51 +05:30
});
2021-06-08 01:23:25 +05:30
it('renders a enabled "Prev" button', () => {
expectEnabledPrev();
});
it('renders an disabled "Next" button', () => {
expectDisabledNext();
});
});
describe('when there is both a previous page and a next page', () => {
beforeEach(() => {
createComponent({
hasPreviousPage: true,
hasNextPage: true,
});
});
it('renders a enabled "Prev" button', () => {
expectEnabledPrev();
});
it('renders an enabled "Next" button', () => {
expectEnabledNext();
});
});
describe('button behavior', () => {
beforeEach(() => {
createComponent({
hasPreviousPage: true,
hasNextPage: true,
...cursors,
});
});
describe('next button behavior', () => {
beforeEach(() => {
findNextButton().trigger('click');
});
it('calls fetchReleases with the correct after cursor', () => {
expect(indexModule.actions.fetchReleases.mock.calls).toEqual([
[expect.anything(), { after: cursors.endCursor }],
]);
});
it('calls historyPushState with the new URL', () => {
expect(historyPushState.mock.calls).toEqual([
[expect.stringContaining(`?after=${cursors.endCursor}`)],
]);
});
});
describe('previous button behavior', () => {
beforeEach(() => {
findPrevButton().trigger('click');
});
it('calls fetchReleases with the correct before cursor', () => {
expect(indexModule.actions.fetchReleases.mock.calls).toEqual([
[expect.anything(), { before: cursors.startCursor }],
]);
});
it('calls historyPushState with the new URL', () => {
expect(historyPushState.mock.calls).toEqual([
[expect.stringContaining(`?before=${cursors.startCursor}`)],
]);
});
2020-11-24 15:15:51 +05:30
});
});
});