Ver Fonte

Refactored FocusTracker tests to be browser independent.

Oskar Wróbel há 9 anos atrás
pai
commit
a1a1098858
1 ficheiros alterados com 25 adições e 27 exclusões
  1. 25 27
      packages/ckeditor5-utils/tests/focustracker.js

+ 25 - 27
packages/ckeditor5-utils/tests/focustracker.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md.
  * For licensing, see LICENSE.md.
  */
  */
 
 
-/* global document */
+/* global document, Event */
 
 
 import FocusTracker from '/ckeditor5/utils/focustracker.js';
 import FocusTracker from '/ckeditor5/utils/focustracker.js';
 import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
 import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
@@ -12,29 +12,21 @@ import testUtils from '/tests/core/_utils/utils.js';
 testUtils.createSinonSandbox();
 testUtils.createSinonSandbox();
 
 
 describe( 'FocusTracker', () => {
 describe( 'FocusTracker', () => {
-	let focusTracker, container, containerFirstInput, containerSecondInput, inputOuOfContainer;
+	let focusTracker, container, containerFirstInput, containerSecondInput;
 
 
 	beforeEach( () => {
 	beforeEach( () => {
 		container = document.createElement( 'div' );
 		container = document.createElement( 'div' );
 		containerFirstInput = document.createElement( 'input' );
 		containerFirstInput = document.createElement( 'input' );
 		containerSecondInput = document.createElement( 'input' );
 		containerSecondInput = document.createElement( 'input' );
-		inputOuOfContainer = document.createElement( 'input' );
 
 
 		container.appendChild( containerFirstInput );
 		container.appendChild( containerFirstInput );
 		container.appendChild( containerSecondInput );
 		container.appendChild( containerSecondInput );
-		document.body.appendChild( container );
-		document.body.appendChild( inputOuOfContainer );
 
 
 		testUtils.sinon.useFakeTimers();
 		testUtils.sinon.useFakeTimers();
 
 
 		focusTracker = new FocusTracker();
 		focusTracker = new FocusTracker();
 	} );
 	} );
 
 
-	afterEach( () => {
-		document.body.removeChild( container );
-		document.body.removeChild( inputOuOfContainer );
-	} );
-
 	describe( 'constructor', () => {
 	describe( 'constructor', () => {
 		describe( 'isFocused', () => {
 		describe( 'isFocused', () => {
 			it( 'should be false at default', () => {
 			it( 'should be false at default', () => {
@@ -68,19 +60,16 @@ describe( 'FocusTracker', () => {
 
 
 				expect( focusTracker.isFocused ).to.false;
 				expect( focusTracker.isFocused ).to.false;
 
 
-				containerFirstInput.focus();
+				containerFirstInput.dispatchEvent( new Event( 'focus' ) );
 
 
 				expect( focusTracker.isFocused ).to.true;
 				expect( focusTracker.isFocused ).to.true;
 			} );
 			} );
 
 
 			it( 'should start listening on element blur and update `isFocused` property', () => {
 			it( 'should start listening on element blur and update `isFocused` property', () => {
 				focusTracker.add( containerFirstInput );
 				focusTracker.add( containerFirstInput );
+				focusTracker.isFocused = true;
 
 
-				containerFirstInput.focus();
-
-				expect( focusTracker.isFocused ).to.true;
-
-				containerSecondInput.focus();
+				containerFirstInput.dispatchEvent( new Event( 'blur' ) );
 				testUtils.sinon.clock.tick( 0 );
 				testUtils.sinon.clock.tick( 0 );
 
 
 				expect( focusTracker.isFocused ).to.false;
 				expect( focusTracker.isFocused ).to.false;
@@ -93,19 +82,16 @@ describe( 'FocusTracker', () => {
 
 
 				expect( focusTracker.isFocused ).to.false;
 				expect( focusTracker.isFocused ).to.false;
 
 
-				containerFirstInput.focus();
+				containerFirstInput.dispatchEvent( new Event( 'focus' ) );
 
 
 				expect( focusTracker.isFocused ).to.true;
 				expect( focusTracker.isFocused ).to.true;
 			} );
 			} );
 
 
 			it( 'should start listening on element blur using event capturing and update `isFocused` property', () => {
 			it( 'should start listening on element blur using event capturing and update `isFocused` property', () => {
 				focusTracker.add( container );
 				focusTracker.add( container );
+				focusTracker.isFocused = true;
 
 
-				containerFirstInput.focus();
-
-				expect( focusTracker.isFocused ).to.true;
-
-				inputOuOfContainer.focus();
+				containerFirstInput.dispatchEvent( new Event( 'blur' ) );
 				testUtils.sinon.clock.tick( 0 );
 				testUtils.sinon.clock.tick( 0 );
 
 
 				expect( focusTracker.isFocused ).to.false;
 				expect( focusTracker.isFocused ).to.false;
@@ -116,13 +102,14 @@ describe( 'FocusTracker', () => {
 
 
 				focusTracker.add( container );
 				focusTracker.add( container );
 
 
-				containerFirstInput.focus();
+				containerFirstInput.dispatchEvent( new Event( 'focus' ) );
 
 
 				focusTracker.listenTo( focusTracker, 'change:isFocused', changeSpy );
 				focusTracker.listenTo( focusTracker, 'change:isFocused', changeSpy );
 
 
 				expect( focusTracker.isFocused ).to.true;
 				expect( focusTracker.isFocused ).to.true;
 
 
-				containerSecondInput.focus();
+				containerFirstInput.dispatchEvent( new Event( 'blur' ) );
+				containerSecondInput.dispatchEvent( new Event( 'focus' ) );
 				testUtils.sinon.clock.tick( 0 );
 				testUtils.sinon.clock.tick( 0 );
 
 
 				expect( focusTracker.isFocused ).to.true;
 				expect( focusTracker.isFocused ).to.true;
@@ -138,18 +125,29 @@ describe( 'FocusTracker', () => {
 			} ).to.not.throw();
 			} ).to.not.throw();
 		} );
 		} );
 
 
-		it( 'should stop listening on element focus and update `isFocused` property', () => {
+		it( 'should stop listening on element focus', () => {
 			focusTracker.add( containerFirstInput );
 			focusTracker.add( containerFirstInput );
 			focusTracker.remove( containerFirstInput );
 			focusTracker.remove( containerFirstInput );
 
 
-			containerFirstInput.focus();
+			containerFirstInput.dispatchEvent( new Event( 'focus' ) );
 
 
 			expect( focusTracker.isFocused ).to.false;
 			expect( focusTracker.isFocused ).to.false;
 		} );
 		} );
 
 
+		it( 'should stop listening on element blur', () => {
+			focusTracker.add( containerFirstInput );
+			focusTracker.remove( containerFirstInput );
+			focusTracker.isFocused = true;
+
+			containerFirstInput.dispatchEvent( new Event( 'blur' ) );
+			testUtils.sinon.clock.tick( 0 );
+
+			expect( focusTracker.isFocused ).to.true;
+		} );
+
 		it( 'should blur element before removing when is focused', () => {
 		it( 'should blur element before removing when is focused', () => {
 			focusTracker.add( containerFirstInput );
 			focusTracker.add( containerFirstInput );
-			containerFirstInput.focus();
+			containerFirstInput.dispatchEvent( new Event( 'focus' ) );
 
 
 			expect( focusTracker.isFocused ).to.true;
 			expect( focusTracker.isFocused ).to.true;