Explorar el Código

Improved view writer's code coverage.

Szymon Kupś hace 8 años
padre
commit
a635fb8a4b

+ 1 - 12
packages/ckeditor5-engine/src/view/writer.js

@@ -58,7 +58,7 @@ export default class Writer {
 	 *
 	 * 		// Removes all ranges.
 	 *		writer.setSelection( null );
-
+	 *
 	 * @param {module:engine/view/selection~Selection|module:engine/view/position~Position|
 	 * Iterable.<module:engine/view/range~Range>|module:engine/view/range~Range|module:engine/view/item~Item|null} selectable
 	 * @param {Boolean|Number|'before'|'end'|'after'} [backwardSelectionOrOffset]
@@ -123,17 +123,6 @@ export default class Writer {
 		return new Element( name, attributes );
 	}
 
-	/**
-	 * Creates new {@link module:engine/view/documentfragment~DocumentFragment document fragment}.
-	 *
-	 *		writer.createDocumentFragment();
-	 *
-	 * @returns {module:engine/view/documentfragment~DocumentFragment} Created document fragment.
-	 */
-	createDocumentFragment() {
-		return new DocumentFragment();
-	}
-
 	/**
 	 * Creates new {@link module:engine/view/attributeelement~AttributeElement}.
 	 *

+ 0 - 0
packages/ckeditor5-engine/tests/view/writer/wrapposition.js


+ 109 - 4
packages/ckeditor5-engine/tests/view/writer/writer.js

@@ -5,21 +5,126 @@
 
 import Writer from '../../../src/view/writer';
 import Document from '../../../src/view/document';
-import Text from '../../../src/view/text';
+import EditableElement from '../../../src/view/editableelement';
+import ViewPosition from '../../../src/view/position';
+import createViewRoot from '../_utils/createroot';
 
 describe( 'Writer', () => {
-	let writer;
+	let writer, attributes, root;
 
 	before( () => {
-		writer = new Writer( new Document() );
+		attributes = { foo: 'bar', baz: 'quz' };
+		const document = new Document();
+		root = createViewRoot( document );
+		writer = new Writer( document );
+	} );
+
+	describe( 'setSelection()', () => {
+		it( 'should use selection._setTo method internally', () => {
+			const spy = sinon.spy( writer.document.selection, '_setTo' );
+			const position = ViewPosition.createAt( root );
+			writer.setSelection( position, true );
+
+			sinon.assert.calledWithExactly( spy, position, true );
+			spy.restore();
+		} );
+	} );
+
+	describe( 'setSelectionFocus()', () => {
+		it( 'should use selection._setFocus method internally', () => {
+			const spy = sinon.spy( writer.document.selection, '_setFocus' );
+			writer.setSelectionFocus( root, 0 );
+
+			sinon.assert.calledWithExactly( spy, root, 0 );
+			spy.restore();
+		} );
+	} );
+
+	describe( 'setFakeSelection()', () => {
+		it( 'should use selection._setFake method internally', () => {
+			const spy = sinon.spy( writer.document.selection, '_setFake' );
+			const options = {};
+			writer.setFakeSelection( true, options );
+
+			sinon.assert.calledWithExactly( spy, true, options );
+			spy.restore();
+		} );
 	} );
 
 	describe( 'createText()', () => {
 		it( 'should create Text instance', () => {
 			const text = writer.createText( 'foo bar' );
 
-			expect( text ).to.be.instanceOf( Text );
+			expect( text.is( 'text' ) ).to.be.true;
 			expect( text.data ).to.equal( 'foo bar' );
 		} );
 	} );
+
+	describe( 'createElement()', () => {
+		it( 'should create Element', () => {
+			const element = writer.createElement( 'foo', attributes );
+
+			expect( element.is( 'element' ) ).to.be.true;
+			expect( element.name ).to.equal( 'foo' );
+			assertElementAttributes( element, attributes );
+		} );
+	} );
+
+	describe( 'createAttributeElement()', () => {
+		it( 'should create AttributeElement', () => {
+			const element = writer.createAttributeElement( 'foo', attributes );
+
+			expect( element.is( 'attributeElement' ) ).to.be.true;
+			expect( element.name ).to.equal( 'foo' );
+			assertElementAttributes( element, attributes );
+		} );
+	} );
+
+	describe( 'createContainerElement()', () => {
+		it( 'should create ContainerElement', () => {
+			const element = writer.createContainerElement( 'foo', attributes );
+
+			expect( element.is( 'containerElement' ) ).to.be.true;
+			expect( element.name ).to.equal( 'foo' );
+			assertElementAttributes( element, attributes );
+		} );
+	} );
+
+	describe( 'createEditableElement()', () => {
+		it( 'should create EditableElement', () => {
+			const element = writer.createEditableElement( 'foo', attributes );
+
+			expect( element ).to.be.instanceOf( EditableElement );
+			expect( element.name ).to.equal( 'foo' );
+			assertElementAttributes( element, attributes );
+		} );
+	} );
+
+	describe( 'createEmptyElement()', () => {
+		it( 'should create EmptyElement', () => {
+			const element = writer.createEmptyElement( 'foo', attributes );
+
+			expect( element.is( 'emptyElement' ) ).to.be.true;
+			expect( element.name ).to.equal( 'foo' );
+			assertElementAttributes( element, attributes );
+		} );
+	} );
+
+	describe( 'createUIElement()', () => {
+		it( 'should create UIElement', () => {
+			const element = writer.createUIElement( 'foo', attributes );
+
+			expect( element.is( 'uiElement' ) ).to.be.true;
+			expect( element.name ).to.equal( 'foo' );
+			assertElementAttributes( element, attributes );
+		} );
+	} );
+
+	function assertElementAttributes( element, attributes ) {
+		for ( const key of Object.keys( attributes ) ) {
+			if ( element.getAttribute( key ) !== attributes[ key ] ) {
+				throw new Error( 'Attributes in element are different that those passed to the constructor method.' );
+			}
+		}
+	}
 } );