/** * Editor.spec.js * (c) 2015~ Summernote Team * summernote may be freely distributed under the MIT license./ */ import chai from 'chai'; import spies from 'chai-spies'; import chaidom from '../../../chaidom'; import $ from 'jquery'; import env from '../../../../src/js/base/core/env'; import range from '../../../../src/js/base/core/range'; import Context from '../../../../src/js/base/Context'; describe('Editor', () => { var expect = chai.expect; chai.use(spies); chai.use(chaidom); var editor, context, $editable; function expectContents(context, markup) { expect(context.layoutInfo.editable.html()).to.equalsIgnoreCase(markup); } function expectToHaveBeenCalled(context, customEvent, handler) { const $note = context.layoutInfo.note; const spy = chai.spy(); $note.on(customEvent, spy); handler(); expect(spy).to.have.been.called(); } beforeEach(function() { $('body').empty(); // important ! var $note = $('
hello
hello world
'); editor.undo(); expectContents(context, 'hello
'); editor.redo(); expectContents(context, 'hello world
'); }); }); } describe('tab', () => { it('should insert tab', () => { editor.tab(); expectContents(context, 'hello
'); }); }); describe('insertParagraph', () => { it('should insert paragraph', () => { editor.insertParagraph(); expectContents(context, 'hello
hello
hello
hello
'); }); }); describe('indent and outdent', () => { // [workaround] style is different by browser if (env.isPhantom) { it('should indent and outdent paragraph', () => { editor.indent(); expectContents(context, 'hello
'); editor.outdent(); expectContents(context, 'hello
'); }); } it('should indent and outdent list', () => { editor.insertOrderedList(); expectContents(context, 'hello world
'); }); it('should be limited', () => { var options = $.extend({}, $.summernote.options); options.langInfo = $.extend(true, {}, $.summernote.lang['en-US'], $.summernote.lang[options.lang]); options.maxTextLength = 5; context = new Context($('hello
hello
'); }); }); describe('insertText', () => { it('should insert text', () => { editor.insertText(' world'); expectContents(context, 'hello world
'); }); it('should be limited', () => { var options = $.extend({}, $.summernote.options); options.langInfo = $.extend(true, {}, $.summernote.lang['en-US'], $.summernote.lang[options.lang]); options.maxTextLength = 5; context = new Context($('hello
hello
'); }); }); describe('pasteHTML', () => { it('should paste html', () => { editor.pasteHTML(' world'); expectContents(context, 'hello world
'); }); it('should not call change change event more than once per paste event', () => { var generateLargeHtml = () => { var html = 'HTML element #' + i + '
'; } html += 'hello
hello
'); }); }); describe('insertHorizontalRule', () => { it('should insert horizontal rule', () => { editor.insertHorizontalRule(); expectContents(context, 'hello
hello
', 'hello
=>helloexpectContents(context, '
hello'); }); it('should apply multi formatBlock', () => { // set multi block html var codes = [ '', '', '' ]; context.invoke('code', codes.join('')); // run formatBlock $editable.appendTo('body'); editor.formatBlock('blockquote'); // check current range position in blockquote element var nodeName = $editable.children()[0].nodeName; expect(nodeName).to.equalsIgnoreCase('blockquote'); }); it('should apply multi test 2 - formatBlock', () => { var codes = [ '', '', '' ]; context.invoke('code', codes.join('')); $editable.appendTo('body'); var startNode = $editable.find('p').first()[0]; var endNode = $editable.find('p').last()[0]; // all p tags is wrapped range.create(startNode, 1, endNode, 1).normalize().select(); editor.formatBlock('blockquote'); var nodeName = $editable.children()[0].nodeName; expect(nodeName).to.equalsIgnoreCase('blockquote'); // p -> blockquote, p is none expect($editable.find('p').length).to.equals(0); }); it('should apply custom className in formatBlock', () => { var $target = $(''); $editable.appendTo('body'); editor.formatBlock('blockquote', $target); // start
hello
=>helloexpectContents(context, '
hello'); }); }); describe('createLink', () => { it('should create normal link', () => { var text = 'hello'; var pNode = $editable.find('p')[0]; var textNode = pNode.childNodes[0]; var startIndex = textNode.wholeText.indexOf(text); var endIndex = startIndex + text.length; range.create(textNode, startIndex, textNode, endIndex).normalize().select(); // check creation normal link editor.createLink({ url: 'http://summernote.org', text: 'summernote' }); expectContents(context, '
hellosummernote
'); }); it('should create a link with range', () => { var text = 'hello'; var pNode = $editable.find('p')[0]; var textNode = pNode.childNodes[0]; var startIndex = textNode.wholeText.indexOf(text); var endIndex = startIndex + text.length; var rng = range.create(textNode, startIndex, textNode, endIndex); editor.createLink({ url: 'http://summernote.org', text: 'summernote', range: rng }); expectContents(context, ''); }); it('should create a link with isNewWindow', () => { var text = 'hello'; var pNode = $editable.find('p')[0]; var textNode = pNode.childNodes[0]; var startIndex = textNode.wholeText.indexOf(text); var endIndex = startIndex + text.length; var rng = range.create(textNode, startIndex, textNode, endIndex); editor.createLink({ url: 'http://summernote.org', text: 'summernote', range: rng, isNewWindow: true }); expectContents(context, ''); }); it('should create a relative link without scheme', () => { var text = 'hello'; var pNode = $editable.find('p')[0]; var textNode = pNode.childNodes[0]; var startIndex = textNode.wholeText.indexOf(text); var endIndex = startIndex + text.length; var rng = range.create(textNode, startIndex, textNode, endIndex); editor.createLink({ url: '/relative/url', text: 'summernote', range: rng, isNewWindow: true }); expectContents(context, ''); }); it('should modify a link', () => { context.invoke('code', ''); var anchorNode = $editable.find('a')[0]; var rng = range.createFromNode(anchorNode); editor.createLink({ url: 'http://wow.summernote.org', text: 'summernote wow', range: rng }); expectContents(context, ''); }); it('should be limited when creating a link', () => { var options = $.extend({}, $.summernote.options); options.langInfo = $.extend(true, {}, $.summernote.lang['en-US'], $.summernote.lang[options.lang]); options.maxTextLength = 5; context = new Context($('hello
hello
'); }); it('should be limited when modifying a link', () => { var options = $.extend({}, $.summernote.options); options.langInfo = $.extend(true, {}, $.summernote.lang['en-US'], $.summernote.lang[options.lang]); options.maxTextLength = 5; context = new Context($(''), options); var editable = context.layoutInfo.editable; var anchorNode = editable.find('a')[0]; var rng = range.createFromNode(anchorNode); editor = context.modules.editor; editor.createLink({ url: 'http://summernote.org', text: 'hello world', range: rng }); expectContents(context, 'hello'); }); }); });