debian-mirror-gitlab/spec/frontend/batch_comments/components/draft_note_spec.js

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

198 lines
5.8 KiB
JavaScript
Raw Normal View History

2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
import { GlButton, GlBadge } from '@gitlab/ui';
2020-10-24 23:57:45 +05:30
import { getByRole } from '@testing-library/dom';
2022-04-04 11:22:00 +05:30
import { shallowMount } from '@vue/test-utils';
2021-09-30 23:02:18 +05:30
import { stubComponent } from 'helpers/stub_component';
2020-06-23 00:09:42 +05:30
import DraftNote from '~/batch_comments/components/draft_note.vue';
2022-04-04 11:22:00 +05:30
import PublishButton from '~/batch_comments/components/publish_button.vue';
2020-06-23 00:09:42 +05:30
import { createStore } from '~/batch_comments/stores';
import NoteableNote from '~/notes/components/noteable_note.vue';
import '~/behaviors/markdown/render_gfm';
import { createDraft } from '../mock_data';
2021-09-30 23:02:18 +05:30
const NoteableNoteStub = stubComponent(NoteableNote, {
template: `
<div>
<slot name="note-header-info">Test</slot>
</div>
`,
});
2020-06-23 00:09:42 +05:30
describe('Batch comments draft note component', () => {
2020-10-24 23:57:45 +05:30
let store;
2020-06-23 00:09:42 +05:30
let wrapper;
let draft;
2020-10-24 23:57:45 +05:30
const LINE_RANGE = {};
const draftWithLineRange = {
position: {
line_range: LINE_RANGE,
},
};
2020-06-23 00:09:42 +05:30
2020-10-24 23:57:45 +05:30
const getList = () => getByRole(wrapper.element, 'list');
2022-04-04 11:22:00 +05:30
const findSubmitReviewButton = () => wrapper.findComponent(PublishButton);
const findAddCommentButton = () => wrapper.findComponent(GlButton);
2020-06-23 00:09:42 +05:30
2022-08-13 15:12:31 +05:30
const createComponent = (propsData = { draft }, glFeatures = {}) => {
2022-04-04 11:22:00 +05:30
wrapper = shallowMount(DraftNote, {
2020-06-23 00:09:42 +05:30
store,
2020-10-24 23:57:45 +05:30
propsData,
2021-09-30 23:02:18 +05:30
stubs: {
NoteableNote: NoteableNoteStub,
},
2022-08-13 15:12:31 +05:30
provide: {
glFeatures,
},
2020-06-23 00:09:42 +05:30
});
jest.spyOn(wrapper.vm.$store, 'dispatch').mockImplementation();
2020-10-24 23:57:45 +05:30
};
beforeEach(() => {
store = createStore();
draft = createDraft();
2020-06-23 00:09:42 +05:30
});
afterEach(() => {
wrapper.destroy();
});
it('renders template', () => {
2020-10-24 23:57:45 +05:30
createComponent();
2022-04-04 11:22:00 +05:30
expect(wrapper.findComponent(GlBadge).exists()).toBe(true);
2020-06-23 00:09:42 +05:30
2022-10-11 01:57:18 +05:30
const note = wrapper.findComponent(NoteableNote);
2020-06-23 00:09:42 +05:30
expect(note.exists()).toBe(true);
expect(note.props().note).toEqual(draft);
});
describe('add comment now', () => {
it('dispatches publishSingleDraft when clicking', () => {
2020-10-24 23:57:45 +05:30
createComponent();
2022-04-04 11:22:00 +05:30
const publishNowButton = findAddCommentButton();
2020-06-23 00:09:42 +05:30
publishNowButton.vm.$emit('click');
expect(wrapper.vm.$store.dispatch).toHaveBeenCalledWith(
'batchComments/publishSingleDraft',
1,
);
});
2022-04-04 11:22:00 +05:30
it('sets as loading when draft is publishing', async () => {
2020-10-24 23:57:45 +05:30
createComponent();
2020-06-23 00:09:42 +05:30
wrapper.vm.$store.state.batchComments.currentlyPublishingDrafts.push(1);
2022-04-04 11:22:00 +05:30
await nextTick();
const publishNowButton = findAddCommentButton();
2020-06-23 00:09:42 +05:30
2022-04-04 11:22:00 +05:30
expect(publishNowButton.props().loading).toBe(true);
});
2020-06-23 00:09:42 +05:30
2022-04-04 11:22:00 +05:30
it('sets as disabled when review is publishing', async () => {
createComponent();
wrapper.vm.$store.state.batchComments.isPublishing = true;
await nextTick();
const publishNowButton = findAddCommentButton();
expect(publishNowButton.props().disabled).toBe(true);
expect(publishNowButton.props().loading).toBe(false);
});
2022-08-13 15:12:31 +05:30
it('hides button when mr_review_submit_comment is enabled', () => {
createComponent({ draft }, { mrReviewSubmitComment: true });
expect(findAddCommentButton().exists()).toBe(false);
});
2022-04-04 11:22:00 +05:30
});
describe('submit review', () => {
it('sets as disabled when draft is publishing', async () => {
createComponent();
wrapper.vm.$store.state.batchComments.currentlyPublishingDrafts.push(1);
await nextTick();
const publishNowButton = findSubmitReviewButton();
2022-10-11 01:57:18 +05:30
expect(publishNowButton.attributes().disabled).toBe('true');
2020-06-23 00:09:42 +05:30
});
});
describe('update', () => {
2022-04-04 11:22:00 +05:30
it('dispatches updateDraft', async () => {
2020-10-24 23:57:45 +05:30
createComponent();
2022-10-11 01:57:18 +05:30
const note = wrapper.findComponent(NoteableNote);
2020-06-23 00:09:42 +05:30
note.vm.$emit('handleEdit');
2022-04-04 11:22:00 +05:30
await nextTick();
const formData = {
note: draft,
noteText: 'a',
resolveDiscussion: false,
};
note.vm.$emit('handleUpdateNote', formData);
expect(wrapper.vm.$store.dispatch).toHaveBeenCalledWith(
'batchComments/updateDraft',
formData,
);
2020-06-23 00:09:42 +05:30
});
});
describe('deleteDraft', () => {
it('dispatches deleteDraft', () => {
2020-10-24 23:57:45 +05:30
createComponent();
2020-06-23 00:09:42 +05:30
jest.spyOn(window, 'confirm').mockImplementation(() => true);
2022-10-11 01:57:18 +05:30
const note = wrapper.findComponent(NoteableNote);
2020-06-23 00:09:42 +05:30
note.vm.$emit('handleDeleteNote', draft);
expect(wrapper.vm.$store.dispatch).toHaveBeenCalledWith('batchComments/deleteDraft', draft);
});
});
describe('quick actions', () => {
2022-04-04 11:22:00 +05:30
it('renders referenced commands', async () => {
2020-10-24 23:57:45 +05:30
createComponent();
2020-06-23 00:09:42 +05:30
wrapper.setProps({
draft: {
...draft,
references: {
commands: 'test command',
},
},
});
2022-04-04 11:22:00 +05:30
await nextTick();
const referencedCommands = wrapper.find('.referenced-commands');
2020-06-23 00:09:42 +05:30
2022-04-04 11:22:00 +05:30
expect(referencedCommands.exists()).toBe(true);
expect(referencedCommands.text()).toContain('test command');
2020-06-23 00:09:42 +05:30
});
});
2020-10-24 23:57:45 +05:30
describe('multiline comments', () => {
describe.each`
2021-03-11 19:13:27 +05:30
desc | props | event | expectedCalls
${'with `draft.position`'} | ${draftWithLineRange} | ${'mouseenter'} | ${[['setSelectedCommentPositionHover', LINE_RANGE]]}
${'with `draft.position`'} | ${draftWithLineRange} | ${'mouseleave'} | ${[['setSelectedCommentPositionHover']]}
${'without `draft.position`'} | ${{}} | ${'mouseenter'} | ${[]}
${'without `draft.position`'} | ${{}} | ${'mouseleave'} | ${[]}
`('$desc', ({ props, event, expectedCalls }) => {
2020-10-24 23:57:45 +05:30
beforeEach(() => {
2021-03-11 19:13:27 +05:30
createComponent({ draft: { ...draft, ...props } });
2020-10-24 23:57:45 +05:30
jest.spyOn(store, 'dispatch');
});
it(`calls store ${expectedCalls.length} times on ${event}`, () => {
getList().dispatchEvent(new MouseEvent(event, { bubbles: true }));
expect(store.dispatch.mock.calls).toEqual(expectedCalls);
});
});
});
2020-06-23 00:09:42 +05:30
});