8
0
Aleksander Nowodzinski 9 лет назад
Родитель
Сommit
095a172d9d

+ 1 - 3
packages/ckeditor5-utils/src/dom/getpositionedancestor.js

@@ -9,8 +9,6 @@
 
 import global from './global.js';
 
-const window = global.window;
-
 /**
  * For a given element, returns the nearest ancestor element which CSS position is not "static".
  *
@@ -19,7 +17,7 @@ const window = global.window;
  */
 export default function getPositionedAncestor( element ) {
 	while ( element && element.tagName.toLowerCase() != 'html' ) {
-		if ( window.getComputedStyle( element ).position != 'static' ) {
+		if ( global.window.getComputedStyle( element ).position != 'static' ) {
 			return element;
 		}
 

+ 6 - 4
packages/ckeditor5-utils/src/dom/global.js

@@ -12,13 +12,15 @@
 /**
  * A helper (module) giving an access to the global DOM objects such as `window` and
  * `document`. Accessing these objects using this helper allows easy and bulletproof
- * testing, i.e. stubbing native properties
+ * testing, i.e. stubbing native properties:
  *
  *		import global from 'ckeditor5/utils/dom/global.js';
  *
  *		const window = global.window;
  *
- * 		// This stub will work for any code using global module in tests.
- *		testUtils.sinon.stub( window, 'innerWidth', 10000 );
+ *		// This stub will work for any code using global module.
+ *		testUtils.sinon.stub( global, 'window', {
+ *			innerWidth: 10000
+ *		} );
  */
-export default { document, window };
+export default { window, document };

+ 5 - 5
packages/ckeditor5-utils/src/dom/position.js

@@ -7,12 +7,10 @@
  * @module utils/dom/position
  */
 
-import Rect from './rect.js';
 import global from './global.js';
+import Rect from './rect.js';
 import getPositionedAncestor from './getpositionedancestor.js';
 
-const window = global.window;
-
 /**
  * Calculates the `position: absolute` coordinates of a given element so it can be positioned with respect to the
  * target in the visually most efficient way, taking various restrictions like viewport or limiter geometry
@@ -216,9 +214,11 @@ function getBestPosition( positions, targetRect, elementRect, limiterRect, viewp
 // @param {utils/dom/rect~Rect} rect A rect to be converted.
 // @returns {Object} Object containing `left` and `top` properties, in absolute coordinates.
 function getAbsoluteRectCoordinates( { left, top } ) {
+	const { scrollX, scrollY } = global.window;
+
 	return {
-		left: left + window.scrollX,
-		top: top + window.scrollY,
+		left: left + scrollX,
+		top: top + scrollY,
 	};
 }
 

+ 1 - 2
packages/ckeditor5-utils/src/dom/rect.js

@@ -11,7 +11,6 @@ import global from './global.js';
 import isRange from './isrange.js';
 import isElement from '../../utils/lib/lodash/isElement.js';
 
-const window = global.window;
 const rectProperties = [ 'top', 'right', 'bottom', 'left', 'width', 'height' ];
 
 /**
@@ -186,7 +185,7 @@ export default class Rect {
 	 * @returns {module:utils/dom/rect~Rect} A viewport rect.
 	 */
 	static getViewportRect() {
-		const { innerWidth, innerHeight } = window;
+		const { innerWidth, innerHeight } = global.window;
 
 		return new Rect( {
 			top: 0,

+ 23 - 0
packages/ckeditor5-utils/tests/dom/global.js

@@ -6,6 +6,9 @@
 /* global window, document */
 
 import global from 'ckeditor5/utils/dom/global.js';
+import testUtils from 'tests/core/_utils/utils.js';
+
+testUtils.createSinonSandbox();
 
 describe( 'global', () => {
 	describe( 'global', () => {
@@ -13,12 +16,32 @@ describe( 'global', () => {
 			it( 'equals native DOM window', () => {
 				expect( global.window ).to.equal( window );
 			} );
+
+			it( 'stubs', () => {
+				testUtils.sinon.stub( global, 'window', {
+					scrollX: 100
+				} );
+
+				expect( global.window ).to.deep.equal( {
+					scrollX: 100
+				} );
+			} );
 		} );
 
 		describe( 'document', () => {
 			it( 'equals native DOM document', () => {
 				expect( global.document ).to.equal( document );
 			} );
+
+			it( 'stubs', () => {
+				testUtils.sinon.stub( global, 'document', {
+					foo: 'abc'
+				} );
+
+				expect( global.document ).to.deep.equal( {
+					foo: 'abc'
+				} );
+			} );
 		} );
 	} );
 } );

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

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md.
  */
 
-/* global document */
+/* global document, window */
 
 import global from 'ckeditor5/utils/dom/global.js';
 import { getOptimalPosition } from 'ckeditor5/utils/dom/position.js';
@@ -11,17 +11,18 @@ import testUtils from 'tests/core/_utils/utils.js';
 
 testUtils.createSinonSandbox();
 
-const window = global.window;
-let element, target, limiter;
+let element, target, limiter, windowStub;
 
 describe( 'getOptimalPosition', () => {
 	beforeEach( () => {
-		// Give us a lot of space.
-		testUtils.sinon.stub( window, 'innerWidth', 10000 );
-		testUtils.sinon.stub( window, 'innerHeight', 10000 );
-
-		testUtils.sinon.stub( window, 'scrollX', 0 );
-		testUtils.sinon.stub( window, 'scrollY', 0 );
+		windowStub = {
+			innerWidth: 10000,
+			innerHeight: 10000,
+			scrollX: 0,
+			scrollY: 0
+		};
+
+		testUtils.sinon.stub( global, 'window', windowStub );
 	} );
 
 	describe( 'for single position', () => {
@@ -36,8 +37,12 @@ describe( 'getOptimalPosition', () => {
 		} );
 
 		it( 'should return coordinates (window scroll)', () => {
-			testUtils.sinon.stub( window, 'scrollX', 100 );
-			testUtils.sinon.stub( window, 'scrollY', 100 );
+			Object.assign( windowStub, {
+				innerWidth: 10000,
+				innerHeight: 10000,
+				scrollX: 100,
+				scrollY: 100,
+			} );
 
 			assertPosition( { element, target, positions: [ attachLeft ] }, {
 				top: 200,
@@ -49,8 +54,15 @@ describe( 'getOptimalPosition', () => {
 		it( 'should return coordinates (positioned element parent)', () => {
 			const positionedParent = document.createElement( 'div' );
 
-			testUtils.sinon.stub( window, 'scrollX', 1000 );
-			testUtils.sinon.stub( window, 'scrollY', 1000 );
+			Object.assign( windowStub, {
+				innerWidth: 10000,
+				innerHeight: 10000,
+				scrollX: 1000,
+				scrollY: 1000,
+				getComputedStyle: ( el ) => {
+					return window.getComputedStyle( el );
+				}
+			} );
 
 			Object.assign( positionedParent.style, {
 				position: 'absolute',

+ 6 - 7
packages/ckeditor5-utils/tests/dom/rect.js

@@ -9,8 +9,6 @@ import global from 'ckeditor5/utils/dom/global.js';
 import Rect from 'ckeditor5/utils/dom/rect.js';
 import testUtils from 'tests/core/_utils/utils.js';
 
-const window = global.window;
-
 testUtils.createSinonSandbox();
 
 describe( 'Rect', () => {
@@ -328,11 +326,12 @@ describe( 'Rect', () => {
 		} );
 
 		it( 'should return the viewport\'s rect', () => {
-			testUtils.sinon.stub( window, 'innerWidth', 1000 );
-			testUtils.sinon.stub( window, 'innerHeight', 500 );
-
-			testUtils.sinon.stub( window, 'scrollX', 100 );
-			testUtils.sinon.stub( window, 'scrollY', 200 );
+			testUtils.sinon.stub( global, 'window', {
+				innerWidth: 1000,
+				innerHeight: 500,
+				scrollX: 100,
+				scrollY: 200
+			} );
 
 			assertRect( Rect.getViewportRect(), {
 				top: 0,