debian-mirror-gitlab/spec/frontend/diffs/components/diff_line_note_form_spec.js

161 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-04-22 19:07:51 +05:30
import { shallowMount } from '@vue/test-utils';
2018-11-08 19:23:39 +05:30
import DiffLineNoteForm from '~/diffs/components/diff_line_note_form.vue';
2019-10-12 21:52:04 +05:30
import { createStore } from '~/mr_notes/stores';
2021-03-11 19:13:27 +05:30
import NoteForm from '~/notes/components/note_form.vue';
2018-11-18 11:00:15 +05:30
import { noteableDataMock } from '../../notes/mock_data';
2021-03-11 19:13:27 +05:30
import diffFileMockData from '../mock_data/diff_file';
2018-11-08 19:23:39 +05:30
describe('DiffLineNoteForm', () => {
2020-04-22 19:07:51 +05:30
let wrapper;
2018-11-08 19:23:39 +05:30
let diffFile;
let diffLines;
2020-05-24 23:13:21 +05:30
const getDiffFileMock = () => ({ ...diffFileMockData });
2018-11-08 19:23:39 +05:30
2021-03-08 18:12:59 +05:30
const createComponent = (args = {}) => {
2018-11-08 19:23:39 +05:30
diffFile = getDiffFileMock();
2019-02-15 15:39:39 +05:30
diffLines = diffFile.highlighted_diff_lines;
2020-04-22 19:07:51 +05:30
const store = createStore();
store.state.notes.userData.id = 1;
store.state.notes.noteableData = noteableDataMock;
2021-03-11 19:13:27 +05:30
store.state.diffs.diffFiles = [diffFile];
2020-04-22 19:07:51 +05:30
2021-03-08 18:12:59 +05:30
store.replaceState({ ...store.state, ...args.state });
return shallowMount(DiffLineNoteForm, {
2020-04-22 19:07:51 +05:30
store,
propsData: {
diffFileHash: diffFile.file_hash,
diffLines,
line: diffLines[0],
noteTargetLine: diffLines[0],
},
2018-11-08 19:23:39 +05:30
});
2021-03-08 18:12:59 +05:30
};
2018-11-08 19:23:39 +05:30
describe('methods', () => {
2021-03-08 18:12:59 +05:30
beforeEach(() => {
wrapper = createComponent();
});
2018-11-08 19:23:39 +05:30
describe('handleCancelCommentForm', () => {
2018-11-18 11:00:15 +05:30
it('should ask for confirmation when shouldConfirm and isDirty passed as truthy', () => {
2020-04-22 19:07:51 +05:30
jest.spyOn(window, 'confirm').mockReturnValue(false);
2018-11-18 11:00:15 +05:30
2020-04-22 19:07:51 +05:30
wrapper.vm.handleCancelCommentForm(true, true);
2018-12-13 13:39:08 +05:30
2018-11-18 11:00:15 +05:30
expect(window.confirm).toHaveBeenCalled();
});
it('should ask for confirmation when one of the params false', () => {
2020-04-22 19:07:51 +05:30
jest.spyOn(window, 'confirm').mockReturnValue(false);
2018-11-18 11:00:15 +05:30
2020-04-22 19:07:51 +05:30
wrapper.vm.handleCancelCommentForm(true, false);
2018-12-13 13:39:08 +05:30
2018-11-18 11:00:15 +05:30
expect(window.confirm).not.toHaveBeenCalled();
2020-04-22 19:07:51 +05:30
wrapper.vm.handleCancelCommentForm(false, true);
2018-12-13 13:39:08 +05:30
2018-11-18 11:00:15 +05:30
expect(window.confirm).not.toHaveBeenCalled();
});
2021-03-08 18:12:59 +05:30
it('should call cancelCommentForm with lineCode', (done) => {
2020-04-22 19:07:51 +05:30
jest.spyOn(window, 'confirm').mockImplementation(() => {});
jest.spyOn(wrapper.vm, 'cancelCommentForm').mockImplementation(() => {});
jest.spyOn(wrapper.vm, 'resetAutoSave').mockImplementation(() => {});
wrapper.vm.handleCancelCommentForm();
2018-11-08 19:23:39 +05:30
2018-11-18 11:00:15 +05:30
expect(window.confirm).not.toHaveBeenCalled();
2020-04-22 19:07:51 +05:30
wrapper.vm.$nextTick(() => {
expect(wrapper.vm.cancelCommentForm).toHaveBeenCalledWith({
2019-02-15 15:39:39 +05:30
lineCode: diffLines[0].line_code,
2020-04-22 19:07:51 +05:30
fileHash: wrapper.vm.diffFileHash,
2018-11-18 11:00:15 +05:30
});
2018-12-13 13:39:08 +05:30
2020-04-22 19:07:51 +05:30
expect(wrapper.vm.resetAutoSave).toHaveBeenCalled();
2018-11-18 11:00:15 +05:30
done();
2018-11-08 19:23:39 +05:30
});
});
});
describe('saveNoteForm', () => {
2021-03-08 18:12:59 +05:30
it('should call saveNote action with proper params', (done) => {
2020-04-22 19:07:51 +05:30
const saveDiffDiscussionSpy = jest
.spyOn(wrapper.vm, 'saveDiffDiscussion')
.mockReturnValue(Promise.resolve());
2018-12-05 23:21:45 +05:30
2020-06-23 00:09:42 +05:30
const lineRange = {
2020-07-28 23:09:34 +05:30
start: {
line_code: wrapper.vm.commentLineStart.line_code,
type: wrapper.vm.commentLineStart.type,
new_line: 1,
old_line: null,
},
end: {
line_code: wrapper.vm.line.line_code,
type: wrapper.vm.line.type,
new_line: 1,
old_line: null,
},
2020-06-23 00:09:42 +05:30
};
const formData = {
...wrapper.vm.formData,
lineRange,
};
2020-04-22 19:07:51 +05:30
wrapper.vm
2018-12-05 23:21:45 +05:30
.handleSaveNote('note body')
.then(() => {
expect(saveDiffDiscussionSpy).toHaveBeenCalledWith({
note: 'note body',
2020-06-23 00:09:42 +05:30
formData,
2018-12-05 23:21:45 +05:30
});
})
.then(done)
.catch(done.fail);
2018-11-08 19:23:39 +05:30
});
});
});
describe('mounted', () => {
it('should init autosave', () => {
2018-11-18 11:00:15 +05:30
const key = 'autosave/Note/Issue/98//DiffNote//1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_1';
2021-03-08 18:12:59 +05:30
wrapper = createComponent();
2018-11-08 19:23:39 +05:30
2020-04-22 19:07:51 +05:30
expect(wrapper.vm.autosave).toBeDefined();
expect(wrapper.vm.autosave.key).toEqual(key);
2018-11-08 19:23:39 +05:30
});
2021-03-08 18:12:59 +05:30
it('should set selectedCommentPosition', () => {
wrapper = createComponent();
let startLineCode = wrapper.vm.commentLineStart.line_code;
let lineCode = wrapper.vm.line.line_code;
expect(startLineCode).toEqual(lineCode);
wrapper.destroy();
const state = {
notes: {
selectedCommentPosition: {
start: {
line_code: 'test',
},
},
},
};
wrapper = createComponent({ state });
startLineCode = wrapper.vm.commentLineStart.line_code;
lineCode = state.notes.selectedCommentPosition.start.line_code;
expect(startLineCode).toEqual(lineCode);
});
2018-11-08 19:23:39 +05:30
});
describe('template', () => {
it('should have note form', () => {
2021-03-08 18:12:59 +05:30
wrapper = createComponent();
2020-04-22 19:07:51 +05:30
expect(wrapper.find(NoteForm).exists()).toBe(true);
2018-11-08 19:23:39 +05:30
});
});
});