Browse Source

Used global module in various modules and tests to bulletproof them with predictable stubs.

Aleksander Nowodzinski 9 years ago
parent
commit
60f4e96c2c

+ 4 - 2
packages/ckeditor5-utils/src/dom/getpositionedancestor.js

@@ -3,12 +3,14 @@
  * For licensing, see LICENSE.md.
  */
 
-/* globals window */
-
 /**
  * @module utils/dom/getpositionedancestor
  */
 
+import global from './global.js';
+
+const window = global.window;
+
 /**
  * For a given element, returns the nearest ancestor element which CSS position is not "static".
  *

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

@@ -3,15 +3,16 @@
  * For licensing, see LICENSE.md.
  */
 
-/* globals window */
-
 /**
  * @module utils/dom/position
  */
 
 import Rect from './rect.js';
+import global from './global.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

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

@@ -3,15 +3,15 @@
  * For licensing, see LICENSE.md.
  */
 
-/* global window */
-
 /**
  * @module utils/dom/rect
  */
 
+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' ];
 
 /**

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

@@ -3,33 +3,25 @@
  * For licensing, see LICENSE.md.
  */
 
-/* global document, window */
+/* global document */
 
+import global from 'ckeditor5/utils/dom/global.js';
 import { getOptimalPosition } from 'ckeditor5/utils/dom/position.js';
-import Rect from 'ckeditor5/utils/dom/rect.js';
 import testUtils from 'tests/core/_utils/utils.js';
 
 testUtils.createSinonSandbox();
 
-let element, target, limiter, revertWindowScroll;
+const window = global.window;
+let element, target, limiter;
 
 describe( 'getOptimalPosition', () => {
 	beforeEach( () => {
 		// Give us a lot of space.
-		testUtils.sinon.stub( Rect, 'getViewportRect' ).returns( new Rect( {
-			top: 0,
-			right: 10000,
-			bottom: 10000,
-			left: 0,
-			width: 10000,
-			height: 10000
-		} ) );
-	} );
+		testUtils.sinon.stub( window, 'innerWidth', 10000 );
+		testUtils.sinon.stub( window, 'innerHeight', 10000 );
 
-	afterEach( () => {
-		if ( revertWindowScroll ) {
-			revertWindowScroll();
-		}
+		testUtils.sinon.stub( window, 'scrollX', 0 );
+		testUtils.sinon.stub( window, 'scrollY', 0 );
 	} );
 
 	describe( 'for single position', () => {
@@ -44,7 +36,8 @@ describe( 'getOptimalPosition', () => {
 		} );
 
 		it( 'should return coordinates (window scroll)', () => {
-			stubWindowScroll( 100, 100 );
+			testUtils.sinon.stub( window, 'scrollX', 100 );
+			testUtils.sinon.stub( window, 'scrollY', 100 );
 
 			assertPosition( { element, target, positions: [ attachLeft ] }, {
 				top: 200,
@@ -55,7 +48,9 @@ describe( 'getOptimalPosition', () => {
 
 		it( 'should return coordinates (positioned element parent)', () => {
 			const positionedParent = document.createElement( 'div' );
-			stubWindowScroll( 1000, 1000 );
+
+			testUtils.sinon.stub( window, 'scrollX', 1000 );
+			testUtils.sinon.stub( window, 'scrollY', 1000 );
 
 			Object.assign( positionedParent.style, {
 				position: 'absolute',
@@ -313,20 +308,6 @@ const attachTop = ( targetRect, elementRect ) => ( {
 	name: 'bottom'
 } );
 
-function stubWindowScroll( x, y ) {
-	const { scrollX: savedX, scrollY: savedY } = window;
-
-	window.scrollX = x;
-	window.scrollY = y;
-
-	revertWindowScroll = () => {
-		window.scrollX = savedX;
-		window.scrollY = savedY;
-
-		revertWindowScroll = null;
-	};
-}
-
 function stubElementRect( element, rect ) {
 	if ( element.getBoundingClientRect.restore ) {
 		element.getBoundingClientRect.restore();

+ 9 - 5
packages/ckeditor5-utils/tests/dom/rect.js

@@ -3,11 +3,14 @@
  * For licensing, see LICENSE.md.
  */
 
-/* global document, window */
+/* global document */
 
+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', () => {
@@ -325,10 +328,11 @@ describe( 'Rect', () => {
 		} );
 
 		it( 'should return the viewport\'s rect', () => {
-			window.scrollX = 100;
-			window.scrollY = 200;
-			window.innerWidth = 1000;
-			window.innerHeight = 500;
+			testUtils.sinon.stub( window, 'innerWidth', 1000 );
+			testUtils.sinon.stub( window, 'innerHeight', 500 );
+
+			testUtils.sinon.stub( window, 'scrollX', 100 );
+			testUtils.sinon.stub( window, 'scrollY', 200 );
 
 			assertRect( Rect.getViewportRect(), {
 				top: 0,