瀏覽代碼

Implemented global module to create stubbable access to global DOM window and document objects.

Aleksander Nowodzinski 9 年之前
父節點
當前提交
c281596570
共有 2 個文件被更改,包括 48 次插入0 次删除
  1. 24 0
      packages/ckeditor5-utils/src/dom/global.js
  2. 24 0
      packages/ckeditor5-utils/tests/dom/global.js

+ 24 - 0
packages/ckeditor5-utils/src/dom/global.js

@@ -0,0 +1,24 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals window, document */
+
+/**
+ * @module utils/dom/global
+ */
+
+/**
+ * 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
+ *
+ *		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 );
+ */
+export default { document, window };

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

@@ -0,0 +1,24 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* global window, document */
+
+import global from 'ckeditor5/utils/dom/global.js';
+
+describe( 'global', () => {
+	describe( 'global', () => {
+		describe( 'window', () => {
+			it( 'equals native DOM window', () => {
+				expect( global.window ).to.equal( window );
+			} );
+		} );
+
+		describe( 'document', () => {
+			it( 'equals native DOM document', () => {
+				expect( global.document ).to.equal( document );
+			} );
+		} );
+	} );
+} );