Browse Source

Tests: Added more accurate assertions.

Maciej Bukowski 7 years ago
parent
commit
f7b79874f7

+ 1 - 1
packages/ckeditor5-engine/src/view/observer/mutationobserver.js

@@ -204,8 +204,8 @@ export default class MutationObserver extends Observer {
 		}
 
 		for ( const viewElement of mutatedElements ) {
-			const viewChildren = Array.from( viewElement.getChildren() );
 			const domElement = domConverter.mapViewToDom( viewElement );
+			const viewChildren = Array.from( viewElement.getChildren() );
 			const newViewChildren = Array.from( domConverter.domChildrenToView( domElement ) );
 
 			// It may happen that as a result of many changes (sth was inserted and then removed),

+ 52 - 6
packages/ckeditor5-engine/tests/view/observer/mutationobserver.js

@@ -271,14 +271,21 @@ describe( 'MutationObserver', () => {
 
 	// https://github.com/ckeditor/ckeditor5/issues/692 Scenario 1.
 	it( 'should handle space after inline filler at the end of container', () => {
-		const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b></container:p>' );
+		const { view, selection } = parse(
+			'<container:p>' +
+				'foo' +
+				'<attribute:b>[]</attribute:b>' +
+			'</container:p>'
+		);
 
 		viewRoot.appendChildren( view );
 		viewDocument.selection.setTo( selection );
 
 		viewDocument.render();
 
-		const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
+		// Appended container is third in the tree.
+		const container = domEditor.childNodes[ 2 ];
+		const inlineFiller = container.childNodes[ 1 ].childNodes[ 0 ];
 
 		inlineFiller.data += ' ';
 
@@ -286,19 +293,31 @@ describe( 'MutationObserver', () => {
 
 		expect( lastMutations.length ).to.equal( 1 );
 		expect( lastMutations[ 0 ].type ).to.equal( 'children' );
+		expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
+		expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
+		expect( lastMutations[ 0 ].newChildren[ 0 ].is( 'text' ) ).to.be.true;
+		expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( ' ' );
 		expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
 	} );
 
 	// https://github.com/ckeditor/ckeditor5/issues/692 Scenario 3.
 	it( 'should handle space after inline filler at the end of container #2', () => {
-		const { view, selection } = parse( '<container:p>foo<attribute:b>bar</attribute:b>[]</container:p>' );
+		const { view, selection } = parse(
+			'<container:p>' +
+				'foo' +
+				'<attribute:b>bar</attribute:b>' +
+				'[]' +
+			'</container:p>'
+		);
 
 		viewRoot.appendChildren( view );
 		viewDocument.selection.setTo( selection );
 
 		viewDocument.render();
 
-		const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 2 ];
+		// Appended container is third in the tree.
+		const container = domEditor.childNodes[ 2 ];
+		const inlineFiller = container.childNodes[ 2 ];
 
 		inlineFiller.data += ' ';
 
@@ -306,19 +325,42 @@ describe( 'MutationObserver', () => {
 
 		expect( lastMutations.length ).to.equal( 1 );
 		expect( lastMutations[ 0 ].type ).to.equal( 'children' );
+		expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 2 );
+		expect( lastMutations[ 0 ].newChildren.length ).to.equal( 3 );
+
+		// Foo and attribute is removed and reinserted.
+		expect( lastMutations[ 0 ].oldChildren[ 0 ].is( 'text' ) ).to.be.true;
+		expect( lastMutations[ 0 ].oldChildren[ 0 ].data ).to.equal( 'foo' );
+		expect( lastMutations[ 0 ].newChildren[ 0 ].is( 'text' ) ).to.be.true;
+		expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
+
+		expect( lastMutations[ 0 ].oldChildren[ 1 ].is( 'attributeElement' ) ).to.be.true;
+		expect( lastMutations[ 0 ].oldChildren[ 1 ].name ).to.equal( 'b' );
+		expect( lastMutations[ 0 ].newChildren[ 1 ].is( 'attributeElement' ) ).to.be.true;
+		expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'b' );
+
+		expect( lastMutations[ 0 ].newChildren[ 2 ].is( 'text' ) ).to.be.true;
+		expect( lastMutations[ 0 ].newChildren[ 2 ].data ).to.equal( ' ' );
 		expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
 	} );
 
 	// https://github.com/ckeditor/ckeditor5/issues/692 Scenario 2.
 	it( 'should handle space after inline filler at the beginning of container', () => {
-		const { view, selection } = parse( '<container:p><attribute:b>[]</attribute:b>foo</container:p>' );
+		const { view, selection } = parse(
+			'<container:p>' +
+				'<attribute:b>[]</attribute:b>' +
+				'foo' +
+			'</container:p>'
+		);
 
 		viewRoot.appendChildren( view );
 		viewDocument.selection.setTo( selection );
 
 		viewDocument.render();
 
-		const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 0 ].childNodes[ 0 ];
+		// Appended container is third in the tree.
+		const container = domEditor.childNodes[ 2 ];
+		const inlineFiller = container.childNodes[ 0 ].childNodes[ 0 ];
 
 		inlineFiller.data += ' ';
 
@@ -326,6 +368,10 @@ describe( 'MutationObserver', () => {
 
 		expect( lastMutations.length ).to.equal( 1 );
 		expect( lastMutations[ 0 ].type ).to.equal( 'children' );
+		expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
+		expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
+		expect( lastMutations[ 0 ].newChildren[ 0 ].is( 'text' ) ).to.be.true;
+		expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( ' ' );
 		expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
 	} );