Procházet zdrojové kódy

Fixed and added Document tests.

Szymon Cofalik před 10 roky
rodič
revize
f6fc013ac9

+ 15 - 1
packages/ckeditor5-utils/src/document/document.js

@@ -98,7 +98,7 @@ CKEDITOR.define( [
 			}
 
 			var root = new RootElement( this );
-			this.roots.set( root, name );
+			this.roots.set( name, root );
 
 			return root;
 		}
@@ -110,6 +110,20 @@ CKEDITOR.define( [
 		 * @returns (document.RootElement} Root registered under given name.
 		 */
 		getRoot( name ) {
+			if ( !this.roots.has( name ) ) {
+				/**
+				 * Root with specified name does not exist.
+				 *
+				 * @error document-createRoot-root-not-exist
+				 * @param {document.Document} doc
+				 * @param {String} name
+				 */
+				throw new CKEditorError(
+					'document-createRoot-root-not-exist: Root with specified name does not exist.',
+					{ doc: this, name: name }
+				);
+			}
+
 			return this.roots.get( name );
 		}
 

+ 72 - 33
packages/ckeditor5-utils/tests/document/document.js

@@ -7,54 +7,93 @@
 
 'use strict';
 
-var modules = bender.amd.require( 'document/document', 'document/element', 'ckeditorerror' );
+var modules = bender.amd.require( 'document/document', 'document/rootelement', 'ckeditorerror' );
 
-describe( 'constructor', function() {
-	it( 'should create Document with no data', function() {
-		var Document = modules[ 'document/document' ];
-		var Element = modules[ 'document/element' ];
+describe( 'Document', function() {
+	var Document, RootElement, CKEditorError;
 
-		var document = new Document();
+	before( function() {
+		Document = modules[ 'document/document' ];
+		RootElement = modules[ 'document/rootelement' ];
+		CKEditorError = modules[ 'ckeditorerror' ];
+	} );
+
+	var document;
 
-		expect( document ).to.have.property( 'root' ).that.is.instanceof( Element );
-		expect( document.root ).to.have.property( 'name' ).that.equal( 'root' );
+	beforeEach( function() {
+		document = new Document();
 	} );
-} );
 
-describe( 'applyOperation', function() {
-	it( 'should increase document version, execute operation and fire operationApplied', function() {
-		var Document = modules[ 'document/document' ];
+	describe( 'constructor', function() {
+		it( 'should create Document with no data', function() {
+			expect( document ).to.have.property( 'roots' ).that.is.instanceof( Map );
+			expect( document.roots.size ).to.be.equal( 1 );
+			expect( document._graveyard ).to.be.instanceof( RootElement );
+			expect( document._graveyard.getChildCount() ).to.be.equal( 0 );
+		} );
+	} );
+
+	describe( 'createRoot', function() {
+		it( 'should create a new RootElement, add it to roots map and return it', function() {
+			var root = document.createRoot( 'root' );
+
+			expect( document.roots.size ).to.be.equal( 2 );
+			expect( root ).to.be.instanceof( RootElement );
+			expect( root.getChildCount() ).to.be.equal( 0 );
+		} );
+
+		it( 'should throw an error when trying to create a second root with the same name', function() {
+			document.createRoot( 'root' );
+
+			expect( function() {
+				document.createRoot( 'root' );
+			} ).to.throw( CKEditorError, /document-createRoot-name-exists/ );
+		} );
+	} );
+
+	describe( 'getRoot', function() {
+		it( 'should return a RootElement previously created with given name', function() {
+			var newRoot = document.createRoot( 'root' );
+			var getRoot = document.getRoot( 'root' );
+
+			expect( getRoot ).to.be.equal( newRoot );
+		} );
 
-		var document = new Document();
-		var operationApplied = sinon.spy();
-		var operation = {
+		it( 'should throw an error when trying to get non-existent root', function() {
+			expect( function() {
+				document.getRoot( 'root' );
+			} ).to.throw( CKEditorError, /document-createRoot-root-not-exist/ );
+		} );
+	} );
+
+	describe( 'applyOperation', function() {
+		it( 'should increase document version, execute operation and fire operationApplied', function() {
+			var operationApplied = sinon.spy();
+			var operation = {
 				baseVersion: 0,
 				_execute: sinon.spy()
 			};
 
-		document.on( 'operationApplied', operationApplied );
-
-		document.applyOperation( operation );
+			document.on( 'operationApplied', operationApplied );
 
-		expect( document.version ).to.be.equal( 1 );
-		sinon.assert.calledOnce( operationApplied );
-		sinon.assert.calledOnce( operation._execute );
-	} );
+			document.applyOperation( operation );
 
-	it( 'should throw an error on the operation base version and the document version is different', function() {
-		var Document = modules[ 'document/document' ];
-		var CKEditorError = modules.ckeditorerror;
+			expect( document.version ).to.be.equal( 1 );
+			sinon.assert.calledOnce( operationApplied );
+			sinon.assert.calledOnce( operation._execute );
+		} );
 
-		var document = new Document();
-		var operationApplied = sinon.spy();
-		var operation = {
+		it( 'should throw an error on the operation base version and the document version is different', function() {
+			var operationApplied = sinon.spy();
+			var operation = {
 				baseVersion: 1
 			};
 
-		document.on( 'operationApplied', operationApplied );
+			document.on( 'operationApplied', operationApplied );
 
-		expect( function() {
-			document.applyOperation( operation );
-		} ).to.throw( CKEditorError, /document-applyOperation-wrong-version/ );
+			expect( function() {
+				document.applyOperation( operation );
+			} ).to.throw( CKEditorError, /document-applyOperation-wrong-version/ );
+		} );
 	} );
-} );
+} );