aboutsummaryrefslogtreecommitdiffstats
path: root/apps/comments/tests
diff options
context:
space:
mode:
authorDaniel Calviño Sánchez <danxuliu@gmail.com>2017-12-07 04:59:29 +0100
committerDaniel Calviño Sánchez <danxuliu@gmail.com>2017-12-07 15:38:46 +0100
commit9b1f3b969e8c273d86901432421189295383d81f (patch)
treefb61cf9990886de5b0ff0235970b8fb56c55798c /apps/comments/tests
parentc43f64d56b517e061ceb48e30ebaa48cc7866236 (diff)
downloadnextcloud-server-9b1f3b969e8c273d86901432421189295383d81f.tar.gz
nextcloud-server-9b1f3b969e8c273d86901432421189295383d81f.zip
Fix Enter sending comment instead of adding autocomplete item to message
When the autocomplete popover is shown the At.js plugin listens on the message input field for key down events, and when Enter is pressed it adds the selected item to the message. However, as "_onTypeComment" also handles key down events for the message input field, when Enter was pressed the comment was submitted and At.js had no chance to add the item before that happened. Now when Enter is pressed and the autocomplete popover is shown the comment is not submitted, and thus At.js adds the selected item to the message as expected. Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
Diffstat (limited to 'apps/comments/tests')
-rw-r--r--apps/comments/tests/js/commentstabviewSpec.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/apps/comments/tests/js/commentstabviewSpec.js b/apps/comments/tests/js/commentstabviewSpec.js
index d4456728f02..813b2a72eae 100644
--- a/apps/comments/tests/js/commentstabviewSpec.js
+++ b/apps/comments/tests/js/commentstabviewSpec.js
@@ -271,6 +271,51 @@ describe('OCA.Comments.CommentsTabView tests', function() {
});
expect(keydownEvent.isDefaultPrevented()).toEqual(true);
});
+ it('creates a new mention when typing enter in the autocomplete popover', function() {
+ var autoCompleteStub = sinon.stub(view, '_onAutoComplete');
+ autoCompleteStub.callsArgWith(1, [{"id":"userId", "label":"User Name", "source":"users"}]);
+
+ // Force the autocomplete to be initialized
+ view._initAutoComplete($newCommentForm.find('.message'));
+
+ // PhantomJS does not seem to handle typing in a contenteditable, so
+ // some tricks are needed to show the autocomplete popover.
+ //
+ // Instead of sending key events to type "@u" the characters are
+ // programatically set in the input field.
+ $newCommentForm.find('.message').text('Mention to @u');
+
+ // When focusing on the input field the caret is not guaranteed to
+ // be at the end; instead of calling "focus()" on the input field
+ // the caret is explicitly set at the end of the input field, that
+ // is, after "@u".
+ var range = document.createRange();
+ range.selectNodeContents($newCommentForm.find('.message')[0]);
+ range.collapse(false);
+ var selection = window.getSelection();
+ selection.removeAllRanges();
+ selection.addRange(range);
+
+ // As PhantomJS does not handle typing in a contenteditable the key
+ // typed here is in practice ignored by At.js, but despite that it
+ // will cause the popover to be shown.
+ $newCommentForm.find('.message').trigger(new $.Event('keydown', {keyCode: 's'}));
+ $newCommentForm.find('.message').trigger(new $.Event('keyup', {keyCode: 's'}));
+
+ expect(autoCompleteStub.calledOnce).toEqual(true);
+
+ var keydownEvent = new $.Event('keydown', {keyCode: 13});
+ $newCommentForm.find('.message').trigger(keydownEvent);
+
+ expect(createStub.calledOnce).toEqual(false);
+ expect($newCommentForm.find('.message').html()).toContain('Mention to <span');
+ expect($newCommentForm.find('.message').html()).toContain('<div class="avatar"');
+ expect($newCommentForm.find('.message').html()).toContain('<strong>User Name</strong>');
+ expect($newCommentForm.find('.message').text()).not.toContain('@');
+ // In this case the default behaviour is prevented by the
+ // "onKeydown" event handler of At.js.
+ expect(keydownEvent.isDefaultPrevented()).toEqual(true);
+ });
it('creates a new line when typing shift+enter', function() {
$newCommentForm.find('.message').text('New message');
var keydownEvent = new $.Event('keydown', {keyCode: 13, shiftKey: true});