Преглед изворни кода

Tests: Test changes after fixing ckeditor5-engine#699: live selection shuld properly set its properties in case of non-collapsed default range.

Szymon Kupś пре 8 година
родитељ
комит
0fbe746720

+ 28 - 20
packages/ckeditor5-image/tests/image/ui/imageballoonpanelview.js

@@ -3,33 +3,41 @@
  * For licensing, see LICENSE.md.
  */
 
-/* global Event */
+/* global window, document, Event */
 
 import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
-import global from '@ckeditor/ckeditor5-utils/src/dom/global';
+
+import ImageEngine from '../../../src/image/imageengine';
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+
 import ImageBalloonPanel from '../../../src/image/ui/imageballoonpanelview';
 import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
-import ImageEngine from '../../../src/image/imageengine';
+
 import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 describe( 'ImageBalloonPanel', () => {
-	let editor, panel, document;
+	let editor, panel, doc, editorElement;
 
 	beforeEach( () => {
-		const editorElement = global.document.createElement( 'div' );
-		global.document.body.appendChild( editorElement );
+		editorElement = document.createElement( 'div' );
+		document.body.appendChild( editorElement );
 
-		return ClassicEditor.create( editorElement, { plugins: [ ImageEngine ] } )
+		return ClassicEditor
+			.create( editorElement, {
+				plugins: [ ImageEngine, Paragraph ]
+			} )
 			.then( newEditor => {
 				editor = newEditor;
 				panel = new ImageBalloonPanel( editor );
-				document = editor.document;
+				doc = editor.document;
 
 				return editor.ui.view.body.add( panel );
 			} );
 	} );
 
 	afterEach( () => {
+		editorElement.remove();
+
 		return editor.destroy();
 	} );
 
@@ -65,11 +73,11 @@ describe( 'ImageBalloonPanel', () => {
 	it( 'should detach when image element is no longer selected', () => {
 		const spy = sinon.spy( panel, 'detach' );
 
-		setData( document, '[<image src=""></image>]' );
+		setData( doc, '<paragraph>foo</paragraph>[<image src=""></image>]' );
 		panel.attach();
 
-		document.enqueueChanges( () => {
-			document.selection.removeAllRanges();
+		doc.enqueueChanges( () => {
+			doc.selection.removeAllRanges();
 		} );
 
 		sinon.assert.calledOnce( spy );
@@ -78,36 +86,36 @@ describe( 'ImageBalloonPanel', () => {
 	it( 'should attach panel correctly', () => {
 		const spy = sinon.spy( panel, 'attachTo' );
 
-		setData( document, '[<image src=""></image>]' );
+		setData( doc, '[<image src=""></image>]' );
 		panel.attach();
 
 		testPanelAttach( spy );
 	} );
 
 	it( 'should calculate panel position on scroll event', () => {
-		setData( document, '[<image src=""></image>]' );
+		setData( doc, '[<image src=""></image>]' );
 		panel.attach();
 
 		const spy = sinon.spy( panel, 'attachTo' );
 
-		global.window.dispatchEvent( new Event( 'scroll' ) );
+		window.dispatchEvent( new Event( 'scroll' ) );
 
 		testPanelAttach( spy );
 	} );
 
 	it( 'should calculate panel position on resize event event', () => {
-		setData( document, '[<image src=""></image>]' );
+		setData( doc, '[<image src=""></image>]' );
 		panel.attach();
 
 		const spy = sinon.spy( panel, 'attachTo' );
 
-		global.window.dispatchEvent( new Event( 'resize' ) );
+		window.dispatchEvent( new Event( 'resize' ) );
 
 		testPanelAttach( spy );
 	} );
 
 	it( 'should hide panel on detach', () => {
-		setData( document, '[<image src=""></image>]' );
+		setData( doc, '[<image src=""></image>]' );
 		panel.attach();
 
 		const spy = sinon.spy( panel, 'hide' );
@@ -118,14 +126,14 @@ describe( 'ImageBalloonPanel', () => {
 	} );
 
 	it( 'should not reposition panel after detaching', () => {
-		setData( document, '[<image src=""></image>]' );
+		setData( doc, '[<image src=""></image>]' );
 		panel.attach();
 
 		const spy = sinon.spy( panel, 'attachTo' );
 
 		panel.detach();
-		global.window.dispatchEvent( new Event( 'resize' ) );
-		global.window.dispatchEvent( new Event( 'scroll' ) );
+		window.dispatchEvent( new Event( 'resize' ) );
+		window.dispatchEvent( new Event( 'scroll' ) );
 
 		sinon.assert.notCalled( spy );
 	} );

+ 140 - 113
packages/ckeditor5-image/tests/imagecaption/imagecaptionengine.js

@@ -4,33 +4,41 @@
  */
 
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
+
+import ImageCaptionEngine from '../../src/imagecaption/imagecaptionengine';
+import ImageEngine from '../../src/image/imageengine';
+import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine';
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+
 import ViewAttributeElement from '@ckeditor/ckeditor5-engine/src/view/attributeelement';
 import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
 import viewWriter from '@ckeditor/ckeditor5-engine/src/view/writer';
 import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
 import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
 import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
-import ImageCaptionEngine from '../../src/imagecaption/imagecaptionengine';
-import ImageEngine from '../../src/image/imageengine';
-import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine';
+
 import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
+
 import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
 import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
 
 describe( 'ImageCaptionEngine', () => {
-	let editor, document, viewDocument;
+	let editor, doc, viewDocument;
 
 	beforeEach( () => {
-		return VirtualTestEditor.create( { plugins: [ ImageCaptionEngine, ImageEngine, UndoEngine ] } )
+		return VirtualTestEditor
+			.create( {
+				plugins: [ ImageCaptionEngine, ImageEngine, UndoEngine, Paragraph ]
+			} )
 			.then( newEditor => {
 				editor = newEditor;
-				document = editor.document;
+				doc = editor.document;
 				viewDocument = editor.editing.view;
-				document.schema.registerItem( 'widget' );
-				document.schema.allow( { name: 'widget', inside: '$root' } );
-				document.schema.allow( { name: 'caption', inside: 'widget' } );
-				document.schema.allow( { name: '$inline', inside: 'widget' } );
+				doc.schema.registerItem( 'widget' );
+				doc.schema.allow( { name: 'widget', inside: '$root' } );
+				doc.schema.allow( { name: 'caption', inside: 'widget' } );
+				doc.schema.allow( { name: '$inline', inside: 'widget' } );
 
 				buildViewConverter()
 					.for( editor.data.viewToModel )
@@ -49,10 +57,10 @@ describe( 'ImageCaptionEngine', () => {
 	} );
 
 	it( 'should set proper schema rules', () => {
-		expect( document.schema.check( { name: 'caption', iniside: 'image' } ) ).to.be.true;
-		expect( document.schema.check( { name: '$inline', inside: 'caption' } ) ).to.be.true;
-		expect( document.schema.itemExtends( 'caption', '$block' ) ).to.be.true;
-		expect( document.schema.limits.has( 'caption' ) );
+		expect( doc.schema.check( { name: 'caption', iniside: 'image' } ) ).to.be.true;
+		expect( doc.schema.check( { name: '$inline', inside: 'caption' } ) ).to.be.true;
+		expect( doc.schema.itemExtends( 'caption', '$block' ) ).to.be.true;
+		expect( doc.schema.limits.has( 'caption' ) );
 	} );
 
 	describe( 'data pipeline', () => {
@@ -60,28 +68,28 @@ describe( 'ImageCaptionEngine', () => {
 			it( 'should convert figcaption inside image figure', () => {
 				editor.setData( '<figure class="image"><img src="foo.png"/><figcaption>foo bar</figcaption></figure>' );
 
-				expect( getModelData( document, { withoutSelection: true } ) )
+				expect( getModelData( doc, { withoutSelection: true } ) )
 					.to.equal( '<image src="foo.png"><caption>foo bar</caption></image>' );
 			} );
 
 			it( 'should add empty caption if there is no figcaption', () => {
 				editor.setData( '<figure class="image"><img src="foo.png"/></figure>' );
 
-				expect( getModelData( document, { withoutSelection: true } ) )
+				expect( getModelData( doc, { withoutSelection: true } ) )
 					.to.equal( '<image src="foo.png"><caption></caption></image>' );
 			} );
 
 			it( 'should not convert figcaption inside other elements than image', () => {
 				editor.setData( '<widget><figcaption>foobar</figcaption></widget>' );
 
-				expect( getModelData( document, { withoutSelection: true } ) )
+				expect( getModelData( doc, { withoutSelection: true } ) )
 					.to.equal( '<widget>foobar</widget>' );
 			} );
 		} );
 
 		describe( 'model to view', () => {
 			it( 'should convert caption element to figcaption', () => {
-				setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
+				setModelData( doc, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
 
 				expect( editor.getData() ).to.equal(
 					'<figure class="image"><img src="img.png"><figcaption>Foo bar baz.</figcaption></figure>'
@@ -89,13 +97,14 @@ describe( 'ImageCaptionEngine', () => {
 			} );
 
 			it( 'should not convert caption to figcaption if it\'s empty', () => {
-				setModelData( document, '<image src="img.png"><caption></caption></image>' );
+				setModelData( doc, '<image src="img.png"><caption></caption></image>' );
 
 				expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"></figure>' );
 			} );
 
 			it( 'should not convert caption from other elements', () => {
-				setModelData( document, '<widget>foo bar<caption></caption></widget>' );
+				setModelData( doc, '<widget>foo bar<caption></caption></widget>' );
+
 				expect( editor.getData() ).to.equal( '<widget>foo bar</widget>' );
 			} );
 		} );
@@ -104,7 +113,7 @@ describe( 'ImageCaptionEngine', () => {
 	describe( 'editing pipeline', () => {
 		describe( 'model to view', () => {
 			it( 'should convert caption element to figcaption contenteditable', () => {
-				setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
+				setModelData( doc, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
 
 				expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
 					'<figure class="image ck-widget" contenteditable="false">' +
@@ -117,20 +126,21 @@ describe( 'ImageCaptionEngine', () => {
 			} );
 
 			it( 'should convert caption to element with proper CSS class if it\'s empty', () => {
-				setModelData( document, '<image src="img.png"><caption></caption></image>' );
+				setModelData( doc, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
 
 				expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
+					'<p>foo</p>' +
 					'<figure class="image ck-widget" contenteditable="false">' +
 						'<img src="img.png"></img>' +
 						'<figcaption class="ck-placeholder ck-editable ck-hidden" ' +
-									'contenteditable="true" data-placeholder="Enter image caption">' +
+							'contenteditable="true" data-placeholder="Enter image caption">' +
 						'</figcaption>' +
 					'</figure>'
 				);
 			} );
 
 			it( 'should not convert caption from other elements', () => {
-				setModelData( document, '<widget>foo bar<caption></caption></widget>' );
+				setModelData( doc, '<widget>foo bar<caption></caption></widget>' );
 				expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '<widget>foo bar</widget>' );
 			} );
 
@@ -150,7 +160,7 @@ describe( 'ImageCaptionEngine', () => {
 					{ priority: 'high' }
 				);
 
-				setModelData( document, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
+				setModelData( doc, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
 
 				expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
 					'<figure class="image ck-widget" contenteditable="false"><img src="img.png"></img><span></span>Foo bar baz.</figure>'
@@ -158,17 +168,19 @@ describe( 'ImageCaptionEngine', () => {
 			} );
 
 			it( 'should show caption when something is inserted inside', () => {
-				setModelData( document, '<image src="img.png"><caption></caption></image>' );
-				const image = document.getRoot().getChild( 0 );
+				setModelData( doc, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
+
+				const image = doc.getRoot().getChild( 1 );
 				const caption = image.getChild( 0 );
 
-				document.enqueueChanges( () => {
-					const batch = document.batch();
+				doc.enqueueChanges( () => {
+					const batch = doc.batch();
 					batch.insert( ModelPosition.createAt( caption ), 'foo bar' );
 				} );
 
 				expect( getViewData( viewDocument ) ).to.equal(
-					'[]<figure class="image ck-widget" contenteditable="false">' +
+					'<p>{}foo</p>' +
+					'<figure class="image ck-widget" contenteditable="false">' +
 						'<img src="img.png"></img>' +
 						'<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
 							'foo bar' +
@@ -178,38 +190,42 @@ describe( 'ImageCaptionEngine', () => {
 			} );
 
 			it( 'should hide when everything is removed from caption', () => {
-				setModelData( document, '<image src="img.png"><caption>foo bar baz</caption></image>' );
-				const image = document.getRoot().getChild( 0 );
+				setModelData( doc, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
+
+				const image = doc.getRoot().getChild( 1 );
 				const caption = image.getChild( 0 );
 
-				document.enqueueChanges( () => {
-					const batch = document.batch();
+				doc.enqueueChanges( () => {
+					const batch = doc.batch();
 					batch.remove( ModelRange.createIn( caption ) );
 				} );
 
 				expect( getViewData( viewDocument ) ).to.equal(
-					'[]<figure class="image ck-widget" contenteditable="false">' +
+					'<p>{}foo</p>' +
+					'<figure class="image ck-widget" contenteditable="false">' +
 						'<img src="img.png"></img>' +
 						'<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
-									'contenteditable="true" data-placeholder="Enter image caption">' +
+							'contenteditable="true" data-placeholder="Enter image caption">' +
 						'</figcaption>' +
 					'</figure>'
 				);
 			} );
 
 			it( 'should show when not everything is removed from caption', () => {
-				setModelData( document, '<image src="img.png"><caption>foo bar baz</caption></image>' );
-				const image = document.getRoot().getChild( 0 );
+				setModelData( doc, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
+
+				const image = doc.getRoot().getChild( 1 );
 				const caption = image.getChild( 0 );
 
-				document.enqueueChanges( () => {
-					const batch = document.batch();
+				doc.enqueueChanges( () => {
+					const batch = doc.batch();
 					batch.remove( ModelRange.createFromParentsAndOffsets( caption, 0, caption, 8 ) );
 				} );
 
 				expect( getViewData( viewDocument ) ).to.equal(
-					'[]<figure class="image ck-widget" contenteditable="false">' +
-					'<img src="img.png"></img>' +
+					'<p>{}foo</p>' +
+					'<figure class="image ck-widget" contenteditable="false">' +
+						'<img src="img.png"></img>' +
 						'<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">baz</figcaption>' +
 					'</figure>'
 				);
@@ -217,89 +233,93 @@ describe( 'ImageCaptionEngine', () => {
 		} );
 	} );
 
-	describe( 'inserting image to document', () => {
+	describe( 'inserting image to the document', () => {
 		it( 'should add caption element if image does not have it', () => {
 			const image = new ModelElement( 'image', { src: '', alt: '' } );
-			const batch = document.batch();
+			const batch = doc.batch();
 
-			document.enqueueChanges( () => {
-				batch.insert( new ModelPosition( document.getRoot(), [ 0 ] ), image );
+			doc.enqueueChanges( () => {
+				batch.insert( new ModelPosition( doc.getRoot(), [ 0 ] ), image );
 			} );
 
-			expect( getModelData( document ) ).to.equal(
-				'[]<image alt="" src=""><caption></caption></image>'
+			expect( getModelData( doc ) ).to.equal(
+				'[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
 			);
 
 			expect( getViewData( viewDocument ) ).to.equal(
-				'[]<figure class="image ck-widget" contenteditable="false">' +
+				'[<figure class="image ck-widget" contenteditable="false">' +
 					'<img alt="" src=""></img>' +
-					'<figcaption class="ck-placeholder ck-editable ck-hidden" ' +
-								'contenteditable="true" data-placeholder="Enter image caption">' +
+					'<figcaption class="ck-placeholder ck-editable" ' +
+						'contenteditable="true" data-placeholder="Enter image caption">' +
 					'</figcaption>' +
-				'</figure>'
+				'</figure>]' +
+				'<p></p>'
 			);
 		} );
 
 		it( 'should not add caption element if image already have it', () => {
 			const caption = new ModelElement( 'caption', null, 'foo bar' );
 			const image = new ModelElement( 'image', { src: '', alt: '' }, caption );
-			const batch = document.batch();
+			const batch = doc.batch();
 
-			document.enqueueChanges( () => {
-				batch.insert( new ModelPosition( document.getRoot(), [ 0 ] ), image );
+			doc.enqueueChanges( () => {
+				batch.insert( new ModelPosition( doc.getRoot(), [ 0 ] ), image );
 			} );
 
-			expect( getModelData( document ) ).to.equal(
-				'[]<image alt="" src=""><caption>foo bar</caption></image>'
+			expect( getModelData( doc ) ).to.equal(
+				'[<image alt="" src=""><caption>foo bar</caption></image>]<paragraph></paragraph>'
 			);
 
 			expect( getViewData( viewDocument ) ).to.equal(
-				'[]<figure class="image ck-widget" contenteditable="false">' +
+				'[<figure class="image ck-widget" contenteditable="false">' +
 					'<img alt="" src=""></img>' +
 					'<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
 						'foo bar' +
 					'</figcaption>' +
-				'</figure>'
+				'</figure>]' +
+				'<p></p>'
 			);
 		} );
 
 		it( 'should not add caption element twice', () => {
 			const image = new ModelElement( 'image', { src: '', alt: '' } );
 			const caption = new ModelElement( 'caption' );
-			const batch = document.batch();
+			const batch = doc.batch();
 
-			document.enqueueChanges( () => {
+			doc.enqueueChanges( () => {
 				batch
 					// Since we are adding an empty image, this should trigger caption fixer.
-					.insert( ModelPosition.createAt( document.getRoot() ), image )
+					.insert( ModelPosition.createAt( doc.getRoot() ), image )
 					// Add caption just after the image is inserted, in same batch and enqueue changes block.
 					.insert( ModelPosition.createAt( image ), caption );
 			} );
 
 			// Check whether caption fixer added redundant caption.
-			expect( getModelData( document ) ).to.equal(
-				'[]<image alt="" src=""><caption></caption></image>'
+			expect( getModelData( doc ) ).to.equal(
+				'[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
 			);
 
 			expect( getViewData( viewDocument ) ).to.equal(
-				'[]<figure class="image ck-widget" contenteditable="false">' +
-				'<img alt="" src=""></img>' +
-				'<figcaption class="ck-placeholder ck-editable ck-hidden" ' +
-							'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
-				'</figure>'
+				'[<figure class="image ck-widget" contenteditable="false">' +
+					'<img alt="" src=""></img>' +
+					'<figcaption class="ck-placeholder ck-editable" ' +
+						'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
+				'</figure>]' +
+				'<p></p>'
 			);
 		} );
 
 		it( 'should do nothing for other changes than insert', () => {
-			setModelData( document, '<image src=""><caption>foo bar</caption></image>' );
-			const image = document.getRoot().getChild( 0 );
-			const batch = document.batch();
+			setModelData( doc, '<image src=""><caption>foo bar</caption></image>' );
 
-			document.enqueueChanges( () => {
+			const image = doc.getRoot().getChild( 0 );
+			const batch = doc.batch();
+
+			doc.enqueueChanges( () => {
 				batch.setAttribute( image, 'alt', 'alt text' );
 			} );
 
-			expect( getModelData( document, { withoutSelection: true } ) ).to.equal(
+			expect( getModelData( doc, { withoutSelection: true } ) ).to.equal(
 				'<image alt="alt text" src=""><caption>foo bar</caption></image>'
 			);
 		} );
@@ -307,9 +327,10 @@ describe( 'ImageCaptionEngine', () => {
 
 	describe( 'editing view', () => {
 		it( 'image should have empty figcaption element when is selected', () => {
-			setModelData( document, '[<image src=""><caption></caption></image>]' );
+			setModelData( doc, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
 
 			expect( getViewData( viewDocument ) ).to.equal(
+				'<p>foo</p>' +
 				'[<figure class="image ck-widget" contenteditable="false">' +
 					'<img src=""></img>' +
 					'<figcaption class="ck-placeholder ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
@@ -319,22 +340,24 @@ describe( 'ImageCaptionEngine', () => {
 		} );
 
 		it( 'image should have empty figcaption element with hidden class when not selected', () => {
-			setModelData( document, '[]<image src=""><caption></caption></image>' );
+			setModelData( doc, '<paragraph>[]foo</paragraph><image src=""><caption></caption></image>' );
 
 			expect( getViewData( viewDocument ) ).to.equal(
-				'[]<figure class="image ck-widget" contenteditable="false">' +
+				'<p>{}foo</p>' +
+				'<figure class="image ck-widget" contenteditable="false">' +
 					'<img src=""></img>' +
 					'<figcaption class="ck-placeholder ck-editable ck-hidden" ' +
-								'contenteditable="true" data-placeholder="Enter image caption">' +
+						'contenteditable="true" data-placeholder="Enter image caption">' +
 					'</figcaption>' +
 				'</figure>'
 			);
 		} );
 
 		it( 'should not add additional figcaption if one is already present', () => {
-			setModelData( document, '[<image src=""><caption>foo bar</caption></image>]' );
+			setModelData( doc, '<paragraph>foo</paragraph>[<image src=""><caption>foo bar</caption></image>]' );
 
 			expect( getViewData( viewDocument ) ).to.equal(
+				'<p>foo</p>' +
 				'[<figure class="image ck-widget" contenteditable="false">' +
 					'<img src=""></img>' +
 					'<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
@@ -343,27 +366,28 @@ describe( 'ImageCaptionEngine', () => {
 		} );
 
 		it( 'should add hidden class to figcaption when caption is empty and image is no longer selected', () => {
-			setModelData( document, '[<image src=""><caption></caption></image>]' );
+			setModelData( doc, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
 
-			document.enqueueChanges( () => {
-				document.selection.removeAllRanges();
+			doc.enqueueChanges( () => {
+				doc.selection.removeAllRanges();
 			} );
 
 			expect( getViewData( viewDocument ) ).to.equal(
-				'[]<figure class="image ck-widget" contenteditable="false">' +
+				'<p>{}foo</p>' +
+				'<figure class="image ck-widget" contenteditable="false">' +
 					'<img src=""></img>' +
 					'<figcaption class="ck-placeholder ck-editable ck-hidden" ' +
-								'contenteditable="true" data-placeholder="Enter image caption">' +
+						'contenteditable="true" data-placeholder="Enter image caption">' +
 					'</figcaption>' +
 				'</figure>'
 			);
 		} );
 
 		it( 'should not remove figcaption when selection is inside it even when it is empty', () => {
-			setModelData( document, '<image src=""><caption>[foo bar]</caption></image>' );
+			setModelData( doc, '<image src=""><caption>[foo bar]</caption></image>' );
 
-			document.enqueueChanges( () => {
-				document.batch().remove( document.selection.getFirstRange() );
+			doc.enqueueChanges( () => {
+				doc.batch().remove( doc.selection.getFirstRange() );
 			} );
 
 			expect( getViewData( viewDocument ) ).to.equal(
@@ -377,29 +401,29 @@ describe( 'ImageCaptionEngine', () => {
 		} );
 
 		it( 'should not remove figcaption when selection is moved from it to its image', () => {
-			setModelData( document, '<image src=""><caption>[foo bar]</caption></image>' );
-			const image = document.getRoot().getChild( 0 );
+			setModelData( doc, '<image src=""><caption>[foo bar]</caption></image>' );
+			const image = doc.getRoot().getChild( 0 );
 
-			document.enqueueChanges( () => {
-				document.batch().remove( document.selection.getFirstRange() );
-				document.selection.setRanges( [ ModelRange.createOn( image ) ] );
+			doc.enqueueChanges( () => {
+				doc.batch().remove( doc.selection.getFirstRange() );
+				doc.selection.setRanges( [ ModelRange.createOn( image ) ] );
 			} );
 
 			expect( getViewData( viewDocument ) ).to.equal(
 				'[<figure class="image ck-widget" contenteditable="false">' +
 					'<img src=""></img>' +
 					'<figcaption class="ck-editable ck-placeholder" ' +
-								'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
+						'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
 				'</figure>]'
 			);
 		} );
 
 		it( 'should not remove figcaption when selection is moved from it to other image', () => {
-			setModelData( document, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
-			const image = document.getRoot().getChild( 1 );
+			setModelData( doc, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
+			const image = doc.getRoot().getChild( 1 );
 
-			document.enqueueChanges( () => {
-				document.selection.setRanges( [ ModelRange.createOn( image ) ] );
+			doc.enqueueChanges( () => {
+				doc.selection.setRanges( [ ModelRange.createOn( image ) ] );
 			} );
 
 			expect( getViewData( viewDocument ) ).to.equal(
@@ -410,33 +434,34 @@ describe( 'ImageCaptionEngine', () => {
 				'[<figure class="image ck-widget" contenteditable="false">' +
 					'<img src=""></img>' +
 					'<figcaption class="ck-placeholder ck-editable" ' +
-								'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
+						'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
 				'</figure>]'
 			);
 		} );
 
 		describe( 'undo/redo integration', () => {
 			it( 'should create view element after redo', () => {
-				setModelData( document, '<image src=""><caption>[foo bar baz]</caption></image>' );
+				setModelData( doc, '<paragraph>foo</paragraph><image src=""><caption>[foo bar baz]</caption></image>' );
 
-				const modelRoot = document.getRoot();
-				const modelImage = modelRoot.getChild( 0 );
+				const modelRoot = doc.getRoot();
+				const modelImage = modelRoot.getChild( 1 );
 				const modelCaption = modelImage.getChild( 0 );
 
 				// Remove text and selection from caption.
-				document.enqueueChanges( () => {
-					const batch = document.batch();
+				doc.enqueueChanges( () => {
+					const batch = doc.batch();
 
 					batch.remove( ModelRange.createIn( modelCaption ) );
-					document.selection.removeAllRanges();
+					doc.selection.removeAllRanges();
 				} );
 
 				// Check if there is no figcaption in the view.
 				expect( getViewData( viewDocument ) ).to.equal(
-					'[]<figure class="image ck-widget" contenteditable="false">' +
+					'<p>{}foo</p>' +
+					'<figure class="image ck-widget" contenteditable="false">' +
 						'<img src=""></img>' +
 						'<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
-									'contenteditable="true" data-placeholder="Enter image caption">' +
+							'contenteditable="true" data-placeholder="Enter image caption">' +
 						'</figcaption>' +
 					'</figure>'
 				);
@@ -445,6 +470,7 @@ describe( 'ImageCaptionEngine', () => {
 
 				// Check if figcaption is back with contents.
 				expect( getViewData( viewDocument ) ).to.equal(
+					'<p>foo</p>' +
 					'<figure class="image ck-widget" contenteditable="false">' +
 						'<img src=""></img>' +
 						'<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
@@ -456,20 +482,21 @@ describe( 'ImageCaptionEngine', () => {
 
 			it( 'undo should work after inserting the image', () => {
 				const image = new ModelElement( 'image' );
+				image.setAttribute( 'src', '/foo.png' );
 
-				setModelData( document, '[]' );
+				setModelData( doc, '<paragraph>foo[]</paragraph>' );
 
-				document.enqueueChanges( () => {
-					const batch = document.batch();
+				doc.enqueueChanges( () => {
+					const batch = doc.batch();
 
-					batch.insert( document.selection.anchor, image );
+					batch.insert( ModelPosition.createAt( doc.getRoot() ), image );
 				} );
 
+				expect( getModelData( doc ) ).to.equal( '<image src="/foo.png"><caption></caption></image><paragraph>foo[]</paragraph>' );
+
 				editor.execute( 'undo' );
 
-				expect( getModelData( document ) ).to.equal(
-					'[]'
-				);
+				expect( getModelData( doc ) ).to.equal( '<paragraph>foo[]</paragraph>' );
 			} );
 		} );
 	} );