gitlab-org--gitlab-foss/spec/javascripts/notes/components/note_form_spec.js

185 lines
4.9 KiB
JavaScript
Raw Normal View History

import { shallowMount, createLocalVue } from '@vue/test-utils';
2018-06-21 08:22:40 -04:00
import createStore from '~/notes/stores';
import NoteForm from '~/notes/components/note_form.vue';
import MarkdownField from '~/vue_shared/components/markdown/field.vue';
2017-11-30 17:44:41 -05:00
import { noteableDataMock, notesDataMock } from '../mock_data';
2017-08-11 14:54:26 -04:00
describe('issue_note_form component', () => {
2018-06-21 08:22:40 -04:00
let store;
let wrapper;
let props;
beforeEach(() => {
2018-06-21 08:22:40 -04:00
store = createStore();
2017-11-30 17:44:41 -05:00
store.dispatch('setNoteableData', noteableDataMock);
store.dispatch('setNotesData', notesDataMock);
props = {
2017-08-11 14:54:26 -04:00
isEditing: false,
noteBody: 'Magni suscipit eius consectetur enim et ex et commodi.',
noteId: '545',
};
const localVue = createLocalVue();
wrapper = shallowMount(NoteForm, {
store,
propsData: props,
// see https://gitlab.com/gitlab-org/gitlab-ce/issues/56317 for the following
localVue,
sync: false,
});
});
afterEach(() => {
wrapper.destroy();
});
describe('noteHash', () => {
it('returns note hash string based on `noteId`', () => {
expect(wrapper.vm.noteHash).toBe(`#note_${props.noteId}`);
});
it('return note hash as `#` when `noteId` is empty', done => {
wrapper.setProps({
...props,
noteId: '',
});
wrapper.vm
.$nextTick()
.then(() => {
expect(wrapper.vm.noteHash).toBe('#');
})
.then(done)
.catch(done.fail);
});
});
2017-08-11 14:54:26 -04:00
describe('conflicts editing', () => {
2018-06-21 08:22:40 -04:00
it('should show conflict message if note changes outside the component', done => {
wrapper.setProps({
...props,
isEditing: true,
noteBody: 'Foo',
});
2018-06-21 08:22:40 -04:00
const message =
'This comment has changed since you started editing, please review the updated comment to ensure information is not lost.';
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);
2017-08-04 11:51:35 -04:00
});
});
describe('form', () => {
it('should render text area with placeholder', () => {
const textarea = wrapper.find('textarea');
expect(textarea.attributes('placeholder')).toEqual(
2018-06-21 08:22:40 -04:00
'Write a comment or drag your files here…',
);
2017-08-04 11:51:35 -04:00
});
it('should link to markdown docs', () => {
const { markdownDocsPath } = notesDataMock;
const markdownField = wrapper.find(MarkdownField);
const markdownFieldProps = markdownField.props();
2018-10-09 14:03:09 -04:00
expect(markdownFieldProps.markdownDocsPath).toBe(markdownDocsPath);
2017-08-04 11:51:35 -04:00
});
describe('keyboard events', () => {
let textarea;
beforeEach(() => {
textarea = wrapper.find('textarea');
textarea.setValue('Foo');
});
2017-08-04 11:51:35 -04:00
describe('up', () => {
it('should ender edit mode', () => {
// TODO: do not spy on vm
spyOn(wrapper.vm, 'editMyLastNote').and.callThrough();
textarea.trigger('keydown.up');
2017-08-04 11:51:35 -04:00
expect(wrapper.vm.editMyLastNote).toHaveBeenCalled();
2017-08-04 11:51:35 -04:00
});
});
describe('enter', () => {
it('should save note when cmd+enter is pressed', () => {
textarea.trigger('keydown.enter', { metaKey: true });
const { handleFormUpdate } = wrapper.emitted();
2017-08-04 11:51:35 -04:00
expect(handleFormUpdate.length).toBe(1);
});
it('should save note when ctrl+enter is pressed', () => {
textarea.trigger('keydown.enter', { ctrlKey: true });
const { handleFormUpdate } = wrapper.emitted();
expect(handleFormUpdate.length).toBe(1);
2017-08-04 11:51:35 -04:00
});
});
});
describe('actions', () => {
2018-06-21 08:22:40 -04:00
it('should be possible to cancel', done => {
// TODO: do not spy on vm
spyOn(wrapper.vm, 'cancelHandler').and.callThrough();
wrapper.setProps({
...props,
isEditing: true,
});
2017-08-04 11:51:35 -04:00
wrapper.vm
.$nextTick()
.then(() => {
const cancelButton = wrapper.find('.note-edit-cancel');
cancelButton.trigger('click');
2017-08-04 11:51:35 -04:00
expect(wrapper.vm.cancelHandler).toHaveBeenCalled();
})
.then(done)
.catch(done.fail);
2017-08-04 11:51:35 -04:00
});
2018-06-21 08:22:40 -04:00
it('should be possible to update the note', done => {
wrapper.setProps({
...props,
isEditing: true,
2017-08-11 14:54:26 -04:00
});
wrapper.vm
.$nextTick()
.then(() => {
const textarea = wrapper.find('textarea');
textarea.setValue('Foo');
const saveButton = wrapper.find('.js-vue-issue-save');
saveButton.trigger('click');
expect(wrapper.vm.isSubmitting).toEqual(true);
})
.then(done)
.catch(done.fail);
2017-08-04 11:51:35 -04:00
});
});
});
});