Commit 988747df authored by Phil Hughes's avatar Phil Hughes

fixed notes_spec.js

parent 0f895147
/* eslint-disable space-before-function-paren, no-unused-expressions, no-var, object-shorthand, comma-dangle, max-len */ /* eslint-disable space-before-function-paren, no-unused-expressions, no-var, object-shorthand, comma-dangle, max-len */
import _ from 'underscore'; import _ from 'underscore';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import * as urlUtils from '~/lib/utils/url_utility'; import * as urlUtils from '~/lib/utils/url_utility';
import 'autosize'; import 'autosize';
import '~/gl_form'; import '~/gl_form';
import '~/lib/utils/text_utility'; import '~/lib/utils/text_utility';
import '~/render_gfm'; import '~/render_gfm';
import Notes from '~/notes'; import Notes from '~/notes';
import timeoutPromise from './helpers/set_timeout_promise_helper';
(function() { (function() {
window.gon || (window.gon = {}); window.gon || (window.gon = {});
...@@ -119,6 +122,7 @@ import Notes from '~/notes'; ...@@ -119,6 +122,7 @@ import Notes from '~/notes';
let noteEntity; let noteEntity;
let $form; let $form;
let $notesContainer; let $notesContainer;
let mock;
beforeEach(() => { beforeEach(() => {
this.notes = new Notes('', []); this.notes = new Notes('', []);
...@@ -136,17 +140,18 @@ import Notes from '~/notes'; ...@@ -136,17 +140,18 @@ import Notes from '~/notes';
$form = $('form.js-main-target-form'); $form = $('form.js-main-target-form');
$notesContainer = $('ul.main-notes-list'); $notesContainer = $('ul.main-notes-list');
$form.find('textarea.js-note-text').val(sampleComment); $form.find('textarea.js-note-text').val(sampleComment);
mock = new MockAdapter(axios);
mock.onPost(/(.*)\/notes$/).reply(200, noteEntity);
}); });
it('updates note and resets edit form', () => { it('updates note and resets edit form', (done) => {
const deferred = $.Deferred();
spyOn($, 'ajax').and.returnValue(deferred.promise());
spyOn(this.notes, 'revertNoteEditForm'); spyOn(this.notes, 'revertNoteEditForm');
spyOn(this.notes, 'setupNewNote'); spyOn(this.notes, 'setupNewNote');
$('.js-comment-button').click(); $('.js-comment-button').click();
deferred.resolve(noteEntity);
setTimeout(() => {
const $targetNote = $notesContainer.find(`#note_${noteEntity.id}`); const $targetNote = $notesContainer.find(`#note_${noteEntity.id}`);
const updatedNote = Object.assign({}, noteEntity); const updatedNote = Object.assign({}, noteEntity);
updatedNote.note = 'bar'; updatedNote.note = 'bar';
...@@ -154,6 +159,9 @@ import Notes from '~/notes'; ...@@ -154,6 +159,9 @@ import Notes from '~/notes';
expect(this.notes.revertNoteEditForm).toHaveBeenCalledWith($targetNote); expect(this.notes.revertNoteEditForm).toHaveBeenCalledWith($targetNote);
expect(this.notes.setupNewNote).toHaveBeenCalled(); expect(this.notes.setupNewNote).toHaveBeenCalled();
done();
});
}); });
}); });
...@@ -479,8 +487,19 @@ import Notes from '~/notes'; ...@@ -479,8 +487,19 @@ import Notes from '~/notes';
}; };
let $form; let $form;
let $notesContainer; let $notesContainer;
let mock;
function mockNotesPost() {
mock.onPost(/(.*)\/notes$/).reply(200, note);
}
function mockNotesPostError() {
mock.onPost(/(.*)\/notes$/).networkError();
}
beforeEach(() => { beforeEach(() => {
mock = new MockAdapter(axios);
this.notes = new Notes('', []); this.notes = new Notes('', []);
window.gon.current_username = 'root'; window.gon.current_username = 'root';
window.gon.current_user_fullname = 'Administrator'; window.gon.current_user_fullname = 'Administrator';
...@@ -489,63 +508,92 @@ import Notes from '~/notes'; ...@@ -489,63 +508,92 @@ import Notes from '~/notes';
$form.find('textarea.js-note-text').val(sampleComment); $form.find('textarea.js-note-text').val(sampleComment);
}); });
afterEach(() => {
mock.restore();
});
it('should show placeholder note while new comment is being posted', () => { it('should show placeholder note while new comment is being posted', () => {
mockNotesPost();
$('.js-comment-button').click(); $('.js-comment-button').click();
expect($notesContainer.find('.note.being-posted').length > 0).toEqual(true); expect($notesContainer.find('.note.being-posted').length > 0).toEqual(true);
}); });
it('should remove placeholder note when new comment is done posting', () => { it('should remove placeholder note when new comment is done posting', (done) => {
const deferred = $.Deferred(); mockNotesPost();
spyOn($, 'ajax').and.returnValue(deferred.promise());
$('.js-comment-button').click(); $('.js-comment-button').click();
deferred.resolve(note); setTimeout(() => {
expect($notesContainer.find('.note.being-posted').length).toEqual(0); expect($notesContainer.find('.note.being-posted').length).toEqual(0);
done();
});
}); });
it('should show actual note element when new comment is done posting', () => { it('should show actual note element when new comment is done posting', (done) => {
const deferred = $.Deferred(); mockNotesPost();
spyOn($, 'ajax').and.returnValue(deferred.promise());
$('.js-comment-button').click(); $('.js-comment-button').click();
deferred.resolve(note); setTimeout(() => {
expect($notesContainer.find(`#note_${note.id}`).length > 0).toEqual(true); expect($notesContainer.find(`#note_${note.id}`).length > 0).toEqual(true);
done();
});
}); });
it('should reset Form when new comment is done posting', () => { it('should reset Form when new comment is done posting', (done) => {
const deferred = $.Deferred(); mockNotesPost();
spyOn($, 'ajax').and.returnValue(deferred.promise());
$('.js-comment-button').click(); $('.js-comment-button').click();
deferred.resolve(note); setTimeout(() => {
expect($form.find('textarea.js-note-text').val()).toEqual(''); expect($form.find('textarea.js-note-text').val()).toEqual('');
done();
});
}); });
it('should show flash error message when new comment failed to be posted', () => { it('should show flash error message when new comment failed to be posted', (done) => {
const deferred = $.Deferred(); mockNotesPostError();
spyOn($, 'ajax').and.returnValue(deferred.promise());
$('.js-comment-button').click(); $('.js-comment-button').click();
deferred.reject(); setTimeout(() => {
expect($notesContainer.parent().find('.flash-container .flash-text').is(':visible')).toEqual(true); expect($notesContainer.parent().find('.flash-container .flash-text').is(':visible')).toEqual(true);
done();
});
}); });
it('should show flash error message when comment failed to be updated', () => { it('should show flash error message when comment failed to be updated', (done) => {
const deferred = $.Deferred(); mockNotesPost();
spyOn($, 'ajax').and.returnValue(deferred.promise());
$('.js-comment-button').click(); $('.js-comment-button').click();
deferred.resolve(note); timeoutPromise()
.then(() => {
const $noteEl = $notesContainer.find(`#note_${note.id}`); const $noteEl = $notesContainer.find(`#note_${note.id}`);
$noteEl.find('.js-note-edit').click(); $noteEl.find('.js-note-edit').click();
$noteEl.find('textarea.js-note-text').val(updatedComment); $noteEl.find('textarea.js-note-text').val(updatedComment);
$noteEl.find('.js-comment-save-button').click();
deferred.reject(); mock.restore();
mockNotesPostError();
$noteEl.find('.js-comment-save-button').click();
})
.then(timeoutPromise)
.then(() => {
const $updatedNoteEl = $notesContainer.find(`#note_${note.id}`); const $updatedNoteEl = $notesContainer.find(`#note_${note.id}`);
expect($updatedNoteEl.hasClass('.being-posted')).toEqual(false); // Remove being-posted visuals 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($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 expect($('.flash-container').is(':visible')).toEqual(true); // Flash error message shown
done();
})
.catch(done.fail);
}); });
}); });
...@@ -563,8 +611,12 @@ import Notes from '~/notes'; ...@@ -563,8 +611,12 @@ import Notes from '~/notes';
}; };
let $form; let $form;
let $notesContainer; let $notesContainer;
let mock;
beforeEach(() => { beforeEach(() => {
mock = new MockAdapter(axios);
mock.onPost(/(.*)\/notes$/).reply(200, note);
this.notes = new Notes('', []); this.notes = new Notes('', []);
window.gon.current_username = 'root'; window.gon.current_username = 'root';
window.gon.current_user_fullname = 'Administrator'; window.gon.current_user_fullname = 'Administrator';
...@@ -582,15 +634,20 @@ import Notes from '~/notes'; ...@@ -582,15 +634,20 @@ import Notes from '~/notes';
$form.find('textarea.js-note-text').val(sampleComment); $form.find('textarea.js-note-text').val(sampleComment);
}); });
it('should remove slash command placeholder when comment with slash commands is done posting', () => { afterEach(() => {
const deferred = $.Deferred(); mock.restore();
spyOn($, 'ajax').and.returnValue(deferred.promise()); });
it('should remove slash command placeholder when comment with slash commands is done posting', (done) => {
spyOn(gl.awardsHandler, 'addAwardToEmojiBar').and.callThrough(); spyOn(gl.awardsHandler, 'addAwardToEmojiBar').and.callThrough();
$('.js-comment-button').click(); $('.js-comment-button').click();
expect($notesContainer.find('.system-note.being-posted').length).toEqual(1); // Placeholder shown expect($notesContainer.find('.system-note.being-posted').length).toEqual(1); // Placeholder shown
deferred.resolve(note);
setTimeout(() => {
expect($notesContainer.find('.system-note.being-posted').length).toEqual(0); // Placeholder removed expect($notesContainer.find('.system-note.being-posted').length).toEqual(0); // Placeholder removed
done();
});
}); });
}); });
...@@ -607,8 +664,12 @@ import Notes from '~/notes'; ...@@ -607,8 +664,12 @@ import Notes from '~/notes';
}; };
let $form; let $form;
let $notesContainer; let $notesContainer;
let mock;
beforeEach(() => { beforeEach(() => {
mock = new MockAdapter(axios);
mock.onPost(/(.*)\/notes$/).reply(200, note);
this.notes = new Notes('', []); this.notes = new Notes('', []);
window.gon.current_username = 'root'; window.gon.current_username = 'root';
window.gon.current_user_fullname = 'Administrator'; window.gon.current_user_fullname = 'Administrator';
...@@ -617,12 +678,14 @@ import Notes from '~/notes'; ...@@ -617,12 +678,14 @@ import Notes from '~/notes';
$form.find('textarea.js-note-text').html(sampleComment); $form.find('textarea.js-note-text').html(sampleComment);
}); });
it('should not render a script tag', () => { afterEach(() => {
const deferred = $.Deferred(); mock.restore();
spyOn($, 'ajax').and.returnValue(deferred.promise()); });
it('should not render a script tag', (done) => {
$('.js-comment-button').click(); $('.js-comment-button').click();
deferred.resolve(note); setTimeout(() => {
const $noteEl = $notesContainer.find(`#note_${note.id}`); const $noteEl = $notesContainer.find(`#note_${note.id}`);
$noteEl.find('.js-note-edit').click(); $noteEl.find('.js-note-edit').click();
$noteEl.find('textarea.js-note-text').html(updatedComment); $noteEl.find('textarea.js-note-text').html(updatedComment);
...@@ -630,6 +693,9 @@ import Notes from '~/notes'; ...@@ -630,6 +693,9 @@ import Notes from '~/notes';
const $updatedNoteEl = $notesContainer.find(`#note_${note.id}`).find('.js-task-list-container'); const $updatedNoteEl = $notesContainer.find(`#note_${note.id}`).find('.js-task-list-container');
expect($updatedNoteEl.find('.note-text').text().trim()).toEqual(''); expect($updatedNoteEl.find('.note-text').text().trim()).toEqual('');
done();
});
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment