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

278 lines
7.7 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import _ from 'underscore';
import Vue from 'vue';
import notesApp from '~/notes/components/notes_app.vue';
import service from '~/notes/services/notes_service';
2018-11-08 19:23:39 +05:30
import createStore from '~/notes/stores';
2018-05-09 12:01:36 +05:30
import '~/behaviors/markdown/render_gfm';
2018-11-08 19:23:39 +05:30
import { mountComponentWithStore } from 'spec/helpers';
2018-03-17 18:26:18 +05:30
import * as mockData from '../mock_data';
const vueMatchers = {
toIncludeElement() {
return {
compare(vm, selector) {
const result = {
pass: vm.$el.querySelector(selector) !== null,
};
return result;
},
};
},
};
describe('note_app', () => {
let mountComponent;
let vm;
2018-11-08 19:23:39 +05:30
let store;
2018-03-17 18:26:18 +05:30
beforeEach(() => {
jasmine.addMatchers(vueMatchers);
2018-03-27 19:54:05 +05:30
$('body').attr('data-page', 'projects:merge_requests:show');
2018-03-17 18:26:18 +05:30
const IssueNotesApp = Vue.extend(notesApp);
2018-11-08 19:23:39 +05:30
store = createStore();
mountComponent = data => {
2018-03-17 18:26:18 +05:30
const props = data || {
noteableData: mockData.noteableDataMock,
notesData: mockData.notesDataMock,
userData: mockData.userDataMock,
};
2018-11-08 19:23:39 +05:30
return mountComponentWithStore(IssueNotesApp, {
props,
store,
});
2018-03-17 18:26:18 +05:30
};
});
afterEach(() => {
vm.$destroy();
});
describe('set data', () => {
const responseInterceptor = (request, next) => {
2018-11-08 19:23:39 +05:30
next(
request.respondWith(JSON.stringify([]), {
status: 200,
}),
);
2018-03-17 18:26:18 +05:30
};
beforeEach(() => {
Vue.http.interceptors.push(responseInterceptor);
vm = mountComponent();
});
afterEach(() => {
Vue.http.interceptors = _.without(Vue.http.interceptors, responseInterceptor);
});
it('should set notes data', () => {
expect(vm.$store.state.notesData).toEqual(mockData.notesDataMock);
});
it('should set issue data', () => {
expect(vm.$store.state.noteableData).toEqual(mockData.noteableDataMock);
});
it('should set user data', () => {
expect(vm.$store.state.userData).toEqual(mockData.userDataMock);
});
2018-11-08 19:23:39 +05:30
it('should fetch discussions', () => {
expect(vm.$store.state.discussions).toEqual([]);
2018-03-17 18:26:18 +05:30
});
});
describe('render', () => {
beforeEach(() => {
Vue.http.interceptors.push(mockData.individualNoteInterceptor);
vm = mountComponent();
});
afterEach(() => {
Vue.http.interceptors = _.without(Vue.http.interceptors, mockData.individualNoteInterceptor);
});
2018-11-08 19:23:39 +05:30
it('should render list of notes', done => {
const note =
mockData.INDIVIDUAL_NOTE_RESPONSE_MAP.GET[
'/gitlab-org/gitlab-ce/issues/26/discussions.json'
][0].notes[0];
2018-03-17 18:26:18 +05:30
setTimeout(() => {
expect(
vm.$el.querySelector('.main-notes-list .note-header-author-name').textContent.trim(),
).toEqual(note.author.name);
2018-11-08 19:23:39 +05:30
expect(vm.$el.querySelector('.main-notes-list .note-text').innerHTML).toEqual(
note.note_html,
);
2018-03-17 18:26:18 +05:30
done();
}, 0);
});
it('should render form', () => {
expect(vm.$el.querySelector('.js-main-target-form').tagName).toEqual('FORM');
expect(
vm.$el.querySelector('.js-main-target-form textarea').getAttribute('placeholder'),
2018-11-08 19:23:39 +05:30
).toEqual('Write a comment or drag your files here…');
2018-03-17 18:26:18 +05:30
});
it('should render form comment button as disabled', () => {
2018-11-08 19:23:39 +05:30
expect(vm.$el.querySelector('.js-note-new-discussion').getAttribute('disabled')).toEqual(
'disabled',
);
2018-03-17 18:26:18 +05:30
});
});
describe('while fetching data', () => {
beforeEach(() => {
vm = mountComponent();
});
2018-03-27 19:54:05 +05:30
it('renders skeleton notes', () => {
expect(vm).toIncludeElement('.animation-container');
2018-03-17 18:26:18 +05:30
});
it('should render form', () => {
expect(vm.$el.querySelector('.js-main-target-form').tagName).toEqual('FORM');
expect(
vm.$el.querySelector('.js-main-target-form textarea').getAttribute('placeholder'),
2018-11-08 19:23:39 +05:30
).toEqual('Write a comment or drag your files here…');
2018-03-17 18:26:18 +05:30
});
});
describe('update note', () => {
describe('individual note', () => {
2018-11-08 19:23:39 +05:30
beforeEach(done => {
2018-03-17 18:26:18 +05:30
Vue.http.interceptors.push(mockData.individualNoteInterceptor);
spyOn(service, 'updateNote').and.callThrough();
vm = mountComponent();
setTimeout(() => {
vm.$el.querySelector('.js-note-edit').click();
Vue.nextTick(done);
}, 0);
});
afterEach(() => {
Vue.http.interceptors = _.without(
Vue.http.interceptors,
mockData.individualNoteInterceptor,
);
});
it('renders edit form', () => {
expect(vm).toIncludeElement('.js-vue-issue-note-form');
});
2018-11-08 19:23:39 +05:30
it('calls the service to update the note', done => {
2018-03-17 18:26:18 +05:30
vm.$el.querySelector('.js-vue-issue-note-form').value = 'this is a note';
vm.$el.querySelector('.js-vue-issue-save').click();
expect(service.updateNote).toHaveBeenCalled();
// Wait for the requests to finish before destroying
Vue.nextTick()
.then(done)
.catch(done.fail);
});
});
describe('discussion note', () => {
2018-11-08 19:23:39 +05:30
beforeEach(done => {
2018-03-17 18:26:18 +05:30
Vue.http.interceptors.push(mockData.discussionNoteInterceptor);
spyOn(service, 'updateNote').and.callThrough();
vm = mountComponent();
setTimeout(() => {
vm.$el.querySelector('.js-note-edit').click();
Vue.nextTick(done);
}, 0);
});
afterEach(() => {
Vue.http.interceptors = _.without(
Vue.http.interceptors,
mockData.discussionNoteInterceptor,
);
});
it('renders edit form', () => {
expect(vm).toIncludeElement('.js-vue-issue-note-form');
});
2018-11-08 19:23:39 +05:30
it('updates the note and resets the edit form', done => {
2018-03-17 18:26:18 +05:30
vm.$el.querySelector('.js-vue-issue-note-form').value = 'this is a note';
vm.$el.querySelector('.js-vue-issue-save').click();
expect(service.updateNote).toHaveBeenCalled();
// Wait for the requests to finish before destroying
Vue.nextTick()
.then(done)
.catch(done.fail);
});
});
});
describe('new note form', () => {
beforeEach(() => {
vm = mountComponent();
});
it('should render markdown docs url', () => {
const { markdownDocsPath } = mockData.notesDataMock;
2018-11-08 19:23:39 +05:30
expect(vm.$el.querySelector(`a[href="${markdownDocsPath}"]`).textContent.trim()).toEqual(
'Markdown',
);
2018-03-17 18:26:18 +05:30
});
it('should render quick action docs url', () => {
const { quickActionsDocsPath } = mockData.notesDataMock;
2018-11-08 19:23:39 +05:30
expect(vm.$el.querySelector(`a[href="${quickActionsDocsPath}"]`).textContent.trim()).toEqual(
'quick actions',
);
2018-03-17 18:26:18 +05:30
});
});
describe('edit form', () => {
beforeEach(() => {
Vue.http.interceptors.push(mockData.individualNoteInterceptor);
vm = mountComponent();
});
afterEach(() => {
Vue.http.interceptors = _.without(Vue.http.interceptors, mockData.individualNoteInterceptor);
});
2018-11-08 19:23:39 +05:30
it('should render markdown docs url', done => {
2018-03-17 18:26:18 +05:30
setTimeout(() => {
vm.$el.querySelector('.js-note-edit').click();
const { markdownDocsPath } = mockData.notesDataMock;
Vue.nextTick(() => {
expect(
vm.$el.querySelector(`.edit-note a[href="${markdownDocsPath}"]`).textContent.trim(),
).toEqual('Markdown is supported');
done();
});
}, 0);
});
2018-11-08 19:23:39 +05:30
it('should not render quick actions docs url', done => {
2018-03-17 18:26:18 +05:30
setTimeout(() => {
vm.$el.querySelector('.js-note-edit').click();
const { quickActionsDocsPath } = mockData.notesDataMock;
Vue.nextTick(() => {
2018-11-08 19:23:39 +05:30
expect(vm.$el.querySelector(`.edit-note a[href="${quickActionsDocsPath}"]`)).toEqual(
null,
);
2018-03-17 18:26:18 +05:30
done();
});
}, 0);
});
});
});