浏览代码

Tests: throwing errors when adding non-Ranges to Selection.

Szymon Cofalik 9 年之前
父节点
当前提交
79419c538d

+ 14 - 19
packages/ckeditor5-engine/tests/model/liveselection.js

@@ -15,6 +15,7 @@ import LiveSelection from '/ckeditor5/engine/model/liveselection.js';
 import InsertOperation from '/ckeditor5/engine/model/operation/insertoperation.js';
 import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
 import AttributeOperation from '/ckeditor5/engine/model/operation/attributeoperation.js';
+import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
 import count from '/ckeditor5/utils/count.js';
 import testUtils from '/tests/core/_utils/utils.js';
 import { wrapInDelta } from '/tests/engine/model/_utils/utils.js';
@@ -128,6 +129,12 @@ describe( 'LiveSelection', () => {
 
 			expect( ranges[ 0 ] ).to.be.instanceof( LiveRange );
 		} );
+
+		it( 'should throw an error when range is invalid', () => {
+			expect( () => {
+				selection.addRange( { invalid: 'range' } );
+			} ).to.throw( CKEditorError, /model-selection-added-not-range/ );
+		} );
 	} );
 
 	describe( 'collapse', () => {
@@ -223,35 +230,23 @@ describe( 'LiveSelection', () => {
 	} );
 
 	describe( 'setRanges', () => {
-		let newRanges, spy, oldRanges;
-
-		before( () => {
-			newRanges = [
-				new Range( new Position( root, [ 4 ] ), new Position( root, [ 5 ] ) ),
-				new Range( new Position( root, [ 5, 0 ] ), new Position( root, [ 6, 0 ] ) )
-			];
+		it( 'should throw an error when range is invalid', () => {
+			expect( () => {
+				selection.setRanges( [ { invalid: 'range' } ] );
+			} ).to.throw( CKEditorError, /model-selection-added-not-range/ );
 		} );
 
-		beforeEach( () => {
+		it( 'should detach removed ranges', () => {
 			selection.addRange( liveRange );
 			selection.addRange( range );
 
-			spy = sinon.spy();
-			selection.on( 'change:range', spy );
-
-			oldRanges = Array.from( selection._ranges );
+			const oldRanges = Array.from( selection._ranges );
 
 			sinon.spy( oldRanges[ 0 ], 'detach' );
 			sinon.spy( oldRanges[ 1 ], 'detach' );
-		} );
 
-		afterEach( () => {
-			oldRanges[ 0 ].detach.restore();
-			oldRanges[ 1 ].detach.restore();
-		} );
+			selection.setRanges( [] );
 
-		it( 'should detach removed ranges', () => {
-			selection.setRanges( newRanges );
 			expect( oldRanges[ 0 ].detach.called ).to.be.true;
 			expect( oldRanges[ 1 ].detach.called ).to.be.true;
 		} );

+ 12 - 0
packages/ckeditor5-engine/tests/model/selection.js

@@ -163,6 +163,12 @@ describe( 'Selection', () => {
 			expect( spy.called ).to.be.true;
 		} );
 
+		it( 'should throw an error when range is invalid', () => {
+			expect( () => {
+				selection.addRange( { invalid: 'range' } );
+			} ).to.throw( CKEditorError, /model-selection-added-not-range/ );
+		} );
+
 		it( 'should throw an error if added range intersects with already stored range', () => {
 			selection.addRange( liveRange );
 
@@ -502,6 +508,12 @@ describe( 'Selection', () => {
 			oldRanges = selection._ranges;
 		} );
 
+		it( 'should throw an error when range is invalid', () => {
+			expect( () => {
+				selection.setRanges( [ { invalid: 'range' } ] );
+			} ).to.throw( CKEditorError, /model-selection-added-not-range/ );
+		} );
+
 		it( 'should remove all ranges and add given ranges', () => {
 			selection.setRanges( newRanges );