debian-mirror-gitlab/spec/frontend/issues/show/components/form_spec.js

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

206 lines
6.2 KiB
JavaScript
Raw Normal View History

2021-06-08 01:23:25 +05:30
import { GlAlert } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
2022-11-25 23:54:43 +05:30
import { getDraft, updateDraft, clearDraft, getLockVersion } from '~/lib/utils/autosave';
2022-01-26 12:08:38 +05:30
import DescriptionTemplate from '~/issues/show/components/fields/description_template.vue';
2022-11-25 23:54:43 +05:30
import IssuableTitleField from '~/issues/show/components/fields/title.vue';
import DescriptionField from '~/issues/show/components/fields/description.vue';
2022-01-26 12:08:38 +05:30
import IssueTypeField from '~/issues/show/components/fields/type.vue';
import formComponent from '~/issues/show/components/form.vue';
import LockedWarning from '~/issues/show/components/locked_warning.vue';
import eventHub from '~/issues/show/event_hub';
2017-09-10 17:25:29 +05:30
2022-11-25 23:54:43 +05:30
jest.mock('~/lib/utils/autosave');
2020-05-24 23:13:21 +05:30
2017-09-10 17:25:29 +05:30
describe('Inline edit form component', () => {
2021-06-08 01:23:25 +05:30
let wrapper;
2019-10-12 21:52:04 +05:30
const defaultProps = {
canDestroy: true,
2022-01-26 12:08:38 +05:30
endpoint: 'gitlab-org/gitlab-test/-/issues/1',
2019-10-12 21:52:04 +05:30
formState: {
title: 'b',
description: 'a',
lockedWarningVisible: false,
},
issuableType: 'issue',
markdownPreviewPath: '/',
markdownDocsPath: '/',
projectPath: '/',
2021-03-11 19:13:27 +05:30
projectId: 1,
2019-10-12 21:52:04 +05:30
projectNamespace: '/',
};
2021-03-08 18:12:59 +05:30
const createComponent = (props) => {
2021-06-08 01:23:25 +05:30
wrapper = shallowMount(formComponent, {
propsData: {
...defaultProps,
...props,
},
2022-11-25 23:54:43 +05:30
stubs: {
DescriptionField,
},
2019-10-12 21:52:04 +05:30
});
};
2017-09-10 17:25:29 +05:30
2022-11-25 23:54:43 +05:30
const findTitleField = () => wrapper.findComponent(IssuableTitleField);
const findDescriptionField = () => wrapper.findComponent(DescriptionField);
2021-06-08 01:23:25 +05:30
const findDescriptionTemplate = () => wrapper.findComponent(DescriptionTemplate);
2021-09-04 01:27:46 +05:30
const findIssuableTypeField = () => wrapper.findComponent(IssueTypeField);
2021-06-08 01:23:25 +05:30
const findLockedWarning = () => wrapper.findComponent(LockedWarning);
const findAlert = () => wrapper.findComponent(GlAlert);
2017-09-10 17:25:29 +05:30
it('does not render template selector if no templates exist', () => {
2019-10-12 21:52:04 +05:30
createComponent();
2021-06-08 01:23:25 +05:30
expect(findDescriptionTemplate().exists()).toBe(false);
2017-09-10 17:25:29 +05:30
});
2021-04-17 20:07:23 +05:30
it('renders template selector when templates as array exists', () => {
2021-03-11 19:13:27 +05:30
createComponent({
issuableTemplates: [
{ name: 'test', id: 'test', project_path: 'test', namespace_path: 'test' },
],
});
2017-09-10 17:25:29 +05:30
2021-06-08 01:23:25 +05:30
expect(findDescriptionTemplate().exists()).toBe(true);
2017-09-10 17:25:29 +05:30
});
2021-04-17 20:07:23 +05:30
it('renders template selector when templates as hash exists', () => {
createComponent({
issuableTemplates: {
test: [{ name: 'test', id: 'test', project_path: 'test', namespace_path: 'test' }],
},
});
2021-06-08 01:23:25 +05:30
expect(findDescriptionTemplate().exists()).toBe(true);
2021-04-17 20:07:23 +05:30
});
2021-09-04 01:27:46 +05:30
it.each`
issuableType | value
${'issue'} | ${true}
${'epic'} | ${false}
`(
'when `issue_type` is set to "$issuableType" rendering the type select will be "$value"',
({ issuableType, value }) => {
createComponent({
issuableType,
});
expect(findIssuableTypeField().exists()).toBe(value);
},
);
2017-09-10 17:25:29 +05:30
it('hides locked warning by default', () => {
2019-10-12 21:52:04 +05:30
createComponent();
2021-06-08 01:23:25 +05:30
expect(findLockedWarning().exists()).toBe(false);
2017-09-10 17:25:29 +05:30
});
2019-10-12 21:52:04 +05:30
it('shows locked warning if formState is different', () => {
createComponent({ formState: { ...defaultProps.formState, lockedWarningVisible: true } });
2017-09-10 17:25:29 +05:30
2021-06-08 01:23:25 +05:30
expect(findLockedWarning().exists()).toBe(true);
2019-10-12 21:52:04 +05:30
});
2017-09-10 17:25:29 +05:30
2019-10-12 21:52:04 +05:30
it('hides locked warning when currently saving', () => {
createComponent({
formState: { ...defaultProps.formState, updateLoading: true, lockedWarningVisible: true },
2017-09-10 17:25:29 +05:30
});
2019-07-31 22:56:46 +05:30
2021-06-08 01:23:25 +05:30
expect(findLockedWarning().exists()).toBe(false);
2019-07-31 22:56:46 +05:30
});
2019-10-12 21:52:04 +05:30
describe('autosave', () => {
beforeEach(() => {
2022-11-25 23:54:43 +05:30
getDraft.mockImplementation((autosaveKey) => {
return autosaveKey[autosaveKey.length - 1];
});
2019-10-12 21:52:04 +05:30
});
2022-11-25 23:54:43 +05:30
it('initializes title and description fields with saved drafts', () => {
2019-10-12 21:52:04 +05:30
createComponent();
2019-07-31 22:56:46 +05:30
2022-11-25 23:54:43 +05:30
expect(findTitleField().props().value).toBe('title');
expect(findDescriptionField().props().value).toBe('description');
});
it('updates local storage drafts when title and description change', () => {
const updatedTitle = 'updated title';
const updatedDescription = 'updated description';
createComponent();
findTitleField().vm.$emit('input', updatedTitle);
findDescriptionField().vm.$emit('input', updatedDescription);
expect(updateDraft).toHaveBeenCalledWith(expect.any(Array), updatedTitle);
expect(updateDraft).toHaveBeenCalledWith(
expect.any(Array),
updatedDescription,
defaultProps.formState.lock_version,
);
2019-10-12 21:52:04 +05:30
});
it('calls reset on autosave when eventHub emits appropriate events', () => {
createComponent();
eventHub.$emit('close.form');
2019-07-31 22:56:46 +05:30
2022-11-25 23:54:43 +05:30
expect(clearDraft).toHaveBeenCalledTimes(2);
2019-07-31 22:56:46 +05:30
2019-10-12 21:52:04 +05:30
eventHub.$emit('delete.issuable');
2019-07-31 22:56:46 +05:30
2022-11-25 23:54:43 +05:30
expect(clearDraft).toHaveBeenCalledTimes(4);
2019-07-31 22:56:46 +05:30
2019-10-12 21:52:04 +05:30
eventHub.$emit('update.issuable');
2022-11-25 23:54:43 +05:30
expect(clearDraft).toHaveBeenCalledTimes(6);
2019-10-12 21:52:04 +05:30
});
2021-06-08 01:23:25 +05:30
describe('outdated description', () => {
2022-11-25 23:54:43 +05:30
const clientSideMockVersion = 'lock version from local storage';
const serverSideMockVersion = 'lock version from server';
const mockGetLockVersion = () => getLockVersion.mockResolvedValue(clientSideMockVersion);
2021-06-08 01:23:25 +05:30
it('does not show warning if lock version from server is the same as the local lock version', () => {
createComponent();
expect(findAlert().exists()).toBe(false);
});
it('shows warning if lock version from server differs than the local lock version', async () => {
2022-11-25 23:54:43 +05:30
mockGetLockVersion();
2021-06-08 01:23:25 +05:30
createComponent({
2022-11-25 23:54:43 +05:30
formState: { ...defaultProps.formState, lock_version: serverSideMockVersion },
2021-06-08 01:23:25 +05:30
});
2022-04-04 11:22:00 +05:30
await nextTick();
2021-06-08 01:23:25 +05:30
expect(findAlert().exists()).toBe(true);
});
2022-11-25 23:54:43 +05:30
describe('when saved draft is discarded', () => {
beforeEach(async () => {
mockGetLockVersion();
createComponent({
formState: { ...defaultProps.formState, lock_version: serverSideMockVersion },
});
await nextTick();
findAlert().vm.$emit('secondaryAction');
});
it('hides the warning alert', () => {
expect(findAlert().exists()).toBe(false);
});
it('clears the description draft', () => {
expect(clearDraft).toHaveBeenCalledWith(expect.any(Array));
});
});
2021-06-08 01:23:25 +05:30
});
2019-07-31 22:56:46 +05:30
});
2017-09-10 17:25:29 +05:30
});