Explorar o código

Introduced ModelTestEditor. Use a fixed engine.

Piotrek Koszuliński %!s(int64=9) %!d(string=hai) anos
pai
achega
da3577190d

+ 1 - 1
package.json

@@ -11,7 +11,7 @@
     "ckeditor5-basic-styles": "ckeditor/ckeditor5-basic-styles#t/1",
     "ckeditor5-creator-classic": "ckeditor/ckeditor5-creator-classic#t/9",
     "ckeditor5-delete": "ckeditor/ckeditor5-delete#t/6",
-    "ckeditor5-engine": "ckeditor/ckeditor5-engine",
+    "ckeditor5-engine": "ckeditor/ckeditor5-engine#t/452",
     "ckeditor5-enter": "ckeditor/ckeditor5-enter#t/12",
     "ckeditor5-paragraph": "ckeditor/ckeditor5-paragraph#t/1",
     "ckeditor5-theme-lark": "ckeditor/ckeditor5-theme-lark",

+ 85 - 0
tests/_utils-tests/modeltesteditor.js

@@ -0,0 +1,85 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import Editor from '/ckeditor5/editor/editor.js';
+import ModelTestEditor from '/tests/ckeditor5/_utils/virtualtesteditor.js';
+import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
+import { getData, setData } from '/tests/engine/_utils/model.js';
+
+import testUtils from '/tests/ckeditor5/_utils/utils.js';
+
+testUtils.createSinonSandbox();
+
+describe( 'ModelTestEditor', () => {
+	describe( 'constructor', () => {
+		it( 'creates an instance of editor', () => {
+			const editor = new ModelTestEditor( { foo: 1 } );
+
+			expect( editor ).to.be.instanceof( Editor );
+
+			expect( editor.config.get( 'foo' ) ).to.equal( 1 );
+		} );
+
+		it( 'creates model and view roots', () => {
+			const editor = new ModelTestEditor( { foo: 1 } );
+
+			expect( editor.document.getRoot() ).to.have.property( 'name', '$root' );
+			expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
+		} );
+	} );
+
+	describe( 'create', () => {
+		it( 'creates an instance of editor', () => {
+			return ModelTestEditor.create( { foo: 1 } )
+				.then( editor => {
+					expect( editor ).to.be.instanceof( ModelTestEditor );
+
+					expect( editor.config.get( 'foo' ) ).to.equal( 1 );
+				} );
+		} );
+	} );
+
+	describe( 'setData', () => {
+		let editor;
+
+		beforeEach( () => {
+			return ModelTestEditor.create()
+				.then( newEditor => {
+					editor = newEditor;
+
+					editor.document.schema.allow( { name: '$text', inside: '$root' } );
+				} );
+		} );
+
+		it( 'should set data of the first root', () => {
+			editor.document.createRoot( 'secondRoot' );
+
+			editor.setData( 'foo' );
+
+			expect( getData( editor.document, { rootName: 'main', withoutSelection: true } ) ).to.equal( 'foo' );
+		} );
+	} );
+
+	describe( 'getData', () => {
+		let editor;
+
+		beforeEach( () => {
+			return ModelTestEditor.create()
+				.then( newEditor => {
+					editor = newEditor;
+
+					editor.document.schema.allow( { name: '$text', inside: '$root' } );
+				} );
+		} );
+
+		it( 'should set data of the first root', () => {
+			setData( editor.document, 'foo' );
+
+			expect( editor.getData() ).to.equal( 'foo' );
+		} );
+	} );
+} );

+ 61 - 0
tests/_utils/modeltesteditor.js

@@ -0,0 +1,61 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import Editor from '/ckeditor5/editor/editor.js';
+import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
+
+/**
+ * A simple editor implementation with a functional model part of the engine (the document).
+ * It contains a full data pipeline but no editing pipeline.
+ *
+ * Should work in Node.js. If not now, then in the future :).
+ *
+ * @memberOf tests.ckeditor5._utils
+ */
+export default class ModelTestEditor extends Editor {
+	constructor( config ) {
+		super( config );
+
+		this.document.createRoot();
+
+		this.data.processor = new HtmlDataProcessor();
+	}
+
+	/**
+	 * Sets the data in the editor's main root.
+	 *
+	 * @param {*} data The data to load.
+	 */
+	setData( data ) {
+		this.data.set( data );
+	}
+
+	/**
+	 * Gets the data from the editor's main root.
+	 */
+	getData() {
+		return this.data.get();
+	}
+
+	/**
+	 * Creates a virtual, element-less editor instance.
+	 *
+	 * @param {Object} config See {@link ckeditor5.editor.StandardEditor}'s param.
+	 * @returns {Promise} Promise resolved once editor is ready.
+	 * @returns {ckeditor5.editor.VirtualTestEditor} return.editor The editor instance.
+	 */
+	static create( config ) {
+		return new Promise( ( resolve ) => {
+			const editor = new this( config );
+
+			resolve(
+				editor.initPlugins()
+					.then( () => editor )
+			);
+		} );
+	}
+}

+ 2 - 2
tests/_utils/virtualtesteditor.js

@@ -9,8 +9,8 @@ import StandardEditor from '/ckeditor5/editor/standardeditor.js';
 import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
 
 /**
- * A simple editor implementation which features a {@link tests.ckeditor5._utils.VirtualEditingController}.
- * Useful for testing engine parts of features.
+ * A simple editor implementation useful for testing the engine part of the features.
+ * It contains full data pipepilne and the engine pipeline but without rendering to DOM.
  *
  * Should work in Node.js. If not now, then in the future :).
  *