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

Removed timeouts from selectionobserver tests.

Maciej Bukowski 7 лет назад
Родитель
Сommit
5b03d1808f
1 измененных файлов с 31 добавлено и 40 удалено
  1. 31 40
      packages/ckeditor5-engine/tests/view/observer/selectionobserver.js

+ 31 - 40
packages/ckeditor5-engine/tests/view/observer/selectionobserver.js

@@ -238,8 +238,9 @@ describe( 'SelectionObserver', () => {
 		}
 	} );
 
-	it( 'should fire `selectionChangeDone` event after selection stop changing', done => {
+	it( 'should fire `selectionChangeDone` event after selection stop changing', () => {
 		const spy = sinon.spy();
+		const clock = testUtils.sinon.useFakeTimers();
 
 		viewDocument.on( 'selectionChangeDone', spy );
 
@@ -250,43 +251,39 @@ describe( 'SelectionObserver', () => {
 		changeDomSelection();
 
 		// Wait 100ms.
-		// Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
-		// See: https://github.com/lodash/lodash/issues/304
-		setTimeout( () => {
-			// Check if spy was called.
-			expect( spy.notCalled ).to.true;
+		clock.tick( 100 );
+		// Check if spy was called.
+		sinon.assert.notCalled( spy );
 
-			// Change selection one more time.
-			changeDomSelection();
+		// Change selection one more time.
+		changeDomSelection();
 
-			// Wait 210ms (debounced function should be called).
-			setTimeout( () => {
-				const data = spy.firstCall.args[ 1 ];
+		// Wait 210ms (debounced function should be called).
+		clock.tick( 210 );
 
-				expect( spy.calledOnce ).to.true;
-				expect( data ).to.have.property( 'domSelection' ).to.equal( domDocument.getSelection() );
+		const data = spy.firstCall.args[ 1 ];
 
-				expect( data ).to.have.property( 'oldSelection' ).to.instanceof( DocumentSelection );
-				expect( data.oldSelection.rangeCount ).to.equal( 0 );
+		sinon.assert.calledOnce( spy );
+		expect( data ).to.have.property( 'domSelection' ).to.equal( domDocument.getSelection() );
 
-				expect( data ).to.have.property( 'newSelection' ).to.instanceof( ViewSelection );
-				expect( data.newSelection.rangeCount ).to.equal( 1 );
+		expect( data ).to.have.property( 'oldSelection' ).to.instanceof( DocumentSelection );
+		expect( data.oldSelection.rangeCount ).to.equal( 0 );
 
-				const newViewRange = data.newSelection.getFirstRange();
-				const viewFoo = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
+		expect( data ).to.have.property( 'newSelection' ).to.instanceof( ViewSelection );
+		expect( data.newSelection.rangeCount ).to.equal( 1 );
 
-				expect( newViewRange.start.parent ).to.equal( viewFoo );
-				expect( newViewRange.start.offset ).to.equal( 3 );
-				expect( newViewRange.end.parent ).to.equal( viewFoo );
-				expect( newViewRange.end.offset ).to.equal( 3 );
+		const newViewRange = data.newSelection.getFirstRange();
+		const viewFoo = viewDocument.getRoot().getChild( 1 ).getChild( 0 );
 
-				done();
-			}, 210 );
-		}, 100 );
+		expect( newViewRange.start.parent ).to.equal( viewFoo );
+		expect( newViewRange.start.offset ).to.equal( 3 );
+		expect( newViewRange.end.parent ).to.equal( viewFoo );
+		expect( newViewRange.end.offset ).to.equal( 3 );
 	} );
 
-	it( 'should not fire `selectionChangeDone` event when observer will be destroyed', done => {
+	it( 'should not fire `selectionChangeDone` event when observer will be destroyed', () => {
 		const spy = sinon.spy();
+		const clock = testUtils.sinon.useFakeTimers();
 
 		viewDocument.on( 'selectionChangeDone', spy );
 
@@ -294,20 +291,14 @@ describe( 'SelectionObserver', () => {
 		changeDomSelection();
 
 		// Wait 100ms.
-		// Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
-		// See: https://github.com/lodash/lodash/issues/304
-		setTimeout( () => {
-			// And destroy observer.
-			selectionObserver.destroy();
-
-			// Wait another 110ms.
-			setTimeout( () => {
-				// Check that event won't be called.
-				expect( spy.notCalled ).to.true;
+		clock.tick( 100 );
+		// And destroy observer.
+		selectionObserver.destroy();
 
-				done();
-			}, 110 );
-		}, 100 );
+		// Wait another 110ms.
+		clock.tick( 110 );
+		// Check that event won't be called.
+		sinon.assert.notCalled( spy );
 	} );
 
 	it( 'should re-render view if selections are similar if DOM selection is in incorrect place', done => {