瀏覽代碼

Tests for the new classes.

Piotrek Koszuliński 9 年之前
父節點
當前提交
6b65af94a3

+ 95 - 0
tests/_utils-tests/classictesteditor.js

@@ -0,0 +1,95 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import StandardEditor from '/ckeditor5/editor/standardeditor.js';
+import ClassicTestEditor from '/tests/ckeditor5/_utils/classictesteditor.js';
+import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
+import BoxedEditorUI from '/ckeditor5/ui/editorui/boxed/boxededitorui.js';
+import Feature from '/ckeditor5/feature.js';
+
+import { getData } from '/tests/engine/_utils/model.js';
+import testUtils from '/tests/ckeditor5/_utils/utils.js';
+
+testUtils.createSinonSandbox();
+
+describe( 'ClassicTestEditor', () => {
+	let editorElement;
+
+	beforeEach( () => {
+		editorElement = document.createElement( 'div' );
+		document.body.appendChild( editorElement );
+	} );
+
+	describe( 'constructor', () => {
+		it( 'creates an instance of editor', () => {
+			const editor = new ClassicTestEditor( editorElement, { foo: 1 } );
+
+			expect( editor ).to.be.instanceof( StandardEditor );
+
+			expect( editor.config.get( 'foo' ) ).to.equal( 1 );
+			expect( editor ).to.have.property( 'element', editorElement );
+		} );
+
+		it( 'creates model and view roots', () => {
+			const editor = new ClassicTestEditor( { foo: 1 } );
+
+			expect( editor.document.getRoot() ).to.have.property( 'name', '$root' );
+			expect( editor.editing.view.getRoot() ).to.have.property( 'name', 'DIV' ); // See ckeditor5-engine#450.
+			expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
+		} );
+	} );
+
+	describe( 'create', () => {
+		it( 'creates an instance of editor', () => {
+			return ClassicTestEditor.create( editorElement, { foo: 1 } )
+				.then( editor => {
+					expect( editor ).to.be.instanceof( ClassicTestEditor );
+
+					expect( editor.config.get( 'foo' ) ).to.equal( 1 );
+					expect( editor ).to.have.property( 'element', editorElement );
+				} );
+		} );
+
+		it( 'creates and initilizes the UI', () => {
+			return ClassicTestEditor.create( editorElement, { foo: 1 } )
+				.then( editor => {
+					expect( editor.ui ).to.be.instanceof( BoxedEditorUI );
+				} );
+		} );
+
+		it( 'loads data from the editor element', () => {
+			editorElement.innerHTML = 'foo';
+
+			class FeatureTextInRoot extends Feature {
+				init() {
+					this.editor.document.schema.allow( { name: '$text', inside: '$root' } );
+				}
+			}
+
+			return ClassicTestEditor.create( editorElement, { features: [ FeatureTextInRoot ] } )
+				.then( editor => {
+					expect( getData( editor.document, { withoutSelection: true } ) ).to.equal( 'foo' );
+				} );
+		} );
+	} );
+
+	describe( 'destroy', () => {
+		it( 'destroys UI and calls super.destroy()', () => {
+			return ClassicTestEditor.create( editorElement, { foo: 1 } )
+				.then( editor => {
+					const superSpy = testUtils.sinon.spy( StandardEditor.prototype, 'destroy' );
+					const uiSpy = sinon.spy( editor.ui, 'destroy' );
+
+					return editor.destroy()
+						.then( () => {
+							expect( superSpy.calledOnce ).to.be.true;
+							expect( uiSpy.calledOnce ).to.be.true;
+						} );
+				} );
+		} );
+	} );
+} );

+ 22 - 0
tests/_utils-tests/virtualeditingcontroller.js

@@ -0,0 +1,22 @@
+/**
+ * @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 VirtualEditingController from '/tests/ckeditor5/_utils/virtualeditingcontroller.js';
+
+describe( 'VirtualEditingController', () => {
+	describe( 'constructor', () => {
+		it( 'sets necessary properties', () => {
+			const editor = new Editor();
+			const controller = new VirtualEditingController( editor.document );
+
+			expect( controller ).to.have.property( 'model', editor.document );
+			expect( controller ).to.have.property( 'view' );
+			expect( controller ).to.have.property( 'modelToView' );
+		} );
+	} );
+} );

+ 65 - 0
tests/_utils-tests/virtualtesteditor.js

@@ -0,0 +1,65 @@
+/**
+ * @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 VirtualTestEditor from '/tests/ckeditor5/_utils/virtualtesteditor.js';
+import VirtualEditingController from '/tests/ckeditor5/_utils/virtualeditingcontroller.js';
+import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
+import { getData, setData } from '/tests/engine/_utils/model.js';
+
+describe( 'VirtualTestEditor', () => {
+	describe( 'constructor', () => {
+		it( 'creates an instance of editor', () => {
+			const editor = new VirtualTestEditor( { foo: 1 } );
+
+			expect( editor ).to.be.instanceof( Editor );
+		} );
+
+		it( 'sets necessary properties', () => {
+			const editor = new VirtualTestEditor( { foo: 1 } );
+
+			expect( editor.config.get( 'foo' ) ).to.equal( 1 );
+
+			expect( editor.editing ).to.be.instanceof( VirtualEditingController );
+			expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
+		} );
+	} );
+
+	describe( 'setData', () => {
+		let editor;
+
+		beforeEach( () => {
+			editor = new VirtualTestEditor();
+			editor.document.schema.allow( { name: '$text', inside: '$root' } );
+		} );
+
+		it( 'should set data', () => {
+			editor.document.createRoot();
+
+			editor.setData( 'foo' );
+
+			expect( getData( editor.document, { rootName: 'main', withoutSelection: true } ) ).to.equal( 'foo' );
+		} );
+	} );
+
+	describe( 'getData', () => {
+		let editor;
+
+		beforeEach( () => {
+			editor = new VirtualTestEditor();
+			editor.document.schema.allow( { name: '$text', inside: '$root' } );
+		} );
+
+		it( 'should get data', () => {
+			editor.document.createRoot();
+
+			setData( editor.document, 'foo' );
+
+			expect( editor.getData() ).to.equal( 'foo' );
+		} );
+	} );
+} );

+ 2 - 0
tests/_utils/classictesteditor.js

@@ -64,6 +64,7 @@ export default class ClassicTestEditor extends StandardEditor {
 	/**
 	 * Creates boxed editor UI.
 	 *
+	 * @protected
 	 * @returns {Promise}
 	 */
 	_createUI() {
@@ -80,6 +81,7 @@ export default class ClassicTestEditor extends StandardEditor {
 	/**
 	 * Initilizes editor UI.
 	 *
+	 * @protected
 	 * @returns {Promise}
 	 */
 	_initUI() {

+ 13 - 0
tests/editor/editor-base.js

@@ -39,6 +39,19 @@ describe( 'Editor', () => {
 				expect( spy.calledOnce ).to.be.true;
 			} );
 		} );
+
+		it( 'should destroy all components it initialized', () => {
+			const editor = new Editor();
+
+			const spy1 = sinon.spy( editor.data, 'destroy' );
+			const spy2 = sinon.spy( editor.document, 'destroy' );
+
+			return editor.destroy()
+				.then( () => {
+					expect( spy1.calledOnce ).to.be.true;
+					expect( spy2.calledOnce ).to.be.true;
+				} );
+		} );
 	} );
 
 	describe( 'execute', () => {

+ 8 - 1
tests/editor/editor.js

@@ -53,7 +53,14 @@ describe( 'Editor', () => {
 		} );
 
 		it( 'loads plugins', () => {
-			// TODO
+			return Editor.create( {
+					features: [ 'A' ]
+				} )
+				.then( editor => {
+					expect( getPlugins( editor ).length ).to.equal( 1 );
+
+					expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
+				} );
 		} );
 	} );
 

+ 71 - 6
tests/editor/standardeditor.js

@@ -11,14 +11,57 @@ import StandardEditor from '/ckeditor5/editor/standardeditor.js';
 import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
 import { getData, setData } from '/tests/engine/_utils/model.js';
 
+import EditingController from '/ckeditor5/engine/editingcontroller.js';
+import KeystrokeHandler from '/ckeditor5/keystrokehandler.js';
+import Feature from '/ckeditor5/feature.js';
+
 describe( 'StandardEditor', () => {
+	let editorElement;
+
+	beforeEach( () => {
+		editorElement = document.createElement( 'div' );
+		document.body.appendChild( editorElement );
+	} );
+
+	describe( 'constructor', () => {
+		it( 'sets all properties', () => {
+			const editor = new StandardEditor( editorElement, { foo: 1 } );
+
+			expect( editor ).to.have.property( 'element', editorElement );
+			expect( editor.editing ).to.be.instanceof( EditingController );
+			expect( editor.keystrokes ).to.be.instanceof( KeystrokeHandler );
+		} );
+
+		it( 'sets config', () => {
+			const editor = new StandardEditor( editorElement, { foo: 1 } );
+
+			expect( editor.config.get( 'foo' ) ).to.equal( 1 );
+		} );
+	} );
+
+	describe( 'create', () => {
+		it( 'initializes editor with plugins and config', () => {
+			class FeatureFoo extends Feature {}
+
+			return StandardEditor.create( editorElement, {
+					foo: 1,
+					features: [ FeatureFoo ]
+				} )
+				.then( editor => {
+					expect( editor ).to.be.instanceof( StandardEditor );
+
+					expect( editor.config.get( 'foo' ) ).to.equal( 1 );
+					expect( editor ).to.have.property( 'element', editorElement );
+
+					expect( editor.plugins.get( FeatureFoo ) ).to.be.instanceof( FeatureFoo );
+				} );
+		} );
+	} );
+
 	describe( 'setData', () => {
 		let editor;
 
 		beforeEach( () => {
-			const editorElement = document.createElement( 'div' );
-			document.body.appendChild( editorElement );
-
 			return StandardEditor.create( editorElement )
 				.then( newEditor => {
 					editor = newEditor;
@@ -46,9 +89,6 @@ describe( 'StandardEditor', () => {
 		let editor;
 
 		beforeEach( () => {
-			const editorElement = document.createElement( 'div' );
-			document.body.appendChild( editorElement );
-
 			return StandardEditor.create( editorElement )
 				.then( newEditor => {
 					editor = newEditor;
@@ -68,4 +108,29 @@ describe( 'StandardEditor', () => {
 			expect( editor.getData() ).to.equal( 'foo' );
 		} );
 	} );
+
+	describe( 'updateEditorElement', () => {
+		it( 'sets data to editor element', () => {
+			const editor = new StandardEditor( editorElement );
+
+			editor.data.get = () => '<p>foo</p>';
+
+			editor.updateEditorElement();
+
+			expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
+		} );
+	} );
+
+	describe( 'loadDataFromEditorElement', () => {
+		it( 'sets data to editor element', () => {
+			const editor = new StandardEditor( editorElement );
+
+			sinon.stub( editor.data, 'set' );
+			editorElement.innerHTML = '<p>foo</p>';
+
+			editor.loadDataFromEditorElement();
+
+			expect( editor.data.set.calledWithExactly( '<p>foo</p>' ) ).to.be.true;
+		} );
+	} );
 } );