2020-04-22 19:07:51 +05:30
|
|
|
import { shallowMount } from '@vue/test-utils';
|
2021-12-11 22:18:48 +05:30
|
|
|
import { nextTick } from 'vue';
|
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';
|
2021-12-11 22:18:48 +05:30
|
|
|
import { confirmAction } from '~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal';
|
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
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
jest.mock('~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal', () => {
|
|
|
|
return {
|
|
|
|
confirmAction: jest.fn(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
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: {
|
2021-12-11 22:18:48 +05:30
|
|
|
...{
|
|
|
|
diffFileHash: diffFile.file_hash,
|
|
|
|
diffLines,
|
|
|
|
line: diffLines[1],
|
|
|
|
range: { start: diffLines[0], end: diffLines[1] },
|
|
|
|
noteTargetLine: diffLines[1],
|
|
|
|
},
|
|
|
|
...(args.props || {}),
|
2020-04-22 19:07:51 +05:30
|
|
|
},
|
2018-11-08 19:23:39 +05:30
|
|
|
});
|
2021-03-08 18:12:59 +05:30
|
|
|
};
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
const findNoteForm = () => wrapper.findComponent(NoteForm);
|
|
|
|
|
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', () => {
|
2021-12-11 22:18:48 +05:30
|
|
|
afterEach(() => {
|
|
|
|
confirmAction.mockReset();
|
|
|
|
});
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it('should ask for confirmation when shouldConfirm and isDirty passed as truthy', () => {
|
2021-12-11 22:18:48 +05:30
|
|
|
confirmAction.mockResolvedValueOnce(false);
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
findNoteForm().vm.$emit('cancelForm', true, true);
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(confirmAction).toHaveBeenCalled();
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
it('should not ask for confirmation when one of the params false', () => {
|
|
|
|
confirmAction.mockResolvedValueOnce(false);
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
findNoteForm().vm.$emit('cancelForm', true, false);
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(confirmAction).not.toHaveBeenCalled();
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
findNoteForm().vm.$emit('cancelForm', false, true);
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(confirmAction).not.toHaveBeenCalled();
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
it('should call cancelCommentForm with lineCode', async () => {
|
|
|
|
confirmAction.mockResolvedValueOnce(true);
|
2020-04-22 19:07:51 +05:30
|
|
|
jest.spyOn(wrapper.vm, 'cancelCommentForm').mockImplementation(() => {});
|
|
|
|
jest.spyOn(wrapper.vm, 'resetAutoSave').mockImplementation(() => {});
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
findNoteForm().vm.$emit('cancelForm', true, true);
|
|
|
|
|
|
|
|
await nextTick();
|
|
|
|
|
|
|
|
expect(confirmAction).toHaveBeenCalled();
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
await nextTick();
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(wrapper.vm.cancelCommentForm).toHaveBeenCalledWith({
|
|
|
|
lineCode: diffLines[1].line_code,
|
|
|
|
fileHash: wrapper.vm.diffFileHash,
|
2018-11-08 19:23:39 +05:30
|
|
|
});
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(wrapper.vm.resetAutoSave).toHaveBeenCalled();
|
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,
|
2021-12-11 22:18:48 +05:30
|
|
|
new_line: 2,
|
2020-07-28 23:09:34 +05:30
|
|
|
old_line: null,
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
line_code: wrapper.vm.line.line_code,
|
|
|
|
type: wrapper.vm.line.type,
|
2021-12-11 22:18:48 +05:30
|
|
|
new_line: 2,
|
2020-07-28 23:09:34 +05:30
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
describe('created', () => {
|
|
|
|
it('should use the provided `range` of lines', () => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
|
|
|
|
expect(wrapper.vm.lines.start).toBe(diffLines[0]);
|
|
|
|
expect(wrapper.vm.lines.end).toBe(diffLines[1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fill the internal `lines` data with the provided `line` if there's no provided `range", () => {
|
|
|
|
wrapper = createComponent({ props: { range: null } });
|
|
|
|
|
|
|
|
expect(wrapper.vm.lines.start).toBe(diffLines[1]);
|
|
|
|
expect(wrapper.vm.lines.end).toBe(diffLines[1]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
describe('mounted', () => {
|
|
|
|
it('should init autosave', () => {
|
2021-12-11 22:18:48 +05:30
|
|
|
const key = 'autosave/Note/Issue/98//DiffNote//1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_2';
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|