浏览代码

Added inital inlineautoformatengine tests.

Maksymilian Barnaś 9 年之前
父节点
当前提交
720c678fa7
共有 1 个文件被更改,包括 139 次插入0 次删除
  1. 139 0
      packages/ckeditor5-autoformat/tests/inlineautoformatengine.js

+ 139 - 0
packages/ckeditor5-autoformat/tests/inlineautoformatengine.js

@@ -0,0 +1,139 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import InlineAutoformatEngine from '/ckeditor5/autoformat/inlineautoformatengine.js';
+import Paragraph from '/ckeditor5/paragraph/paragraph.js';
+import VirtualTestEditor from '/tests/core/_utils/virtualtesteditor.js';
+import Enter from '/ckeditor5/enter/enter.js';
+import { setData, getData } from '/ckeditor5/engine/dev-utils/model.js';
+import testUtils from '/tests/core/_utils/utils.js';
+import Command from '/ckeditor5/core/command/command.js';
+
+testUtils.createSinonSandbox();
+
+describe( 'InlineAutoformatEngine', () => {
+	let editor, doc, batch;
+
+	beforeEach( () => {
+		return VirtualTestEditor.create( {
+			features: [ Enter, Paragraph ]
+		} )
+		.then( newEditor => {
+			editor = newEditor;
+			doc = editor.document;
+			batch = doc.batch();
+		} );
+	} );
+
+	describe( 'Command name', () => {
+		it( 'should run a command when the pattern is matched', () => {
+			const spy = testUtils.sinon.spy();
+			editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
+			new InlineAutoformatEngine( editor, /^[\*]\s$/, 'testCommand' );
+
+			setData( doc, '<paragraph>*[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' ' );
+			} );
+
+			sinon.assert.calledOnce( spy );
+		} );
+
+		it( 'should remove found pattern', () => {
+			const spy = testUtils.sinon.spy();
+			editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
+			new InlineAutoformatEngine( editor, /^[\*]\s$/, 'testCommand' );
+
+			setData( doc, '<paragraph>*[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' ' );
+			} );
+
+			sinon.assert.calledOnce( spy );
+			expect( getData( doc ) ).to.equal( '<paragraph>[]</paragraph>' );
+		} );
+	} );
+
+	describe( 'Callback', () => {
+		it( 'should stop when there are no format ranges returned from testCallback', () => {
+			const formatSpy = testUtils.sinon.spy();
+			const testStub = testUtils.sinon.stub().returns( {
+				format: [ [] ],
+				remove: []
+			} );
+
+			new InlineAutoformatEngine( editor, testStub, formatSpy );
+
+			setData( doc, '<paragraph>*[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' ' );
+			} );
+
+			sinon.assert.notCalled( formatSpy );
+		} );
+
+		it( 'should stop when there are no remove ranges returned from testCallback', () => {
+			const formatSpy = testUtils.sinon.spy();
+			const testStub = testUtils.sinon.stub().returns( {
+				format: [],
+				remove: [ [] ]
+			} );
+
+			new InlineAutoformatEngine( editor, testStub, formatSpy );
+
+			setData( doc, '<paragraph>*[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' ' );
+			} );
+
+			sinon.assert.notCalled( formatSpy );
+		} );
+
+		it( 'takes text from nested elements', () => {
+			const formatSpy = testUtils.sinon.spy();
+			const testStub = testUtils.sinon.stub().returns( {
+				format: [],
+				remove: []
+			} );
+
+			new InlineAutoformatEngine( editor, testStub, formatSpy );
+
+			setData( doc, '<paragraph><paragraph>foobar[]</paragraph></paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' ' );
+			} );
+
+			sinon.assert.called( testStub );
+			sinon.assert.notCalled( formatSpy );
+			sinon.assert.calledWith( testStub, 'foobar' );
+		} );
+	} );
+} );
+
+/**
+ * Dummy command to execute.
+ */
+class TestCommand extends Command {
+	/**
+	 * Creates an instance of the command.
+	 *
+	 * @param {core.editor.Editor} editor Editor instance.
+	 * @param {Function} onExecuteCallback _doExecute call hook
+	 */
+	constructor( editor, onExecuteCallback ) {
+		super( editor );
+
+		this.onExecute = onExecuteCallback;
+	}
+
+	/**
+	 * Executes command.
+	 *
+	 * @protected
+	 */
+	_doExecute() {
+		this.onExecute();
+	}
+}