forked from summernote/summernote
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* HintPopover.spec.js | ||
* (c) 2015~ Summernote Team | ||
* summernote may be freely distributed under the MIT license./ | ||
*/ | ||
import chai from 'chai'; | ||
import $ from 'jquery'; | ||
import Context from '../../../src/js/base/Context'; | ||
import range from '../../../src/js/base/core/range'; | ||
import env from '../../../src/js/base/core/env'; | ||
import key from '../../../src/js/base/core/key'; | ||
import '../../../src/js/bs4/settings'; | ||
|
||
describe('HintPopover', () => { | ||
var expect = chai.expect; | ||
var $note, editor, context, $editable; | ||
|
||
function expectContents(context, markup) { | ||
expect(context.layoutInfo.editable.html()).to.equalsIgnoreCase(markup); | ||
} | ||
|
||
beforeEach(function() { | ||
$('body').empty(); // important ! | ||
$note = $('<div><p>hello world</p></div>'); | ||
$note.appendTo('body'); | ||
|
||
var options = $.extend({}, $.summernote.options, { | ||
hint: { | ||
mentions: ['jayden', 'sam', 'alvin', 'david'], | ||
match: /^\w*#(\w*)$/, | ||
search: function(keyword, callback) { | ||
callback($.grep(this.mentions, function(item) { | ||
return item.indexOf(keyword) === 0; | ||
})); | ||
}, | ||
content: function(item) { | ||
return item; | ||
}, | ||
}, | ||
}); | ||
|
||
context = new Context($note, options); | ||
editor = context.modules.editor; | ||
$editable = context.layoutInfo.editable; | ||
|
||
// [workaround] | ||
// - Safari does not popup hintpopover | ||
// - IE8-11 can't create range in headless mode | ||
if (env.isMSIE || env.isSafari) { | ||
this.skip(); | ||
} | ||
}); | ||
|
||
it('should not be shown without matches', () => { | ||
expect($('.note-hint-popover').css('display')).to.equals('none'); | ||
}); | ||
|
||
it('should be shown when it matches the given condition', (done) => { | ||
var textNode = $editable.find('p')[0].firstChild; | ||
editor.setLastRange(range.create(textNode, 5, textNode, 5).select()); | ||
editor.insertText(' #'); | ||
$editable.keyup(); | ||
|
||
setTimeout(() => { | ||
expect($('.note-hint-popover').css('display')).to.equals('block'); | ||
done(); | ||
}, 10); | ||
}); | ||
|
||
it('should be replaced with the selected hint', (done) => { | ||
var textNode = $editable.find('p')[0].firstChild; | ||
editor.setLastRange(range.create(textNode, 5, textNode, 5).select()); | ||
editor.insertText(' #'); | ||
$editable.keyup(); | ||
|
||
setTimeout(() => { | ||
var e = $.Event('keydown'); | ||
e.keyCode = key.code.ENTER; | ||
$note.trigger('summernote.keydown', e); | ||
|
||
setTimeout(() => { | ||
expectContents(context, '<p>hello #jayden world</p>'); | ||
done(); | ||
}, 10); | ||
}, 10); | ||
}); | ||
|
||
it('should move selection by pressing arrow key', (done) => { | ||
var textNode = $editable.find('p')[0].firstChild; | ||
editor.setLastRange(range.create(textNode, 5, textNode, 5).select()); | ||
editor.insertText(' #'); | ||
$editable.keyup(); | ||
|
||
setTimeout(() => { | ||
var e = $.Event('keydown'); | ||
e.keyCode = key.code.DOWN; | ||
$note.trigger('summernote.keydown', e); | ||
e.keyCode = key.code.ENTER; | ||
$note.trigger('summernote.keydown', e); | ||
|
||
setTimeout(() => { | ||
expectContents(context, '<p>hello #sam world</p>'); | ||
done(); | ||
}, 10); | ||
}, 10); | ||
}); | ||
}); |