debian-mirror-gitlab/spec/frontend/boards/components/board_form_spec.js

350 lines
11 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { GlModal } from '@gitlab/ui';
2021-02-22 17:27:13 +05:30
import { shallowMount } from '@vue/test-utils';
2021-10-27 15:23:28 +05:30
import setWindowLocation from 'helpers/set_window_location_helper';
2021-02-22 17:27:13 +05:30
import waitForPromises from 'helpers/wait_for_promises';
import BoardForm from '~/boards/components/board_form.vue';
2021-03-11 19:13:27 +05:30
import { formType } from '~/boards/constants';
2021-03-08 18:12:59 +05:30
import createBoardMutation from '~/boards/graphql/board_create.mutation.graphql';
import destroyBoardMutation from '~/boards/graphql/board_destroy.mutation.graphql';
2021-03-11 19:13:27 +05:30
import updateBoardMutation from '~/boards/graphql/board_update.mutation.graphql';
2021-04-17 20:07:23 +05:30
import { createStore } from '~/boards/stores';
2021-03-11 19:13:27 +05:30
import { visitUrl } from '~/lib/utils/url_utility';
2021-02-22 17:27:13 +05:30
jest.mock('~/lib/utils/url_utility', () => ({
2021-09-30 23:02:18 +05:30
...jest.requireActual('~/lib/utils/url_utility'),
2021-02-22 17:27:13 +05:30
visitUrl: jest.fn().mockName('visitUrlMock'),
}));
const currentBoard = {
2021-12-11 22:18:48 +05:30
id: 'gid://gitlab/Board/1',
2021-02-22 17:27:13 +05:30
name: 'test',
labels: [],
2021-12-11 22:18:48 +05:30
milestone: {},
2021-02-22 17:27:13 +05:30
assignee: {},
weight: null,
2021-12-11 22:18:48 +05:30
hideBacklogList: false,
hideClosedList: false,
2021-02-22 17:27:13 +05:30
};
const defaultProps = {
canAdminBoard: false,
currentBoard,
2021-03-11 19:13:27 +05:30
currentPage: '',
2021-02-22 17:27:13 +05:30
};
2020-04-22 19:07:51 +05:30
2021-02-22 17:27:13 +05:30
describe('BoardForm', () => {
2020-04-22 19:07:51 +05:30
let wrapper;
2021-03-08 18:12:59 +05:30
let mutate;
2020-04-22 19:07:51 +05:30
2021-02-22 17:27:13 +05:30
const findModal = () => wrapper.find(GlModal);
const findModalActionPrimary = () => findModal().props('actionPrimary');
const findForm = () => wrapper.find('[data-testid="board-form"]');
const findFormWrapper = () => wrapper.find('[data-testid="board-form-wrapper"]');
const findDeleteConfirmation = () => wrapper.find('[data-testid="delete-confirmation-message"]');
const findInput = () => wrapper.find('#board-new-name');
2020-04-22 19:07:51 +05:30
2021-04-17 20:07:23 +05:30
const store = createStore({
getters: {
isGroupBoard: () => true,
isProjectBoard: () => false,
},
});
2021-02-22 17:27:13 +05:30
const createComponent = (props, data) => {
wrapper = shallowMount(BoardForm, {
propsData: { ...defaultProps, ...props },
data() {
return {
...data,
};
},
provide: {
2021-03-08 18:12:59 +05:30
rootPath: 'root',
2021-02-22 17:27:13 +05:30
},
mocks: {
$apollo: {
mutate,
},
},
2021-04-17 20:07:23 +05:30
store,
2021-03-08 18:12:59 +05:30
attachTo: document.body,
2021-02-22 17:27:13 +05:30
});
};
2020-04-22 19:07:51 +05:30
afterEach(() => {
wrapper.destroy();
wrapper = null;
2021-03-08 18:12:59 +05:30
mutate = null;
2020-04-22 19:07:51 +05:30
});
2021-02-22 17:27:13 +05:30
describe('when user can not admin the board', () => {
beforeEach(() => {
2021-03-11 19:13:27 +05:30
createComponent({ currentPage: formType.new });
2021-02-22 17:27:13 +05:30
});
it('hides modal footer when user is not a board admin', () => {
expect(findModal().attributes('hide-footer')).toBeDefined();
});
it('displays board scope title', () => {
expect(findModal().attributes('title')).toBe('Board scope');
});
it('does not display a form', () => {
expect(findForm().exists()).toBe(false);
});
});
describe('when user can admin the board', () => {
beforeEach(() => {
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.new });
2021-02-22 17:27:13 +05:30
});
it('shows modal footer when user is a board admin', () => {
expect(findModal().attributes('hide-footer')).toBeUndefined();
});
it('displays a form', () => {
expect(findForm().exists()).toBe(true);
});
it('focuses an input field', async () => {
expect(document.activeElement).toBe(wrapper.vm.$refs.name);
});
});
describe('when creating a new board', () => {
describe('on non-scoped-board', () => {
beforeEach(() => {
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.new });
2021-02-22 17:27:13 +05:30
});
it('clears the form', () => {
2021-03-08 18:12:59 +05:30
expect(findInput().element.value).toBe('');
2021-02-22 17:27:13 +05:30
});
it('shows a correct title about creating a board', () => {
expect(findModal().attributes('title')).toBe('Create new board');
});
it('passes correct primary action text and variant', () => {
expect(findModalActionPrimary().text).toBe('Create board');
2022-04-04 11:22:00 +05:30
expect(findModalActionPrimary().attributes[0].variant).toBe('confirm');
2021-02-22 17:27:13 +05:30
});
it('does not render delete confirmation message', () => {
expect(findDeleteConfirmation().exists()).toBe(false);
});
it('renders form wrapper', () => {
expect(findFormWrapper().exists()).toBe(true);
});
});
describe('when submitting a create event', () => {
2021-03-08 18:12:59 +05:30
const fillForm = () => {
findInput().value = 'Test name';
findInput().trigger('input');
findInput().trigger('keyup.enter', { metaKey: true });
};
2021-02-22 17:27:13 +05:30
beforeEach(() => {
2021-03-08 18:12:59 +05:30
mutate = jest.fn().mockResolvedValue({
data: {
createBoard: { board: { id: 'gid://gitlab/Board/123', webPath: 'test-path' } },
},
});
2021-02-22 17:27:13 +05:30
});
it('does not call API if board name is empty', async () => {
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.new });
2021-02-22 17:27:13 +05:30
findInput().trigger('keyup.enter', { metaKey: true });
await waitForPromises();
expect(mutate).not.toHaveBeenCalled();
});
2021-03-08 18:12:59 +05:30
it('calls a correct GraphQL mutation and redirects to correct page from existing board', async () => {
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.new });
2021-03-08 18:12:59 +05:30
fillForm();
2021-02-22 17:27:13 +05:30
await waitForPromises();
expect(mutate).toHaveBeenCalledWith({
mutation: createBoardMutation,
variables: {
2021-03-08 18:12:59 +05:30
input: expect.objectContaining({
name: 'test',
}),
2021-02-22 17:27:13 +05:30
},
});
await waitForPromises();
2021-03-08 18:12:59 +05:30
expect(visitUrl).toHaveBeenCalledWith('test-path');
});
2021-09-04 01:27:46 +05:30
it('shows a GlAlert if GraphQL mutation fails', async () => {
2021-03-08 18:12:59 +05:30
mutate = jest.fn().mockRejectedValue('Houston, we have a problem');
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.new });
2021-09-04 01:27:46 +05:30
jest.spyOn(wrapper.vm, 'setError').mockImplementation(() => {});
2021-03-08 18:12:59 +05:30
fillForm();
await waitForPromises();
expect(mutate).toHaveBeenCalled();
await waitForPromises();
expect(visitUrl).not.toHaveBeenCalled();
2021-09-04 01:27:46 +05:30
expect(wrapper.vm.setError).toHaveBeenCalled();
2020-04-22 19:07:51 +05:30
});
});
});
2021-02-22 17:27:13 +05:30
describe('when editing a board', () => {
describe('on non-scoped-board', () => {
beforeEach(() => {
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.edit });
2021-02-22 17:27:13 +05:30
});
it('clears the form', () => {
2021-03-08 18:12:59 +05:30
expect(findInput().element.value).toEqual(currentBoard.name);
2021-02-22 17:27:13 +05:30
});
it('shows a correct title about creating a board', () => {
expect(findModal().attributes('title')).toBe('Edit board');
});
it('passes correct primary action text and variant', () => {
expect(findModalActionPrimary().text).toBe('Save changes');
2021-04-29 21:17:54 +05:30
expect(findModalActionPrimary().attributes[0].variant).toBe('confirm');
2021-02-22 17:27:13 +05:30
});
it('does not render delete confirmation message', () => {
expect(findDeleteConfirmation().exists()).toBe(false);
});
it('renders form wrapper', () => {
expect(findFormWrapper().exists()).toBe(true);
});
2021-03-08 18:12:59 +05:30
});
2021-02-22 17:27:13 +05:30
2021-03-08 18:12:59 +05:30
it('calls GraphQL mutation with correct parameters when issues are not grouped', async () => {
mutate = jest.fn().mockResolvedValue({
data: {
updateBoard: { board: { id: 'gid://gitlab/Board/321', webPath: 'test-path' } },
},
2021-02-22 17:27:13 +05:30
});
2021-10-27 15:23:28 +05:30
setWindowLocation('https://test/boards/1');
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.edit });
2021-02-22 17:27:13 +05:30
2021-03-08 18:12:59 +05:30
findInput().trigger('keyup.enter', { metaKey: true });
await waitForPromises();
expect(mutate).toHaveBeenCalledWith({
mutation: updateBoardMutation,
variables: {
input: expect.objectContaining({
2021-12-11 22:18:48 +05:30
id: currentBoard.id,
2021-03-08 18:12:59 +05:30
}),
},
2021-02-22 17:27:13 +05:30
});
2021-03-08 18:12:59 +05:30
await waitForPromises();
expect(visitUrl).toHaveBeenCalledWith('test-path');
});
2021-02-22 17:27:13 +05:30
2021-03-08 18:12:59 +05:30
it('calls GraphQL mutation with correct parameters when issues are grouped by epic', async () => {
mutate = jest.fn().mockResolvedValue({
data: {
updateBoard: { board: { id: 'gid://gitlab/Board/321', webPath: 'test-path' } },
},
});
2021-10-27 15:23:28 +05:30
setWindowLocation('https://test/boards/1?group_by=epic');
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.edit });
2021-02-22 17:27:13 +05:30
2021-03-08 18:12:59 +05:30
findInput().trigger('keyup.enter', { metaKey: true });
2021-02-22 17:27:13 +05:30
2021-03-08 18:12:59 +05:30
await waitForPromises();
2020-04-22 19:07:51 +05:30
2021-03-08 18:12:59 +05:30
expect(mutate).toHaveBeenCalledWith({
mutation: updateBoardMutation,
variables: {
input: expect.objectContaining({
2021-12-11 22:18:48 +05:30
id: currentBoard.id,
2021-03-08 18:12:59 +05:30
}),
},
2020-04-22 19:07:51 +05:30
});
2021-03-08 18:12:59 +05:30
await waitForPromises();
expect(visitUrl).toHaveBeenCalledWith('test-path?group_by=epic');
});
2021-09-04 01:27:46 +05:30
it('shows a GlAlert if GraphQL mutation fails', async () => {
2021-03-08 18:12:59 +05:30
mutate = jest.fn().mockRejectedValue('Houston, we have a problem');
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.edit });
2021-09-04 01:27:46 +05:30
jest.spyOn(wrapper.vm, 'setError').mockImplementation(() => {});
2021-03-08 18:12:59 +05:30
findInput().trigger('keyup.enter', { metaKey: true });
await waitForPromises();
expect(mutate).toHaveBeenCalled();
await waitForPromises();
expect(visitUrl).not.toHaveBeenCalled();
2021-09-04 01:27:46 +05:30
expect(wrapper.vm.setError).toHaveBeenCalled();
2021-03-08 18:12:59 +05:30
});
});
describe('when deleting a board', () => {
it('passes correct primary action text and variant', () => {
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.delete });
2021-03-08 18:12:59 +05:30
expect(findModalActionPrimary().text).toBe('Delete');
expect(findModalActionPrimary().attributes[0].variant).toBe('danger');
});
it('renders delete confirmation message', () => {
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.delete });
2021-03-08 18:12:59 +05:30
expect(findDeleteConfirmation().exists()).toBe(true);
});
it('calls a correct GraphQL mutation and redirects to correct page after deleting board', async () => {
mutate = jest.fn().mockResolvedValue({});
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.delete });
2021-03-08 18:12:59 +05:30
findModal().vm.$emit('primary');
await waitForPromises();
expect(mutate).toHaveBeenCalledWith({
mutation: destroyBoardMutation,
variables: {
2021-12-11 22:18:48 +05:30
id: currentBoard.id,
2021-03-08 18:12:59 +05:30
},
});
await waitForPromises();
expect(visitUrl).toHaveBeenCalledWith('root');
});
2021-09-04 01:27:46 +05:30
it('dispatches `setError` action when GraphQL mutation fails', async () => {
2021-03-08 18:12:59 +05:30
mutate = jest.fn().mockRejectedValue('Houston, we have a problem');
2021-03-11 19:13:27 +05:30
createComponent({ canAdminBoard: true, currentPage: formType.delete });
2021-09-04 01:27:46 +05:30
jest.spyOn(wrapper.vm, 'setError').mockImplementation(() => {});
2021-03-08 18:12:59 +05:30
findModal().vm.$emit('primary');
await waitForPromises();
expect(mutate).toHaveBeenCalled();
await waitForPromises();
expect(visitUrl).not.toHaveBeenCalled();
2021-09-04 01:27:46 +05:30
expect(wrapper.vm.setError).toHaveBeenCalled();
2020-04-22 19:07:51 +05:30
});
});
});