debian-mirror-gitlab/spec/frontend/branches/components/delete_branch_modal_spec.js

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

191 lines
6.2 KiB
JavaScript
Raw Normal View History

2021-09-04 01:27:46 +05:30
import { GlButton, GlModal, GlFormInput, GlSprintf } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { stubComponent } from 'helpers/stub_component';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import DeleteBranchModal from '~/branches/components/delete_branch_modal.vue';
import eventHub from '~/branches/event_hub';
let wrapper;
2023-06-20 00:43:36 +05:30
let showMock;
let hideMock;
2021-09-04 01:27:46 +05:30
const branchName = 'test_modal';
const defaultBranchName = 'default';
const deletePath = '/path/to/branch';
const merged = false;
const isProtectedBranch = false;
2023-06-20 00:43:36 +05:30
const createComponent = () => {
showMock = jest.fn();
hideMock = jest.fn();
2021-09-04 01:27:46 +05:30
wrapper = extendedWrapper(
shallowMount(DeleteBranchModal, {
stubs: {
GlModal: stubComponent(GlModal, {
template:
'<div><slot name="modal-title"></slot><slot></slot><slot name="modal-footer"></slot></div>',
2023-06-20 00:43:36 +05:30
methods: {
show: showMock,
hide: hideMock,
},
2021-09-04 01:27:46 +05:30
}),
GlButton,
GlFormInput,
GlSprintf,
},
}),
);
};
const findModal = () => wrapper.findComponent(GlModal);
const findModalMessage = () => wrapper.findByTestId('modal-message');
const findDeleteButton = () => wrapper.findByTestId('delete-branch-confirmation-button');
const findCancelButton = () => wrapper.findByTestId('delete-branch-cancel-button');
const findFormInput = () => wrapper.findComponent(GlFormInput);
const findForm = () => wrapper.find('form');
2023-06-20 00:43:36 +05:30
const createSubmitFormSpy = () => jest.spyOn(findForm().element, 'submit');
const emitOpenModal = (data = {}) =>
eventHub.$emit('openModal', {
isProtectedBranch,
branchName,
defaultBranchName,
deletePath,
merged,
...data,
});
2021-09-04 01:27:46 +05:30
describe('Delete branch modal', () => {
const expectedUnmergedWarning =
2022-07-16 23:28:13 +05:30
"This branch hasn't been merged into default. To avoid data loss, consider merging this branch before deleting it.";
2021-09-04 01:27:46 +05:30
2023-06-20 00:43:36 +05:30
beforeEach(() => {
createComponent();
emitOpenModal();
showMock.mockClear();
hideMock.mockClear();
});
2021-09-04 01:27:46 +05:30
describe('Deleting a regular branch', () => {
const expectedTitle = 'Delete branch. Are you ABSOLUTELY SURE?';
const expectedWarning = "You're about to permanently delete the branch test_modal.";
const expectedMessage = `${expectedWarning} ${expectedUnmergedWarning}`;
it('renders the modal correctly', () => {
expect(findModal().props('title')).toBe(expectedTitle);
expect(findModalMessage().text()).toMatchInterpolatedText(expectedMessage);
expect(findCancelButton().text()).toBe('Cancel, keep branch');
expect(findDeleteButton().text()).toBe('Yes, delete branch');
expect(findForm().attributes('action')).toBe(deletePath);
});
it('submits the form when the delete button is clicked', () => {
2023-06-20 00:43:36 +05:30
const submitSpy = createSubmitFormSpy();
expect(submitSpy).not.toHaveBeenCalled();
2021-09-04 01:27:46 +05:30
findDeleteButton().trigger('click');
expect(findForm().attributes('action')).toBe(deletePath);
2023-06-20 00:43:36 +05:30
expect(submitSpy).toHaveBeenCalled();
2021-09-04 01:27:46 +05:30
});
2023-06-20 00:43:36 +05:30
it('calls show on the modal when a `openModal` event is received through the event hub', () => {
expect(showMock).not.toHaveBeenCalled();
2021-09-04 01:27:46 +05:30
2023-06-20 00:43:36 +05:30
emitOpenModal();
2021-09-04 01:27:46 +05:30
2023-06-20 00:43:36 +05:30
expect(showMock).toHaveBeenCalled();
2021-09-04 01:27:46 +05:30
});
it('calls hide on the modal when cancel button is clicked', () => {
2023-06-20 00:43:36 +05:30
expect(hideMock).not.toHaveBeenCalled();
2021-09-04 01:27:46 +05:30
findCancelButton().trigger('click');
2023-06-20 00:43:36 +05:30
expect(hideMock).toHaveBeenCalled();
2021-09-04 01:27:46 +05:30
});
});
describe('Deleting a protected branch (for owner or maintainer)', () => {
const expectedTitleProtected = 'Delete protected branch. Are you ABSOLUTELY SURE?';
const expectedWarningProtected =
"You're about to permanently delete the protected branch test_modal.";
const expectedMessageProtected = `${expectedWarningProtected} ${expectedUnmergedWarning}`;
const expectedConfirmationText =
2022-07-16 23:28:13 +05:30
'After you confirm and select Yes, delete protected branch, you cannot recover this branch. Please type the following to confirm: test_modal';
2021-09-04 01:27:46 +05:30
beforeEach(() => {
2023-06-20 00:43:36 +05:30
emitOpenModal({
isProtectedBranch: true,
});
2021-09-04 01:27:46 +05:30
});
describe('rendering the modal correctly for a protected branch', () => {
it('sets the modal title for a protected branch', () => {
expect(findModal().props('title')).toBe(expectedTitleProtected);
});
it('renders the correct text in the modal message', () => {
expect(findModalMessage().text()).toMatchInterpolatedText(expectedMessageProtected);
});
it('renders the protected branch name confirmation form with expected text and action', () => {
expect(findForm().text()).toMatchInterpolatedText(expectedConfirmationText);
expect(findForm().attributes('action')).toBe(deletePath);
});
it('renders the buttons with the correct button text', () => {
expect(findCancelButton().text()).toBe('Cancel, keep branch');
expect(findDeleteButton().text()).toBe('Yes, delete protected branch');
});
});
2023-01-13 00:05:48 +05:30
it('opens with the delete button disabled and doesn`t fire submit when clicked or pressed enter', async () => {
expect(findDeleteButton().props('disabled')).toBe(true);
findFormInput().vm.$emit('input', 'hello');
await waitForPromises();
2023-06-20 00:43:36 +05:30
const submitSpy = createSubmitFormSpy();
2023-01-13 00:05:48 +05:30
findDeleteButton().trigger('click');
2023-06-20 00:43:36 +05:30
expect(submitSpy).not.toHaveBeenCalled();
2023-01-13 00:05:48 +05:30
});
it('opens with the delete button disabled and enables it when branch name is confirmed and fires submit', async () => {
2021-09-04 01:27:46 +05:30
expect(findDeleteButton().props('disabled')).toBe(true);
findFormInput().vm.$emit('input', branchName);
await waitForPromises();
expect(findDeleteButton().props('disabled')).not.toBe(true);
2023-01-13 00:05:48 +05:30
2023-06-20 00:43:36 +05:30
const submitSpy = createSubmitFormSpy();
expect(submitSpy).not.toHaveBeenCalled();
2023-01-13 00:05:48 +05:30
findDeleteButton().trigger('click');
2023-06-20 00:43:36 +05:30
expect(submitSpy).toHaveBeenCalled();
2021-09-04 01:27:46 +05:30
});
});
describe('Deleting a merged branch', () => {
2023-06-20 00:43:36 +05:30
beforeEach(() => {
emitOpenModal({ merged: true });
});
2021-09-04 01:27:46 +05:30
2023-06-20 00:43:36 +05:30
it('does not include the unmerged branch warning when merged is true', () => {
expect(findModalMessage().text()).not.toContain(expectedUnmergedWarning);
2021-09-04 01:27:46 +05:30
});
});
});