debian-mirror-gitlab/spec/javascripts/notes/components/note_form_spec.js

269 lines
7 KiB
JavaScript
Raw Normal View History

2019-07-07 11:18:12 +05:30
import { shallowMount, createLocalVue } from '@vue/test-utils';
2018-11-08 19:23:39 +05:30
import createStore from '~/notes/stores';
2019-07-07 11:18:12 +05:30
import NoteForm from '~/notes/components/note_form.vue';
import MarkdownField from '~/vue_shared/components/markdown/field.vue';
2018-03-17 18:26:18 +05:30
import { noteableDataMock, notesDataMock } from '../mock_data';
describe('issue_note_form component', () => {
2019-07-07 11:18:12 +05:30
const dummyAutosaveKey = 'some-autosave-key';
const dummyDraft = 'dummy draft content';
2018-11-08 19:23:39 +05:30
let store;
2019-07-07 11:18:12 +05:30
let wrapper;
2018-03-17 18:26:18 +05:30
let props;
2019-07-07 11:18:12 +05:30
const createComponentWrapper = () => {
const localVue = createLocalVue();
2020-01-01 13:55:28 +05:30
return shallowMount(localVue.extend(NoteForm), {
2019-07-07 11:18:12 +05:30
store,
propsData: props,
2019-12-04 20:38:33 +05:30
// see https://gitlab.com/gitlab-org/gitlab-foss/issues/56317 for the following
2019-07-07 11:18:12 +05:30
localVue,
sync: false,
});
};
2018-03-17 18:26:18 +05:30
beforeEach(() => {
2019-07-07 11:18:12 +05:30
spyOnDependency(NoteForm, 'getDraft').and.callFake(key => {
if (key === dummyAutosaveKey) {
return dummyDraft;
}
return null;
});
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
store = createStore();
2018-03-17 18:26:18 +05:30
store.dispatch('setNoteableData', noteableDataMock);
store.dispatch('setNotesData', notesDataMock);
props = {
isEditing: false,
noteBody: 'Magni suscipit eius consectetur enim et ex et commodi.',
2018-11-20 20:47:30 +05:30
noteId: '545',
2018-03-17 18:26:18 +05:30
};
});
afterEach(() => {
2019-07-07 11:18:12 +05:30
wrapper.destroy();
2018-03-17 18:26:18 +05:30
});
2018-11-20 20:47:30 +05:30
describe('noteHash', () => {
2019-07-07 11:18:12 +05:30
beforeEach(() => {
wrapper = createComponentWrapper();
});
2018-11-20 20:47:30 +05:30
it('returns note hash string based on `noteId`', () => {
2019-07-07 11:18:12 +05:30
expect(wrapper.vm.noteHash).toBe(`#note_${props.noteId}`);
2018-11-20 20:47:30 +05:30
});
it('return note hash as `#` when `noteId` is empty', done => {
2019-07-07 11:18:12 +05:30
wrapper.setProps({
...props,
noteId: '',
});
wrapper.vm
.$nextTick()
2018-11-20 20:47:30 +05:30
.then(() => {
2019-07-07 11:18:12 +05:30
expect(wrapper.vm.noteHash).toBe('#');
2018-11-20 20:47:30 +05:30
})
.then(done)
.catch(done.fail);
});
});
2018-03-17 18:26:18 +05:30
describe('conflicts editing', () => {
2019-07-07 11:18:12 +05:30
beforeEach(() => {
wrapper = createComponentWrapper();
});
2018-11-08 19:23:39 +05:30
it('should show conflict message if note changes outside the component', done => {
2019-07-07 11:18:12 +05:30
wrapper.setProps({
...props,
isEditing: true,
noteBody: 'Foo',
});
2018-11-08 19:23:39 +05:30
const message =
'This comment has changed since you started editing, please review the updated comment to ensure information is not lost.';
2018-03-17 18:26:18 +05:30
2019-07-07 11:18:12 +05:30
wrapper.vm
.$nextTick()
.then(() => {
const conflictWarning = wrapper.find('.js-conflict-edit-warning');
expect(conflictWarning.exists()).toBe(true);
expect(
conflictWarning
.text()
.replace(/\s+/g, ' ')
.trim(),
).toBe(message);
})
.then(done)
.catch(done.fail);
2018-03-17 18:26:18 +05:30
});
});
describe('form', () => {
2019-07-07 11:18:12 +05:30
beforeEach(() => {
wrapper = createComponentWrapper();
});
2018-03-17 18:26:18 +05:30
it('should render text area with placeholder', () => {
2019-07-07 11:18:12 +05:30
const textarea = wrapper.find('textarea');
expect(textarea.attributes('placeholder')).toEqual(
2018-11-08 19:23:39 +05:30
'Write a comment or drag your files here…',
);
2018-03-17 18:26:18 +05:30
});
it('should link to markdown docs', () => {
const { markdownDocsPath } = notesDataMock;
2019-07-07 11:18:12 +05:30
const markdownField = wrapper.find(MarkdownField);
const markdownFieldProps = markdownField.props();
2018-12-13 13:39:08 +05:30
2019-07-07 11:18:12 +05:30
expect(markdownFieldProps.markdownDocsPath).toBe(markdownDocsPath);
2018-03-17 18:26:18 +05:30
});
describe('keyboard events', () => {
2019-07-07 11:18:12 +05:30
let textarea;
beforeEach(() => {
textarea = wrapper.find('textarea');
textarea.setValue('Foo');
});
2018-03-17 18:26:18 +05:30
describe('up', () => {
it('should ender edit mode', () => {
2019-07-07 11:18:12 +05:30
// TODO: do not spy on vm
spyOn(wrapper.vm, 'editMyLastNote').and.callThrough();
2018-03-17 18:26:18 +05:30
2019-07-07 11:18:12 +05:30
textarea.trigger('keydown.up');
expect(wrapper.vm.editMyLastNote).toHaveBeenCalled();
2018-03-17 18:26:18 +05:30
});
});
describe('enter', () => {
it('should save note when cmd+enter is pressed', () => {
2019-07-07 11:18:12 +05:30
textarea.trigger('keydown.enter', { metaKey: true });
const { handleFormUpdate } = wrapper.emitted();
2018-03-17 18:26:18 +05:30
2019-07-07 11:18:12 +05:30
expect(handleFormUpdate.length).toBe(1);
2018-03-17 18:26:18 +05:30
});
2018-12-13 13:39:08 +05:30
2018-03-17 18:26:18 +05:30
it('should save note when ctrl+enter is pressed', () => {
2019-07-07 11:18:12 +05:30
textarea.trigger('keydown.enter', { ctrlKey: true });
2018-03-17 18:26:18 +05:30
2019-07-07 11:18:12 +05:30
const { handleFormUpdate } = wrapper.emitted();
expect(handleFormUpdate.length).toBe(1);
2018-03-17 18:26:18 +05:30
});
});
});
describe('actions', () => {
2018-11-08 19:23:39 +05:30
it('should be possible to cancel', done => {
2019-07-07 11:18:12 +05:30
// TODO: do not spy on vm
spyOn(wrapper.vm, 'cancelHandler').and.callThrough();
wrapper.setProps({
...props,
isEditing: true,
});
2018-03-17 18:26:18 +05:30
2019-07-07 11:18:12 +05:30
wrapper.vm
.$nextTick()
.then(() => {
const cancelButton = wrapper.find('.note-edit-cancel');
cancelButton.trigger('click');
2018-03-17 18:26:18 +05:30
2019-07-07 11:18:12 +05:30
expect(wrapper.vm.cancelHandler).toHaveBeenCalled();
})
.then(done)
.catch(done.fail);
2019-05-18 00:54:41 +05:30
});
2019-05-30 16:15:17 +05:30
it('should be possible to update the note', done => {
2019-07-07 11:18:12 +05:30
wrapper.setProps({
...props,
isEditing: true,
});
2019-05-18 00:54:41 +05:30
2019-07-07 11:18:12 +05:30
wrapper.vm
.$nextTick()
.then(() => {
const textarea = wrapper.find('textarea');
textarea.setValue('Foo');
const saveButton = wrapper.find('.js-vue-issue-save');
saveButton.trigger('click');
2019-05-18 00:54:41 +05:30
2019-07-07 11:18:12 +05:30
expect(wrapper.vm.isSubmitting).toEqual(true);
})
.then(done)
.catch(done.fail);
});
});
});
describe('with autosaveKey', () => {
describe('with draft', () => {
beforeEach(done => {
Object.assign(props, {
noteBody: '',
autosaveKey: dummyAutosaveKey,
2019-05-18 00:54:41 +05:30
});
2019-07-07 11:18:12 +05:30
wrapper = createComponentWrapper();
wrapper.vm
.$nextTick()
.then(done)
.catch(done.fail);
2019-05-18 00:54:41 +05:30
});
2019-07-07 11:18:12 +05:30
it('displays the draft in textarea', () => {
const textarea = wrapper.find('textarea');
expect(textarea.element.value).toBe(dummyDraft);
});
});
describe('without draft', () => {
beforeEach(done => {
Object.assign(props, {
noteBody: '',
autosaveKey: 'some key without draft',
});
wrapper = createComponentWrapper();
wrapper.vm
.$nextTick()
.then(done)
.catch(done.fail);
});
it('leaves the textarea empty', () => {
const textarea = wrapper.find('textarea');
expect(textarea.element.value).toBe('');
});
});
it('updates the draft if textarea content changes', () => {
const updateDraftSpy = spyOnDependency(NoteForm, 'updateDraft').and.stub();
Object.assign(props, {
noteBody: '',
autosaveKey: dummyAutosaveKey,
});
wrapper = createComponentWrapper();
const textarea = wrapper.find('textarea');
const dummyContent = 'some new content';
textarea.setValue(dummyContent);
expect(updateDraftSpy).toHaveBeenCalledWith(dummyAutosaveKey, dummyContent);
2019-05-18 00:54:41 +05:30
});
2018-03-17 18:26:18 +05:30
});
});