Explorar o código

Tests: Updated getOptimalPosition DOM helper's tests so they don't rely on global#window.

Aleksander Nowodzinski %!s(int64=8) %!d(string=hai) anos
pai
achega
3c0f93d945

+ 3 - 2
packages/ckeditor5-utils/src/dom/position.js

@@ -11,6 +11,7 @@ import global from './global';
 import Rect from './rect';
 import Rect from './rect';
 import getPositionedAncestor from './getpositionedancestor';
 import getPositionedAncestor from './getpositionedancestor';
 import getBorderWidths from './getborderwidths';
 import getBorderWidths from './getborderwidths';
+import isFunction from '../lib/lodash/isFunction';
 
 
 /**
 /**
  * Calculates the `position: absolute` coordinates of a given element so it can be positioned with respect to the
  * Calculates the `position: absolute` coordinates of a given element so it can be positioned with respect to the
@@ -79,13 +80,13 @@ import getBorderWidths from './getborderwidths';
 export function getOptimalPosition( { element, target, positions, limiter, fitInViewport } ) {
 export function getOptimalPosition( { element, target, positions, limiter, fitInViewport } ) {
 	// If the {@link module:utils/dom/position~Options#target} is a function, use what it returns.
 	// If the {@link module:utils/dom/position~Options#target} is a function, use what it returns.
 	// https://github.com/ckeditor/ckeditor5-utils/issues/157
 	// https://github.com/ckeditor/ckeditor5-utils/issues/157
-	if ( typeof target == 'function' ) {
+	if ( isFunction( target ) ) {
 		target = target();
 		target = target();
 	}
 	}
 
 
 	// If the {@link module:utils/dom/position~Options#limiter} is a function, use what it returns.
 	// If the {@link module:utils/dom/position~Options#limiter} is a function, use what it returns.
 	// https://github.com/ckeditor/ckeditor5-ui/issues/260
 	// https://github.com/ckeditor/ckeditor5-ui/issues/260
-	if ( typeof limiter == 'function' ) {
+	if ( isFunction( limiter ) ) {
 		limiter = limiter();
 		limiter = limiter();
 	}
 	}
 
 

+ 13 - 6
packages/ckeditor5-utils/tests/dom/position.js

@@ -3,9 +3,13 @@
  * For licensing, see LICENSE.md.
  * For licensing, see LICENSE.md.
  */
  */
 
 
-import global from '../../src/dom/global';
+/* global document, window */
+
 import { getOptimalPosition } from '../../src/dom/position';
 import { getOptimalPosition } from '../../src/dom/position';
 import Rect from '../../src/dom/rect';
 import Rect from '../../src/dom/rect';
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+
+testUtils.createSinonSandbox();
 
 
 let element, target, limiter;
 let element, target, limiter;
 
 
@@ -51,6 +55,8 @@ const attachTop = ( targetRect, elementRect ) => ( {
 
 
 describe( 'getOptimalPosition()', () => {
 describe( 'getOptimalPosition()', () => {
 	beforeEach( () => {
 	beforeEach( () => {
+		testUtils.sinon.stub( window, 'getComputedStyle' );
+
 		stubWindow( {
 		stubWindow( {
 			innerWidth: 10000,
 			innerWidth: 10000,
 			innerHeight: 10000,
 			innerHeight: 10000,
@@ -422,7 +428,8 @@ function getElement( properties = {}, styles = {} ) {
 	const element = {
 	const element = {
 		tagName: 'div',
 		tagName: 'div',
 		scrollLeft: 0,
 		scrollLeft: 0,
-		scrollTop: 0
+		scrollTop: 0,
+		ownerDocument: document
 	};
 	};
 
 
 	Object.assign( element, properties );
 	Object.assign( element, properties );
@@ -435,7 +442,7 @@ function getElement( properties = {}, styles = {} ) {
 		styles.borderTopWidth = '0px';
 		styles.borderTopWidth = '0px';
 	}
 	}
 
 
-	global.window.getComputedStyle.withArgs( element ).returns( styles );
+	window.getComputedStyle.withArgs( element ).returns( styles );
 
 
 	return element;
 	return element;
 }
 }
@@ -445,9 +452,9 @@ function getElement( properties = {}, styles = {} ) {
 // @private
 // @private
 // @param {Object} properties A set of properties the window should have.
 // @param {Object} properties A set of properties the window should have.
 function stubWindow( properties ) {
 function stubWindow( properties ) {
-	global.window = Object.assign( {
-		getComputedStyle: sinon.stub()
-	}, properties );
+	for ( const p in properties ) {
+		testUtils.sinon.stub( window, p ).value( properties[ p ] );
+	}
 }
 }
 
 
 //        <-- 100px ->
 //        <-- 100px ->