8
0
Просмотр исходного кода

Added tests to image caption util function isInsideCaption.

Szymon Kupś 9 лет назад
Родитель
Сommit
8476a6984c

+ 7 - 1
packages/ckeditor5-image/src/imagecaption/utils.js

@@ -62,6 +62,12 @@ export function getCaptionFromImage( imageModelElement ) {
 	return null;
 }
 
+/**
+ * Returns `true` if provided `node` is placed inside image's caption.
+ *
+ * @param {module:engine/model/node~Node} node
+ * @return {Boolean}
+ */
 export function isInsideCaption( node ) {
-	return node.parent && node.parent.name == 'caption' && node.parent.parent && node.parent.parent.name == 'image';
+	return !!( node.parent && node.parent.name == 'caption' && node.parent.parent && node.parent.parent.name == 'image' );
 }

+ 39 - 1
packages/ckeditor5-image/tests/imagecaption/utils.js

@@ -5,7 +5,7 @@
 
 import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
 import ViewEditableElement from '@ckeditor/ckeditor5-engine/src/view/editableelement';
-import { captionElementCreator, isCaption, getCaptionFromImage } from '../../src/imagecaption/utils';
+import { captionElementCreator, isCaption, getCaptionFromImage, isInsideCaption } from '../../src/imagecaption/utils';
 import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
 
 describe( 'image captioning utils', () => {
@@ -65,4 +65,42 @@ describe( 'image captioning utils', () => {
 			expect( getCaptionFromImage( image ) ).to.be.null;
 		} );
 	} );
+
+	describe( 'isInsideCaption', () => {
+		it( 'should return false if node has no parent', () => {
+			const el = new ModelElement( 'test' );
+
+			expect( isInsideCaption( el ) ).to.be.false;
+		} );
+
+		it( 'should return false if node\'s parent is not caption', () => {
+			const el = new ModelElement( 'test' );
+			new ModelElement( 'test', null, el );
+
+			expect( isInsideCaption( el ) ).to.be.false;
+		} );
+
+		it( 'should return false if parent`s parent node is not defined', () => {
+			const el = new ModelElement( 'test' );
+			new ModelElement( 'caption', null, el );
+
+			expect( isInsideCaption( el ) ).to.be.false;
+		} );
+
+		it( 'should return false if parent\'s parent node is not an image', () => {
+			const el = new ModelElement( 'test' );
+			const parent = new ModelElement( 'caption', null, el );
+			new ModelElement( 'not-image', null, parent );
+
+			expect( isInsideCaption( el ) ).to.be.false;
+		} );
+
+		it( 'should return true if node is placed inside image\'s caption', () => {
+			const el = new ModelElement( 'test' );
+			const parent = new ModelElement( 'caption', null, el );
+			new ModelElement( 'image', null, parent );
+
+			expect( isInsideCaption( el ) ).to.be.true;
+		} );
+	} );
 } );