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