debian-mirror-gitlab/spec/javascripts/notes_spec.js

1034 lines
30 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
/* eslint-disable no-unused-expressions, no-var, object-shorthand */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import _ from 'underscore';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import 'autosize';
2017-08-17 22:00:37 +05:30
import '~/gl_form';
import '~/lib/utils/text_utility';
2018-05-09 12:01:36 +05:30
import '~/behaviors/markdown/render_gfm';
2018-03-17 18:26:18 +05:30
import Notes from '~/notes';
import timeoutPromise from './helpers/set_timeout_promise_helper';
2016-09-13 17:45:13 +05:30
2018-12-13 13:39:08 +05:30
window.gon || (window.gon = {});
window.gl = window.gl || {};
gl.utils = gl.utils || {};
const htmlEscape = comment => {
const escapedString = comment.replace(/["&'<>]/g, a => {
const escapedToken = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#x27;',
'`': '&#x60;',
}[a];
return escapedToken;
});
2016-09-29 09:46:39 +05:30
2018-12-13 13:39:08 +05:30
return escapedString;
};
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
describe('Notes', function() {
const FLASH_TYPE_ALERT = 'alert';
const NOTES_POST_PATH = /(.*)\/notes\?html=true$/;
var fixture = 'snippets/show.html.raw';
preloadFixtures(fixture);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
beforeEach(function() {
loadFixtures(fixture);
gl.utils.disableButtonIfEmptyField = _.noop;
window.project_uploads_path = 'http://test.host/uploads';
$('body').attr('data-page', 'projects:merge_requets:show');
});
2018-05-09 12:01:36 +05:30
2018-12-13 13:39:08 +05:30
afterEach(() => {
// Undo what we did to the shared <body>
$('body').removeAttr('data-page');
});
2018-05-09 12:01:36 +05:30
2018-12-13 13:39:08 +05:30
describe('addBinding', () => {
it('calls postComment when comment button is clicked', () => {
spyOn(Notes.prototype, 'postComment');
this.notes = new Notes('', []);
$('.js-comment-button').click();
expect(Notes.prototype.postComment).toHaveBeenCalled();
2018-05-09 12:01:36 +05:30
});
2018-12-13 13:39:08 +05:30
});
2018-05-09 12:01:36 +05:30
2018-12-13 13:39:08 +05:30
describe('task lists', function() {
let mock;
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
beforeEach(function() {
spyOn(axios, 'patch').and.callFake(() => new Promise(() => {}));
mock = new MockAdapter(axios);
mock.onAny().reply(200, {});
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
$('.js-comment-button').on('click', function(e) {
e.preventDefault();
2016-09-13 17:45:13 +05:30
});
2018-12-13 13:39:08 +05:30
this.notes = new Notes('', []);
});
2016-09-29 09:46:39 +05:30
2018-12-13 13:39:08 +05:30
afterEach(() => {
mock.restore();
});
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
it('modifies the Markdown field', function() {
const changeEvent = document.createEvent('HTMLEvents');
changeEvent.initEvent('change', true, true);
$('input[type=checkbox]')
.attr('checked', true)[0]
.dispatchEvent(changeEvent);
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
expect($('.js-task-list-field.original-task-list').val()).toBe('- [x] Task List Item');
});
2016-09-29 09:46:39 +05:30
2018-12-13 13:39:08 +05:30
it('submits an ajax request on tasklist:changed', function(done) {
$('.js-task-list-container').trigger('tasklist:changed');
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
setTimeout(() => {
expect(axios.patch).toHaveBeenCalled();
done();
2016-09-29 09:46:39 +05:30
});
});
2018-12-13 13:39:08 +05:30
});
2016-09-29 09:46:39 +05:30
2018-12-13 13:39:08 +05:30
describe('comments', function() {
var textarea = '.js-note-text';
2016-09-29 09:46:39 +05:30
2018-12-13 13:39:08 +05:30
beforeEach(function() {
this.notes = new Notes('', []);
2016-09-29 09:46:39 +05:30
2018-12-13 13:39:08 +05:30
this.autoSizeSpy = spyOnEvent($(textarea), 'autosize:update');
spyOn(this.notes, 'renderNote').and.stub();
2016-09-29 09:46:39 +05:30
2018-12-13 13:39:08 +05:30
$(textarea).data('autosave', {
reset: function() {},
});
2016-09-29 09:46:39 +05:30
2018-12-13 13:39:08 +05:30
$('.js-comment-button').on('click', e => {
const $form = $(this);
e.preventDefault();
this.notes.addNote($form);
this.notes.reenableTargetFormSubmitButton(e);
this.notes.resetMainTargetForm(e);
2016-09-13 17:45:13 +05:30
});
2018-12-13 13:39:08 +05:30
});
2016-09-29 09:46:39 +05:30
2018-12-13 13:39:08 +05:30
it('autosizes after comment submission', function() {
$(textarea).text('This is an example comment note');
2016-09-29 09:46:39 +05:30
2018-12-13 13:39:08 +05:30
expect(this.autoSizeSpy).not.toHaveBeenTriggered();
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
$('.js-comment-button').click();
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
expect(this.autoSizeSpy).toHaveBeenTriggered();
2016-09-13 17:45:13 +05:30
});
2018-12-13 13:39:08 +05:30
it('should not place escaped text in the comment box in case of error', function() {
const deferred = $.Deferred();
spyOn($, 'ajax').and.returnValue(deferred.promise());
$(textarea).text('A comment with `markup`.');
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
deferred.reject();
$('.js-comment-button').click();
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
expect($(textarea).val()).toEqual('A comment with `markup`.');
});
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('updateNote', () => {
let sampleComment;
let noteEntity;
let $form;
let $notesContainer;
let mock;
beforeEach(() => {
this.notes = new Notes('', []);
window.gon.current_username = 'root';
window.gon.current_user_fullname = 'Administrator';
sampleComment = 'foo';
noteEntity = {
id: 1234,
html: `<li class="note note-row-1234 timeline-entry" id="note_1234">
<div class="note-text">${sampleComment}</div>
</li>`,
note: sampleComment,
valid: true,
};
$form = $('form.js-main-target-form');
$notesContainer = $('ul.main-notes-list');
$form.find('textarea.js-note-text').val(sampleComment);
mock = new MockAdapter(axios);
mock.onPost(NOTES_POST_PATH).reply(200, noteEntity);
});
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
afterEach(() => {
mock.restore();
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('updates note and resets edit form', done => {
spyOn(this.notes, 'revertNoteEditForm');
spyOn(this.notes, 'setupNewNote');
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
$('.js-comment-button').click();
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
setTimeout(() => {
const $targetNote = $notesContainer.find(`#note_${noteEntity.id}`);
const updatedNote = Object.assign({}, noteEntity);
updatedNote.note = 'bar';
this.notes.updateNote(updatedNote, $targetNote);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(this.notes.revertNoteEditForm).toHaveBeenCalledWith($targetNote);
expect(this.notes.setupNewNote).toHaveBeenCalled();
done();
2017-09-10 17:25:29 +05:30
});
});
2018-12-13 13:39:08 +05:30
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
describe('updateNoteTargetSelector', () => {
const hash = 'note_foo';
let $note;
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
beforeEach(() => {
$note = $(`<div id="${hash}"></div>`);
spyOn($note, 'filter').and.callThrough();
spyOn($note, 'toggleClass').and.callThrough();
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
it('sets target when hash matches', () => {
spyOnDependency(Notes, 'getLocationHash').and.returnValue(hash);
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
Notes.updateNoteTargetSelector($note);
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect($note.filter).toHaveBeenCalledWith(`#${hash}`);
expect($note.toggleClass).toHaveBeenCalledWith('target', true);
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
it('unsets target when hash does not match', () => {
spyOnDependency(Notes, 'getLocationHash').and.returnValue('note_doesnotexist');
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
Notes.updateNoteTargetSelector($note);
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect($note.toggleClass).toHaveBeenCalledWith('target', false);
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
it('unsets target when there is not a hash fragment anymore', () => {
spyOnDependency(Notes, 'getLocationHash').and.returnValue(null);
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
Notes.updateNoteTargetSelector($note);
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect($note.toggleClass).toHaveBeenCalledWith('target', false);
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('renderNote', () => {
let notes;
let note;
let $notesList;
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
beforeEach(() => {
note = {
id: 1,
valid: true,
note: 'heya',
html: '<div>heya</div>',
};
$notesList = jasmine.createSpyObj('$notesList', ['find', 'append']);
notes = jasmine.createSpyObj('notes', [
'setupNewNote',
'refresh',
'collapseLongCommitList',
'updateNotesCount',
'putConflictEditWarningInPlace',
]);
notes.taskList = jasmine.createSpyObj('tasklist', ['init']);
notes.note_ids = [];
notes.updatedNotesTrackingMap = {};
spyOn(Notes, 'isNewNote').and.callThrough();
spyOn(Notes, 'isUpdatedNote').and.callThrough();
spyOn(Notes, 'animateAppendNote').and.callThrough();
spyOn(Notes, 'animateUpdateNote').and.callThrough();
});
describe('when adding note', () => {
it('should call .animateAppendNote', () => {
Notes.isNewNote.and.returnValue(true);
Notes.prototype.renderNote.call(notes, note, null, $notesList);
expect(Notes.animateAppendNote).toHaveBeenCalledWith(note.html, $notesList);
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('when note was edited', () => {
it('should call .animateUpdateNote', () => {
Notes.isNewNote.and.returnValue(false);
Notes.isUpdatedNote.and.returnValue(true);
const $note = $('<div>');
$notesList.find.and.returnValue($note);
const $newNote = $(note.html);
Notes.animateUpdateNote.and.returnValue($newNote);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
Notes.prototype.renderNote.call(notes, note, null, $notesList);
expect(Notes.animateUpdateNote).toHaveBeenCalledWith(note.html, $note);
expect(notes.setupNewNote).toHaveBeenCalledWith($newNote);
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
describe('while editing', () => {
it('should update textarea if nothing has been touched', () => {
2017-08-17 22:00:37 +05:30
Notes.isNewNote.and.returnValue(false);
Notes.isUpdatedNote.and.returnValue(true);
2018-12-13 13:39:08 +05:30
const $note = $(`<div class="is-editing">
<div class="original-note-content">initial</div>
<textarea class="js-note-text">initial</textarea>
</div>`);
2017-08-17 22:00:37 +05:30
$notesList.find.and.returnValue($note);
Notes.prototype.renderNote.call(notes, note, null, $notesList);
2018-12-13 13:39:08 +05:30
expect($note.find('.js-note-text').val()).toEqual(note.note);
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
it('should call .putConflictEditWarningInPlace', () => {
Notes.isNewNote.and.returnValue(false);
Notes.isUpdatedNote.and.returnValue(true);
const $note = $(`<div class="is-editing">
<div class="original-note-content">initial</div>
<textarea class="js-note-text">different</textarea>
</div>`);
$notesList.find.and.returnValue($note);
Notes.prototype.renderNote.call(notes, note, null, $notesList);
expect(notes.putConflictEditWarningInPlace).toHaveBeenCalledWith(note, $note);
2017-08-17 22:00:37 +05:30
});
});
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('isUpdatedNote', () => {
it('should consider same note text as the same', () => {
const result = Notes.isUpdatedNote(
{
note: 'initial',
},
$(`<div>
<div class="original-note-content">initial</div>
</div>`),
);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(result).toEqual(false);
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should consider same note with trailing newline as the same', () => {
const result = Notes.isUpdatedNote(
{
note: 'initial\n',
},
$(`<div>
<div class="original-note-content">initial\n</div>
</div>`),
);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(result).toEqual(false);
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should consider different notes as different', () => {
const result = Notes.isUpdatedNote(
{
note: 'foo',
},
$(`<div>
<div class="original-note-content">bar</div>
</div>`),
);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(result).toEqual(true);
});
});
describe('renderDiscussionNote', () => {
let discussionContainer;
let note;
let notes;
let $form;
let row;
beforeEach(() => {
note = {
html: '<li></li>',
discussion_html: '<div></div>',
discussion_id: 1,
discussion_resolvable: false,
diff_discussion_html: false,
};
$form = jasmine.createSpyObj('$form', ['closest', 'find']);
$form.length = 1;
row = jasmine.createSpyObj('row', ['prevAll', 'first', 'find']);
notes = jasmine.createSpyObj('notes', ['isParallelView', 'updateNotesCount']);
notes.note_ids = [];
spyOn(Notes, 'isNewNote');
spyOn(Notes, 'animateAppendNote');
Notes.isNewNote.and.returnValue(true);
notes.isParallelView.and.returnValue(false);
row.prevAll.and.returnValue(row);
row.first.and.returnValue(row);
row.find.and.returnValue(row);
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
describe('Discussion root note', () => {
let body;
2017-08-17 22:00:37 +05:30
beforeEach(() => {
2018-12-13 13:39:08 +05:30
body = jasmine.createSpyObj('body', ['attr']);
discussionContainer = { length: 0 };
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
$form.closest.and.returnValues(row, $form);
$form.find.and.returnValues(discussionContainer);
body.attr.and.returnValue('');
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
it('should call Notes.animateAppendNote', () => {
Notes.prototype.renderDiscussionNote.call(notes, note, $form);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(Notes.animateAppendNote).toHaveBeenCalledWith(
note.discussion_html,
$('.main-notes-list'),
);
});
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
it('should append to row selected with line_code', () => {
$form.length = 0;
note.discussion_line_code = 'line_code';
note.diff_discussion_html = '<tr></tr>';
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
const line = document.createElement('div');
line.id = note.discussion_line_code;
document.body.appendChild(line);
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
$form.closest.and.returnValues($form);
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
Notes.prototype.renderDiscussionNote.call(notes, note, $form);
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
expect(line.nextSibling.outerHTML).toEqual(note.diff_discussion_html);
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('Discussion sub note', () => {
beforeEach(() => {
discussionContainer = { length: 1 };
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
$form.closest.and.returnValues(row, $form);
$form.find.and.returnValues(discussionContainer);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
Notes.prototype.renderDiscussionNote.call(notes, note, $form);
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should call Notes.animateAppendNote', () => {
expect(Notes.animateAppendNote).toHaveBeenCalledWith(note.html, discussionContainer);
2017-08-17 22:00:37 +05:30
});
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('animateAppendNote', () => {
let noteHTML;
let $notesList;
let $resultantNote;
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
beforeEach(() => {
noteHTML = '<div></div>';
$notesList = jasmine.createSpyObj('$notesList', ['append']);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
$resultantNote = Notes.animateAppendNote(noteHTML, $notesList);
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should have `fade-in-full` class', () => {
expect($resultantNote.hasClass('fade-in-full')).toEqual(true);
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should append note to the notes list', () => {
expect($notesList.append).toHaveBeenCalledWith($resultantNote);
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('animateUpdateNote', () => {
let noteHTML;
let $note;
let $updatedNote;
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
beforeEach(() => {
noteHTML = '<div></div>';
$note = jasmine.createSpyObj('$note', ['replaceWith']);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
$updatedNote = Notes.animateUpdateNote(noteHTML, $note);
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should have `fade-in` class', () => {
expect($updatedNote.hasClass('fade-in')).toEqual(true);
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should call replaceWith on $note', () => {
expect($note.replaceWith).toHaveBeenCalledWith($updatedNote);
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('putEditFormInPlace', () => {
it('should call GLForm with GFM parameter passed through', () => {
const notes = new Notes('', []);
const $el = $(`
<div>
<form></form>
</div>
`);
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
notes.putEditFormInPlace($el);
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect(notes.glForm.enableGFM).toBeTruthy();
2017-09-10 17:25:29 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
describe('postComment & updateComment', () => {
const sampleComment = 'foo';
const updatedComment = 'bar';
const note = {
id: 1234,
html: `<li class="note note-row-1234 timeline-entry" id="note_1234">
<div class="note-text">${sampleComment}</div>
</li>`,
note: sampleComment,
valid: true,
};
let $form;
let $notesContainer;
let mock;
function mockNotesPost() {
mock.onPost(NOTES_POST_PATH).reply(200, note);
}
function mockNotesPostError() {
mock.onPost(NOTES_POST_PATH).networkError();
}
beforeEach(() => {
mock = new MockAdapter(axios);
this.notes = new Notes('', []);
window.gon.current_username = 'root';
window.gon.current_user_fullname = 'Administrator';
$form = $('form.js-main-target-form');
$notesContainer = $('ul.main-notes-list');
$form.find('textarea.js-note-text').val(sampleComment);
});
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
afterEach(() => {
mock.restore();
});
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
it('should show placeholder note while new comment is being posted', () => {
mockNotesPost();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
$('.js-comment-button').click();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect($notesContainer.find('.note.being-posted').length).toBeGreaterThan(0);
});
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
it('should remove placeholder note when new comment is done posting', done => {
mockNotesPost();
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
$('.js-comment-button').click();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
setTimeout(() => {
expect($notesContainer.find('.note.being-posted').length).toEqual(0);
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
done();
});
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('postComment', () => {
it('disables the submit button', done => {
const $submitButton = $form.find('.js-comment-submit-button');
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
expect($submitButton).not.toBeDisabled();
const dummyEvent = {
preventDefault() {},
target: $submitButton,
};
mock.onPost(NOTES_POST_PATH).replyOnce(() => {
expect($submitButton).toBeDisabled();
return [200, note];
2018-03-17 18:26:18 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
this.notes
.postComment(dummyEvent)
.then(() => {
expect($submitButton).not.toBeDisabled();
})
.then(done)
.catch(done.fail);
2018-05-09 12:01:36 +05:30
});
2018-12-13 13:39:08 +05:30
});
2018-05-09 12:01:36 +05:30
2018-12-13 13:39:08 +05:30
it('should show actual note element when new comment is done posting', done => {
mockNotesPost();
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
$('.js-comment-button').click();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
setTimeout(() => {
expect($notesContainer.find(`#note_${note.id}`).length).toBeGreaterThan(0);
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
done();
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should reset Form when new comment is done posting', done => {
mockNotesPost();
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
$('.js-comment-button').click();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
setTimeout(() => {
expect($form.find('textarea.js-note-text').val()).toEqual('');
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
done();
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should show flash error message when new comment failed to be posted', done => {
mockNotesPostError();
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
$('.js-comment-button').click();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
setTimeout(() => {
expect(
$notesContainer
.parent()
.find('.flash-container .flash-text')
.is(':visible'),
).toEqual(true);
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
done();
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should show flash error message when comment failed to be updated', done => {
mockNotesPost();
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
$('.js-comment-button').click();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
timeoutPromise()
.then(() => {
const $noteEl = $notesContainer.find(`#note_${note.id}`);
$noteEl.find('.js-note-edit').click();
$noteEl.find('textarea.js-note-text').val(updatedComment);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
mock.restore();
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
mockNotesPostError();
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
$noteEl.find('.js-comment-save-button').click();
})
.then(timeoutPromise)
.then(() => {
const $updatedNoteEl = $notesContainer.find(`#note_${note.id}`);
expect($updatedNoteEl.hasClass('.being-posted')).toEqual(false); // Remove being-posted visuals
expect(
$updatedNoteEl
.find('.note-text')
.text()
.trim(),
).toEqual(sampleComment); // See if comment reverted back to original
expect($('.flash-container').is(':visible')).toEqual(true); // Flash error message shown
done();
})
.catch(done.fail);
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('postComment with Slash commands', () => {
const sampleComment = '/assign @root\n/award :100:';
const note = {
commands_changes: {
assignee_id: 1,
emoji_award: '100',
},
errors: {
commands_only: ['Commands applied'],
},
valid: false,
};
let $form;
let $notesContainer;
let mock;
beforeEach(() => {
mock = new MockAdapter(axios);
mock.onPost(NOTES_POST_PATH).reply(200, note);
this.notes = new Notes('', []);
window.gon.current_username = 'root';
window.gon.current_user_fullname = 'Administrator';
gl.awardsHandler = {
addAwardToEmojiBar: () => {},
scrollToAwards: () => {},
};
gl.GfmAutoComplete = {
dataSources: {
commands: '/root/test-project/autocomplete_sources/commands',
2017-09-10 17:25:29 +05:30
},
};
2018-12-13 13:39:08 +05:30
$form = $('form.js-main-target-form');
$notesContainer = $('ul.main-notes-list');
$form.find('textarea.js-note-text').val(sampleComment);
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
afterEach(() => {
mock.restore();
});
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
it('should remove slash command placeholder when comment with slash commands is done posting', done => {
spyOn(gl.awardsHandler, 'addAwardToEmojiBar').and.callThrough();
$('.js-comment-button').click();
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect($notesContainer.find('.system-note.being-posted').length).toEqual(1); // Placeholder shown
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
setTimeout(() => {
expect($notesContainer.find('.system-note.being-posted').length).toEqual(0); // Placeholder removed
done();
2017-09-10 17:25:29 +05:30
});
});
2018-12-13 13:39:08 +05:30
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
describe('update comment with script tags', () => {
const sampleComment = '<script></script>';
const updatedComment = '<script></script>';
const note = {
id: 1234,
html: `<li class="note note-row-1234 timeline-entry" id="note_1234">
<div class="note-text">${sampleComment}</div>
</li>`,
note: sampleComment,
valid: true,
};
let $form;
let $notesContainer;
let mock;
beforeEach(() => {
mock = new MockAdapter(axios);
mock.onPost(NOTES_POST_PATH).reply(200, note);
this.notes = new Notes('', []);
window.gon.current_username = 'root';
window.gon.current_user_fullname = 'Administrator';
$form = $('form.js-main-target-form');
$notesContainer = $('ul.main-notes-list');
$form.find('textarea.js-note-text').html(sampleComment);
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
afterEach(() => {
mock.restore();
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should not render a script tag', done => {
$('.js-comment-button').click();
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
setTimeout(() => {
const $noteEl = $notesContainer.find(`#note_${note.id}`);
$noteEl.find('.js-note-edit').click();
$noteEl.find('textarea.js-note-text').html(updatedComment);
$noteEl.find('.js-comment-save-button').click();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
const $updatedNoteEl = $notesContainer
.find(`#note_${note.id}`)
.find('.js-task-list-container');
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(
$updatedNoteEl
.find('.note-text')
.text()
.trim(),
).toEqual('');
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
done();
2017-08-17 22:00:37 +05:30
});
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('getFormData', () => {
let $form;
let sampleComment;
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
beforeEach(() => {
this.notes = new Notes('', []);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
$form = $('form');
sampleComment = 'foobar';
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
it('should return form metadata object from form reference', () => {
$form.find('textarea.js-note-text').val(sampleComment);
const { formData, formContent, formAction } = this.notes.getFormData($form);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(formData.indexOf(sampleComment)).toBeGreaterThan(-1);
expect(formContent).toEqual(sampleComment);
expect(formAction).toEqual($form.attr('action'));
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
it('should return form metadata with sanitized formContent from form reference', () => {
spyOn(_, 'escape').and.callFake(htmlEscape);
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
sampleComment = '<script>alert("Boom!");</script>';
$form.find('textarea.js-note-text').val(sampleComment);
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
const { formContent } = this.notes.getFormData($form);
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect(_.escape).toHaveBeenCalledWith(sampleComment);
expect(formContent).toEqual('&lt;script&gt;alert(&quot;Boom!&quot;);&lt;/script&gt;');
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('hasQuickActions', () => {
beforeEach(() => {
this.notes = new Notes('', []);
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should return true when comment begins with a quick action', () => {
const sampleComment = '/wip\n/milestone %1.0\n/merge\n/unassign Merging this';
const hasQuickActions = this.notes.hasQuickActions(sampleComment);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(hasQuickActions).toBeTruthy();
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should return false when comment does NOT begin with a quick action', () => {
const sampleComment = 'Hey, /unassign Merging this';
const hasQuickActions = this.notes.hasQuickActions(sampleComment);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(hasQuickActions).toBeFalsy();
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should return false when comment does NOT have any quick actions', () => {
const sampleComment = 'Looking good, Awesome!';
const hasQuickActions = this.notes.hasQuickActions(sampleComment);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(hasQuickActions).toBeFalsy();
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('stripQuickActions', () => {
it('should strip quick actions from the comment which begins with a quick action', () => {
this.notes = new Notes();
const sampleComment = '/wip\n/milestone %1.0\n/merge\n/unassign Merging this';
const stripedComment = this.notes.stripQuickActions(sampleComment);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(stripedComment).toBe('');
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should strip quick actions from the comment but leaves plain comment if it is present', () => {
this.notes = new Notes();
const sampleComment = '/wip\n/milestone %1.0\n/merge\n/unassign\nMerging this';
const stripedComment = this.notes.stripQuickActions(sampleComment);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(stripedComment).toBe('Merging this');
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should NOT strip string that has slashes within', () => {
this.notes = new Notes();
const sampleComment = 'http://127.0.0.1:3000/root/gitlab-shell/issues/1';
const stripedComment = this.notes.stripQuickActions(sampleComment);
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(stripedComment).toBe(sampleComment);
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('getQuickActionDescription', () => {
const availableQuickActions = [
{ name: 'close', description: 'Close this issue', params: [] },
{ name: 'title', description: 'Change title', params: [{}] },
{ name: 'estimate', description: 'Set time estimate', params: [{}] },
];
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
beforeEach(() => {
this.notes = new Notes();
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
it('should return executing quick action description when note has single quick action', () => {
const sampleComment = '/close';
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect(this.notes.getQuickActionDescription(sampleComment, availableQuickActions)).toBe(
'Applying command to close this issue',
);
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
it('should return generic multiple quick action description when note has multiple quick actions', () => {
const sampleComment = '/close\n/title [Duplicate] Issue foobar';
expect(this.notes.getQuickActionDescription(sampleComment, availableQuickActions)).toBe(
'Applying multiple commands',
);
2017-09-10 17:25:29 +05:30
});
2018-12-13 13:39:08 +05:30
it('should return generic quick action description when available quick actions list is not populated', () => {
const sampleComment = '/close\n/title [Duplicate] Issue foobar';
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(this.notes.getQuickActionDescription(sampleComment)).toBe('Applying command');
});
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
describe('createPlaceholderNote', () => {
const sampleComment = 'foobar';
const uniqueId = 'b1234-a4567';
const currentUsername = 'root';
const currentUserFullname = 'Administrator';
const currentUserAvatar = 'avatar_url';
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
beforeEach(() => {
this.notes = new Notes('', []);
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should return constructed placeholder element for regular note based on form contents', () => {
const $tempNote = this.notes.createPlaceholderNote({
formContent: sampleComment,
uniqueId,
isDiscussionNote: false,
currentUsername,
currentUserFullname,
currentUserAvatar,
});
const $tempNoteHeader = $tempNote.find('.note-header');
expect($tempNote.prop('nodeName')).toEqual('LI');
expect($tempNote.attr('id')).toEqual(uniqueId);
expect($tempNote.hasClass('being-posted')).toBeTruthy();
expect($tempNote.hasClass('fade-in-half')).toBeTruthy();
$tempNote.find('.timeline-icon > a, .note-header-info > a').each(function() {
expect($(this).attr('href')).toEqual(`/${currentUsername}`);
});
expect($tempNote.find('.timeline-icon .avatar').attr('src')).toEqual(currentUserAvatar);
expect($tempNote.find('.timeline-content').hasClass('discussion')).toBeFalsy();
expect(
$tempNoteHeader
.find('.d-none.d-sm-inline-block')
.text()
.trim(),
).toEqual(currentUserFullname);
expect(
$tempNoteHeader
.find('.note-headline-light')
.text()
.trim(),
).toEqual(`@${currentUsername}`);
expect(
$tempNote
.find('.note-body .note-text p')
.text()
.trim(),
).toEqual(sampleComment);
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should return constructed placeholder element for discussion note based on form contents', () => {
const $tempNote = this.notes.createPlaceholderNote({
formContent: sampleComment,
uniqueId,
isDiscussionNote: true,
currentUsername,
currentUserFullname,
2017-09-10 17:25:29 +05:30
});
2018-12-13 13:39:08 +05:30
expect($tempNote.prop('nodeName')).toEqual('LI');
expect($tempNote.find('.timeline-content').hasClass('discussion')).toBeTruthy();
2017-09-10 17:25:29 +05:30
});
2018-12-13 13:39:08 +05:30
it('should return a escaped user name', () => {
const currentUserFullnameXSS = 'Foo <script>alert("XSS")</script>';
const $tempNote = this.notes.createPlaceholderNote({
formContent: sampleComment,
uniqueId,
isDiscussionNote: false,
currentUsername,
currentUserFullname: currentUserFullnameXSS,
currentUserAvatar,
});
const $tempNoteHeader = $tempNote.find('.note-header');
expect(
$tempNoteHeader
.find('.d-none.d-sm-inline-block')
.text()
.trim(),
).toEqual('Foo &lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt;');
});
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
describe('createPlaceholderSystemNote', () => {
const sampleCommandDescription = 'Applying command to close this issue';
const uniqueId = 'b1234-a4567';
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
beforeEach(() => {
this.notes = new Notes('', []);
spyOn(_, 'escape').and.callFake(htmlEscape);
});
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
it('should return constructed placeholder element for system note based on form contents', () => {
const $tempNote = this.notes.createPlaceholderSystemNote({
formContent: sampleCommandDescription,
uniqueId,
});
expect($tempNote.prop('nodeName')).toEqual('LI');
expect($tempNote.attr('id')).toEqual(uniqueId);
expect($tempNote.hasClass('being-posted')).toBeTruthy();
expect($tempNote.hasClass('fade-in-half')).toBeTruthy();
expect(
$tempNote
.find('.timeline-content i')
.text()
.trim(),
).toEqual(sampleCommandDescription);
2017-09-10 17:25:29 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
describe('appendFlash', () => {
beforeEach(() => {
this.notes = new Notes();
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
it('shows a flash message', () => {
this.notes.addFlash('Error message', FLASH_TYPE_ALERT, this.notes.parentTimeline.get(0));
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect($('.flash-alert').is(':visible')).toBeTruthy();
2017-09-10 17:25:29 +05:30
});
2018-12-13 13:39:08 +05:30
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
describe('clearFlash', () => {
beforeEach(() => {
$(document).off('ajax:success');
this.notes = new Notes();
});
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
it('hides visible flash message', () => {
this.notes.addFlash('Error message 1', FLASH_TYPE_ALERT, this.notes.parentTimeline.get(0));
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
this.notes.clearFlash();
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect($('.flash-alert').is(':visible')).toBeFalsy();
2017-08-17 22:00:37 +05:30
});
});
2018-12-13 13:39:08 +05:30
});