gitlab-org--gitlab-foss/spec/frontend/notes/components/note_app_spec.js

322 lines
9.2 KiB
JavaScript
Raw Normal View History

2019-06-03 10:36:34 -04:00
import $ from 'helpers/jquery';
2017-08-07 13:23:56 -04:00
import Vue from 'vue';
import { mount, createLocalVue } from '@vue/test-utils';
import NotesApp from '~/notes/components/notes_app.vue';
2017-11-30 17:44:41 -05:00
import service from '~/notes/services/notes_service';
2018-06-21 08:22:40 -04:00
import createStore from '~/notes/stores';
import '~/behaviors/markdown/render_gfm';
2019-06-03 10:36:34 -04:00
import { setTestTimeout } from 'helpers/timeout';
// TODO: use generated fixture (https://gitlab.com/gitlab-org/gitlab-ce/issues/62491)
import * as mockData from '../../../javascripts/notes/mock_data';
const originalInterceptors = [...Vue.http.interceptors];
const emptyResponseInterceptor = (request, next) => {
next(
request.respondWith(JSON.stringify([]), {
status: 200,
}),
);
};
setTestTimeout(1000);
2017-12-08 15:24:52 -05:00
describe('note_app', () => {
2017-08-07 13:23:56 -04:00
let mountComponent;
let wrapper;
2018-06-21 08:22:40 -04:00
let store;
2017-08-08 19:24:49 -04:00
2019-06-03 10:36:34 -04:00
/**
* waits for fetchNotes() to complete
*/
const waitForDiscussionsRequest = () =>
new Promise(resolve => {
const { vm } = wrapper.find(NotesApp);
const unwatch = vm.$watch('isFetching', isFetching => {
if (isFetching) {
return;
}
unwatch();
resolve();
});
});
2017-08-07 13:23:56 -04:00
beforeEach(() => {
$('body').attr('data-page', 'projects:merge_requests:show');
2018-06-21 08:22:40 -04:00
store = createStore();
mountComponent = data => {
const propsData = data || {
2017-11-30 17:44:41 -05:00
noteableData: mockData.noteableDataMock,
2017-08-08 19:24:49 -04:00
notesData: mockData.notesDataMock,
userData: mockData.userDataMock,
};
const localVue = createLocalVue();
return mount(
{
components: {
NotesApp,
},
template: '<div class="js-vue-notes-event"><notes-app v-bind="$attrs" /></div>',
},
{
2019-06-03 10:36:34 -04:00
attachToDocument: true,
propsData,
store,
localVue,
sync: false,
},
);
2017-08-08 19:24:49 -04:00
};
2017-08-04 11:51:35 -04:00
});
2017-08-08 19:24:49 -04:00
afterEach(() => {
wrapper.destroy();
2019-06-03 10:36:34 -04:00
Vue.http.interceptors = [...originalInterceptors];
2017-08-08 19:24:49 -04:00
});
2017-08-07 13:23:56 -04:00
2017-08-08 19:24:49 -04:00
describe('set data', () => {
2017-08-07 13:23:56 -04:00
beforeEach(() => {
2019-06-03 10:36:34 -04:00
Vue.http.interceptors.push(emptyResponseInterceptor);
wrapper = mountComponent();
2019-06-03 10:36:34 -04:00
return waitForDiscussionsRequest();
2017-08-07 13:23:56 -04:00
});
it('should set notes data', () => {
expect(store.state.notesData).toEqual(mockData.notesDataMock);
2017-08-07 13:23:56 -04:00
});
it('should set issue data', () => {
expect(store.state.noteableData).toEqual(mockData.noteableDataMock);
2017-08-07 13:23:56 -04:00
});
it('should set user data', () => {
expect(store.state.userData).toEqual(mockData.userDataMock);
2017-08-07 13:23:56 -04:00
});
2017-08-04 11:51:35 -04:00
2018-06-21 08:22:40 -04:00
it('should fetch discussions', () => {
expect(store.state.discussions).toEqual([]);
2017-08-07 13:23:56 -04:00
});
2017-08-04 11:51:35 -04:00
});
describe('render', () => {
2017-08-07 13:23:56 -04:00
beforeEach(() => {
setFixtures('<div class="js-discussions-count"></div>');
2017-12-11 16:39:30 -05:00
Vue.http.interceptors.push(mockData.individualNoteInterceptor);
wrapper = mountComponent();
2019-06-03 10:36:34 -04:00
return waitForDiscussionsRequest();
2017-08-07 13:23:56 -04:00
});
2019-06-03 10:36:34 -04:00
it('should render list of notes', () => {
const note =
mockData.INDIVIDUAL_NOTE_RESPONSE_MAP.GET[
2018-06-21 08:22:40 -04:00
'/gitlab-org/gitlab-ce/issues/26/discussions.json'
][0].notes[0];
2019-06-03 10:36:34 -04:00
expect(
wrapper
.find('.main-notes-list .note-header-author-name')
.text()
.trim(),
).toEqual(note.author.name);
2019-06-03 10:36:34 -04:00
expect(wrapper.find('.main-notes-list .note-text').html()).toContain(note.note_html);
2017-08-07 13:23:56 -04:00
});
2017-08-04 11:51:35 -04:00
2017-08-07 13:23:56 -04:00
it('should render form', () => {
expect(wrapper.find('.js-main-target-form').name()).toEqual('form');
expect(wrapper.find('.js-main-target-form textarea').attributes('placeholder')).toEqual(
'Write a comment or drag your files here…',
);
2017-08-07 13:23:56 -04:00
});
2017-08-08 19:24:49 -04:00
it('should render form comment button as disabled', () => {
expect(wrapper.find('.js-note-new-discussion').attributes('disabled')).toEqual('disabled');
});
2019-06-03 10:36:34 -04:00
it('updates discussions badge', () => {
expect(document.querySelector('.js-discussions-count').textContent).toEqual('2');
});
});
describe('render with comments disabled', () => {
beforeEach(() => {
setFixtures('<div class="js-discussions-count"></div>');
2019-06-03 10:36:34 -04:00
Vue.http.interceptors.push(mockData.individualNoteInterceptor);
store.state.commentsDisabled = true;
wrapper = mountComponent();
return waitForDiscussionsRequest();
});
it('should not render form when commenting is disabled', () => {
expect(wrapper.find('.js-main-target-form').exists()).toBe(false);
2017-08-08 19:24:49 -04:00
});
it('should render discussion filter note `commentsDisabled` is true', () => {
expect(wrapper.find('.js-discussion-filter-note').exists()).toBe(true);
});
2017-08-04 11:51:35 -04:00
});
2017-08-07 13:23:56 -04:00
describe('while fetching data', () => {
beforeEach(() => {
2019-06-03 10:36:34 -04:00
Vue.http.interceptors.push(emptyResponseInterceptor);
wrapper = mountComponent();
2017-08-07 13:23:56 -04:00
});
2019-06-03 10:36:34 -04:00
afterEach(() => waitForDiscussionsRequest());
it('renders skeleton notes', () => {
expect(wrapper.find('.animation-container').exists()).toBe(true);
2017-08-07 13:23:56 -04:00
});
2017-08-04 11:51:35 -04:00
2017-08-07 13:23:56 -04:00
it('should render form', () => {
expect(wrapper.find('.js-main-target-form').name()).toEqual('form');
expect(wrapper.find('.js-main-target-form textarea').attributes('placeholder')).toEqual(
'Write a comment or drag your files here…',
);
2017-08-07 13:23:56 -04:00
});
2017-08-04 11:51:35 -04:00
});
2017-08-07 13:23:56 -04:00
describe('update note', () => {
describe('individual note', () => {
2019-06-03 10:36:34 -04:00
beforeEach(() => {
2017-12-11 16:39:30 -05:00
Vue.http.interceptors.push(mockData.individualNoteInterceptor);
2019-06-03 10:36:34 -04:00
jest.spyOn(service, 'updateNote');
wrapper = mountComponent();
2019-06-03 10:36:34 -04:00
return waitForDiscussionsRequest().then(() => {
wrapper.find('.js-note-edit').trigger('click');
2019-06-03 10:36:34 -04:00
});
});
it('renders edit form', () => {
expect(wrapper.find('.js-vue-issue-note-form').exists()).toBe(true);
2017-08-07 13:23:56 -04:00
});
2019-06-03 10:36:34 -04:00
it('calls the service to update the note', () => {
wrapper.find('.js-vue-issue-note-form').value = 'this is a note';
wrapper.find('.js-vue-issue-save').trigger('click');
expect(service.updateNote).toHaveBeenCalled();
});
2017-08-07 13:23:56 -04:00
});
describe('discussion note', () => {
2019-06-03 10:36:34 -04:00
beforeEach(() => {
2017-12-11 16:39:30 -05:00
Vue.http.interceptors.push(mockData.discussionNoteInterceptor);
2019-06-03 10:36:34 -04:00
jest.spyOn(service, 'updateNote');
wrapper = mountComponent();
2019-06-03 10:36:34 -04:00
return waitForDiscussionsRequest().then(() => {
wrapper.find('.js-note-edit').trigger('click');
2019-06-03 10:36:34 -04:00
});
2017-08-08 19:24:49 -04:00
});
2017-08-07 13:23:56 -04:00
it('renders edit form', () => {
expect(wrapper.find('.js-vue-issue-note-form').exists()).toBe(true);
2017-08-08 19:24:49 -04:00
});
2017-08-07 13:23:56 -04:00
2019-06-03 10:36:34 -04:00
it('updates the note and resets the edit form', () => {
wrapper.find('.js-vue-issue-note-form').value = 'this is a note';
wrapper.find('.js-vue-issue-save').trigger('click');
expect(service.updateNote).toHaveBeenCalled();
});
2017-08-07 13:23:56 -04:00
});
2017-08-04 11:51:35 -04:00
});
2017-08-08 19:24:49 -04:00
describe('new note form', () => {
beforeEach(() => {
2019-06-03 10:36:34 -04:00
Vue.http.interceptors.push(mockData.individualNoteInterceptor);
wrapper = mountComponent();
2019-06-03 10:36:34 -04:00
return waitForDiscussionsRequest();
2017-08-04 11:51:35 -04:00
});
2017-08-08 19:24:49 -04:00
it('should render markdown docs url', () => {
const { markdownDocsPath } = mockData.notesDataMock;
2018-10-09 14:03:09 -04:00
expect(
wrapper
.find(`a[href="${markdownDocsPath}"]`)
.text()
.trim(),
).toEqual('Markdown');
2017-08-07 13:23:56 -04:00
});
2017-08-04 11:51:35 -04:00
2017-08-08 19:24:49 -04:00
it('should render quick action docs url', () => {
const { quickActionsDocsPath } = mockData.notesDataMock;
2018-10-09 14:03:09 -04:00
expect(
wrapper
.find(`a[href="${quickActionsDocsPath}"]`)
.text()
.trim(),
).toEqual('quick actions');
2017-08-04 11:51:35 -04:00
});
});
2017-08-07 13:23:56 -04:00
2017-08-08 19:24:49 -04:00
describe('edit form', () => {
beforeEach(() => {
2017-12-11 16:39:30 -05:00
Vue.http.interceptors.push(mockData.individualNoteInterceptor);
wrapper = mountComponent();
2019-06-03 10:36:34 -04:00
return waitForDiscussionsRequest();
2017-08-07 13:23:56 -04:00
});
2019-06-03 10:36:34 -04:00
it('should render markdown docs url', () => {
wrapper.find('.js-note-edit').trigger('click');
const { markdownDocsPath } = mockData.notesDataMock;
2017-08-07 13:23:56 -04:00
2019-06-03 10:36:34 -04:00
return Vue.nextTick().then(() => {
expect(
wrapper
.find(`.edit-note a[href="${markdownDocsPath}"]`)
.text()
.trim(),
).toEqual('Markdown is supported');
});
2017-08-07 13:23:56 -04:00
});
2019-06-03 10:36:34 -04:00
it('should not render quick actions docs url', () => {
wrapper.find('.js-note-edit').trigger('click');
const { quickActionsDocsPath } = mockData.notesDataMock;
expect(wrapper.find(`.edit-note a[href="${quickActionsDocsPath}"]`).exists()).toBe(false);
2017-08-07 13:23:56 -04:00
});
});
describe('emoji awards', () => {
2019-06-03 10:36:34 -04:00
beforeEach(() => {
Vue.http.interceptors.push(emptyResponseInterceptor);
wrapper = mountComponent();
return waitForDiscussionsRequest();
});
it('dispatches toggleAward after toggleAward event', () => {
const toggleAwardEvent = new CustomEvent('toggleAward', {
detail: {
awardName: 'test',
noteId: 1,
},
});
2019-06-03 10:36:34 -04:00
const toggleAwardAction = jest.fn().mockName('toggleAward');
wrapper.vm.$store.hotUpdate({
actions: {
toggleAward: toggleAwardAction,
2019-06-03 10:36:34 -04:00
stopPolling() {},
},
});
wrapper.vm.$parent.$el.dispatchEvent(toggleAwardEvent);
expect(toggleAwardAction).toHaveBeenCalledTimes(1);
2019-06-03 10:36:34 -04:00
const [, payload] = toggleAwardAction.mock.calls[0];
expect(payload).toEqual({
awardName: 'test',
noteId: 1,
});
});
});
2017-08-07 13:23:56 -04:00
});