Selaa lähdekoodia

Improvements in SelectionObserver and FocusObserver tests.

Szymon Kupś 8 vuotta sitten
vanhempi
commit
5a029a3182

+ 8 - 7
packages/ckeditor5-engine/tests/view/observer/focusobserver.js

@@ -9,6 +9,7 @@ import ViewRange from '../../../src/view/range';
 import global from '@ckeditor/ckeditor5-utils/src/dom/global';
 
 const setTimeout = global.window.setTimeout;
+const domDocument = global.document;
 
 describe( 'FocusObserver', () => {
 	let viewDocument, observer;
@@ -36,12 +37,12 @@ describe( 'FocusObserver', () => {
 
 			viewDocument.on( 'focus', spy );
 
-			observer.onDomEvent( { type: 'focus', target: global.document.body } );
+			observer.onDomEvent( { type: 'focus', target: domDocument.body } );
 
 			expect( spy.calledOnce ).to.be.true;
 
 			const data = spy.args[ 0 ][ 1 ];
-			expect( data.domTarget ).to.equal( global.document.body );
+			expect( data.domTarget ).to.equal( domDocument.body );
 		} );
 
 		it( 'should fire blur with the right event data', () => {
@@ -49,18 +50,18 @@ describe( 'FocusObserver', () => {
 
 			viewDocument.on( 'blur', spy );
 
-			observer.onDomEvent( { type: 'blur', target: global.document.body } );
+			observer.onDomEvent( { type: 'blur', target: domDocument.body } );
 
 			expect( spy.calledOnce ).to.be.true;
 
 			const data = spy.args[ 0 ][ 1 ];
-			expect( data.domTarget ).to.equal( global.document.body );
+			expect( data.domTarget ).to.equal( domDocument.body );
 		} );
 
 		it( 'should render document after blurring', () => {
 			const renderSpy = sinon.spy( viewDocument, 'render' );
 
-			observer.onDomEvent( { type: 'blur', target: global.document.body } );
+			observer.onDomEvent( { type: 'blur', target: domDocument.body } );
 
 			sinon.assert.calledOnce( renderSpy );
 		} );
@@ -70,8 +71,8 @@ describe( 'FocusObserver', () => {
 		let domMain, domHeader, viewMain, viewHeader;
 
 		beforeEach( () => {
-			domMain = global.document.createElement( 'div' );
-			domHeader = global.document.createElement( 'h1' );
+			domMain = domDocument.createElement( 'div' );
+			domHeader = domDocument.createElement( 'h1' );
 
 			viewMain = viewDocument.createRoot( domMain );
 			viewHeader = viewDocument.createRoot( domHeader, 'header' );

+ 15 - 11
packages/ckeditor5-engine/tests/view/observer/selectionobserver.js

@@ -3,8 +3,6 @@
  * For licensing, see LICENSE.md.
  */
 
-/* globals setTimeout, document */
-
 import ViewRange from '../../../src/view/range';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import ViewSelection from '../../../src/view/selection';
@@ -12,21 +10,25 @@ import ViewDocument from '../../../src/view/document';
 import SelectionObserver from '../../../src/view/observer/selectionobserver';
 import MutationObserver from '../../../src/view/observer/mutationobserver';
 import FocusObserver from '../../../src/view/observer/focusobserver';
-
 import log from '@ckeditor/ckeditor5-utils/src/log';
-
+import global from '@ckeditor/ckeditor5-utils/src/dom/global';
 import { parse } from '../../../src/dev-utils/view';
 
+const setTimeout = global.window.setTimeout;
+const setInterval = global.window.setInterval;
+const clearInterval = global.window.clearInterval;
+const domDocument = global.document;
+
 testUtils.createSinonSandbox();
 
 describe( 'SelectionObserver', () => {
 	let viewDocument, viewRoot, mutationObserver, selectionObserver, domRoot, domMain;
 
 	beforeEach( ( done ) => {
-		domRoot = document.createElement( 'div' );
+		domRoot = domDocument.createElement( 'div' );
 		domRoot.innerHTML = `<div contenteditable="true"></div><div contenteditable="true" id="additional"></div>`;
 		domMain = domRoot.childNodes[ 0 ];
-		document.body.appendChild( domRoot );
+		domDocument.body.appendChild( domRoot );
 
 		viewDocument = new ViewDocument();
 		viewDocument.createRoot( domMain );
@@ -43,7 +45,7 @@ describe( 'SelectionObserver', () => {
 		viewDocument.render();
 
 		viewDocument.selection.removeAllRanges();
-		document.getSelection().removeAllRanges();
+		domDocument.getSelection().removeAllRanges();
 
 		viewDocument.isFocused = true;
 
@@ -61,7 +63,7 @@ describe( 'SelectionObserver', () => {
 
 	it( 'should fire selectionChange when it is the only change', ( done ) => {
 		viewDocument.on( 'selectionChange', ( evt, data ) => {
-			expect( data ).to.have.property( 'domSelection' ).that.equals( document.getSelection() );
+			expect( data ).to.have.property( 'domSelection' ).that.equals( domDocument.getSelection() );
 
 			expect( data ).to.have.property( 'oldSelection' ).that.is.instanceof( ViewSelection );
 			expect( data.oldSelection.rangeCount ).to.equal( 0 );
@@ -85,7 +87,7 @@ describe( 'SelectionObserver', () => {
 
 	it( 'should add only one listener to one document', ( done ) => {
 		// Add second roots to ensure that listener is added once.
-		viewDocument.createRoot( document.getElementById( 'additional' ), 'additional' );
+		viewDocument.createRoot( domDocument.getElementById( 'additional' ), 'additional' );
 
 		viewDocument.on( 'selectionChange', () => {
 			done();
@@ -140,6 +142,8 @@ describe( 'SelectionObserver', () => {
 	it( 'should warn and not enter infinite loop', ( done ) => {
 		// Reset infinite loop counters so other tests won't mess up with this test.
 		selectionObserver._clearInfiniteLoop();
+		clearInterval( selectionObserver._clearInfiniteLoopInterval );
+		selectionObserver._clearInfiniteLoopInterval = setInterval( () => selectionObserver._clearInfiniteLoop(), 2000 );
 
 		let counter = 100;
 
@@ -255,7 +259,7 @@ describe( 'SelectionObserver', () => {
 				const data = spy.firstCall.args[ 1 ];
 
 				expect( spy.calledOnce ).to.true;
-				expect( data ).to.have.property( 'domSelection' ).to.equal( document.getSelection() );
+				expect( data ).to.have.property( 'domSelection' ).to.equal( domDocument.getSelection() );
 
 				expect( data ).to.have.property( 'oldSelection' ).to.instanceof( ViewSelection );
 				expect( data.oldSelection.rangeCount ).to.equal( 0 );
@@ -302,7 +306,7 @@ describe( 'SelectionObserver', () => {
 	} );
 
 	function changeDomSelection() {
-		const domSelection = document.getSelection();
+		const domSelection = domDocument.getSelection();
 		const domFoo = domMain.childNodes[ 0 ].childNodes[ 0 ];
 		const offset = domSelection.anchorOffset;