debian-mirror-gitlab/spec/frontend/issue_show/components/edit_actions_spec.js

164 lines
4.9 KiB
JavaScript
Raw Normal View History

2021-09-04 01:27:46 +05:30
import { GlButton, GlModal } from '@gitlab/ui';
import { createLocalVue } from '@vue/test-utils';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import IssuableEditActions from '~/issue_show/components/edit_actions.vue';
2017-09-10 17:25:29 +05:30
import eventHub from '~/issue_show/event_hub';
2021-09-04 01:27:46 +05:30
import {
getIssueStateQueryResponse,
updateIssueStateQueryResponse,
} from '../mock_data/apollo_mock';
const localVue = createLocalVue();
localVue.use(VueApollo);
describe('Edit Actions component', () => {
let wrapper;
let fakeApollo;
let mockIssueStateData;
const mockResolvers = {
Query: {
issueState() {
return {
__typename: 'IssueState',
rawData: mockIssueStateData(),
};
},
},
};
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
const modalId = 'delete-issuable-modal-1';
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
const createComponent = ({ props, data } = {}) => {
fakeApollo = createMockApollo([], mockResolvers);
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
wrapper = shallowMountExtended(IssuableEditActions, {
apolloProvider: fakeApollo,
2017-09-10 17:25:29 +05:30
propsData: {
2021-09-04 01:27:46 +05:30
formState: {
title: 'GitLab Issue',
},
2017-09-10 17:25:29 +05:30
canDestroy: true,
2018-12-05 23:21:45 +05:30
issuableType: 'issue',
2021-09-04 01:27:46 +05:30
...props,
2017-09-10 17:25:29 +05:30
},
2021-09-04 01:27:46 +05:30
data() {
return {
issueState: {},
modalId,
...data,
};
},
});
};
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
async function deleteIssuable(localWrapper) {
localWrapper.findComponent(GlModal).vm.$emit('primary');
}
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
const findModal = () => wrapper.findComponent(GlModal);
const findEditButtons = () => wrapper.findAllComponents(GlButton);
const findDeleteButton = () => wrapper.findByTestId('issuable-delete-button');
const findSaveButton = () => wrapper.findByTestId('issuable-save-button');
const findCancelButton = () => wrapper.findByTestId('issuable-cancel-button');
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
beforeEach(() => {
mockIssueStateData = jest.fn();
createComponent();
2017-09-10 17:25:29 +05:30
});
2021-09-04 01:27:46 +05:30
afterEach(() => {
wrapper.destroy();
});
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
it('renders all buttons as enabled', () => {
const buttons = findEditButtons().wrappers;
buttons.forEach((button) => {
expect(button.attributes('disabled')).toBeFalsy();
2017-09-10 17:25:29 +05:30
});
});
2021-09-04 01:27:46 +05:30
it('does not render the delete button if canDestroy is false', () => {
createComponent({ props: { canDestroy: false } });
expect(findDeleteButton().exists()).toBe(false);
});
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
it('disables save button when title is blank', () => {
createComponent({ props: { formState: { title: '', issue_type: '' } } });
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
expect(findSaveButton().attributes('disabled')).toBe('true');
2017-09-10 17:25:29 +05:30
});
2021-09-04 01:27:46 +05:30
it('does not render the delete button if showDeleteButton is false', () => {
createComponent({ props: { showDeleteButton: false } });
2018-03-17 18:26:18 +05:30
2021-09-04 01:27:46 +05:30
expect(findDeleteButton().exists()).toBe(false);
2018-03-17 18:26:18 +05:30
});
2017-09-10 17:25:29 +05:30
describe('updateIssuable', () => {
2021-09-04 01:27:46 +05:30
beforeEach(() => {
jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
2017-09-10 17:25:29 +05:30
});
2021-09-04 01:27:46 +05:30
it('sends update.issauble event when clicking save button', () => {
findSaveButton().vm.$emit('click', { preventDefault: jest.fn() });
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
expect(eventHub.$emit).toHaveBeenCalledWith('update.issuable');
2017-09-10 17:25:29 +05:30
});
});
describe('closeForm', () => {
2021-09-04 01:27:46 +05:30
beforeEach(() => {
jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
});
2017-09-10 17:25:29 +05:30
it('emits close.form when clicking cancel', () => {
2021-09-04 01:27:46 +05:30
findCancelButton().vm.$emit('click');
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect(eventHub.$emit).toHaveBeenCalledWith('close.form');
2017-09-10 17:25:29 +05:30
});
});
2021-09-04 01:27:46 +05:30
describe('renders create modal with the correct information', () => {
it('renders correct modal id', () => {
expect(findModal().attributes('modalid')).toBe(modalId);
});
});
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
describe('deleteIssuable', () => {
beforeEach(() => {
jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
2017-09-10 17:25:29 +05:30
});
2021-09-04 01:27:46 +05:30
it('does not send the `delete.issuable` event when clicking delete button', () => {
findDeleteButton().vm.$emit('click');
expect(eventHub.$emit).not.toHaveBeenCalled();
});
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
it('sends the `delete.issuable` event when clicking the delete confirm button', async () => {
expect(eventHub.$emit).toHaveBeenCalledTimes(0);
await deleteIssuable(wrapper);
expect(eventHub.$emit).toHaveBeenCalledWith('delete.issuable', { destroy_confirm: true });
expect(eventHub.$emit).toHaveBeenCalledTimes(1);
});
});
2018-12-13 13:39:08 +05:30
2021-09-04 01:27:46 +05:30
describe('with Apollo cache mock', () => {
it('renders the right delete button text per apollo cache type', async () => {
mockIssueStateData.mockResolvedValue(getIssueStateQueryResponse);
await waitForPromises();
expect(findDeleteButton().text()).toBe('Delete issue');
});
2017-09-10 17:25:29 +05:30
2021-09-04 01:27:46 +05:30
it('should not change the delete button text per apollo cache mutation', async () => {
mockIssueStateData.mockResolvedValue(updateIssueStateQueryResponse);
await waitForPromises();
expect(findDeleteButton().text()).toBe('Delete issue');
2017-09-10 17:25:29 +05:30
});
});
});