Repository: capricorn86/happy-dom
Pull request #184 (2020-12-14 22:48:25)

#175@patch: The methods window.whenAsyncComplete() and window.cancelA…
…sync() has been moved to window.happyDOM.whenAsyncComplete() and window.happyDOM.cancelAsync().


Patch:
diff --git a/README.md b/README.md
index 3b5afeba..95bdf0da 100644
--- a/README.md
+++ b/README.md
@@ -82,7 +82,11 @@ This package makes it easier to setup servering side rendering of web components
 
 
 
-# Whats New in 1.0.0?
+# Whats New in v2.0.0?
+
+- The methods window.whenAsyncComplete() and window.cancelAsync() has been moved to window.happyDOM.whenAsyncComplete() and window.happyDOM.cancelAsync()
+
+# Whats New in v1.0.0?
 
 - [Lerna](https://lerna.js.org/) is used for managing all packages within a single repository
 
diff --git a/packages/happy-dom/README.md b/packages/happy-dom/README.md
index 0aafa6b2..efac8065 100644
--- a/packages/happy-dom/README.md
+++ b/packages/happy-dom/README.md
@@ -92,7 +92,7 @@ const script = new VM.Script(`
 `);
 
 window.location.href = 'http://localhost:8080';
-window.whenAsyncComplete().then(() => {
+window.happyDOM.whenAsyncComplete().then(() => {
     const myContainer = document.querySelector('.myContainer div');
 
     // Will output "Test"
@@ -137,10 +137,22 @@ AsyncWindow extends Window and adds support for fetch(). It also exposes two met
 
 Returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that is resolved when all async tasks has been completed.
 
+```javascript
+window.happyDOM.whenAsyncComplete().then(() => {
+    // Do something when all async task are completed.
+});
+```
+
 **cancelAsync()**
 
 This method will cancel all running async tasks.
 
+```javascript
+setTimeout(() => {
+    // This timeout will be canceled
+});
+window.happyDOM.cancelAsync();
+```
 
 # Performance
 
diff --git a/packages/happy-dom/src/window/AsyncWindow.ts b/packages/happy-dom/src/window/AsyncWindow.ts
index 48077de6..76777121 100644
--- a/packages/happy-dom/src/window/AsyncWindow.ts
+++ b/packages/happy-dom/src/window/AsyncWindow.ts
@@ -11,6 +11,12 @@ const FETCH_RESPONSE_TYPE_METHODS = ['blob', 'json', 'formData', 'text'];
  * The async Window makes it possible to wait for asyncrounous tasks to complete by calling the method whenAsyncComplete(). It also adds support for the method fetch().
  */
 export default class AsyncWindow extends Window {
+	// Public Properties
+	public happyDOM: {
+		whenAsyncComplete: () => Promise<void>;
+		cancelAsync: () => void;
+	} = null;
+
 	// Private Properties
 	private _asyncTaskManager = new AsyncTaskManager();
 
@@ -20,6 +26,15 @@ export default class AsyncWindow extends Window {
 	constructor() {
 		super();
 
+		this.happyDOM = {
+			whenAsyncComplete: async () => {
+				return this._asyncTaskManager.whenComplete();
+			},
+			cancelAsync: () => {
+				this._asyncTaskManager.cancelAllTasks();
+			}
+		};
+
 		// Binds all methods to "this", so that it will use the correct context when called globally.
 		for (const key of Object.keys(AsyncWindow.prototype)) {
 			if (typeof this[key] === 'function') {
@@ -154,21 +169,4 @@ export default class AsyncWindow extends Window {
 				});
 		});
 	}
-
-	/**
-	 * Returns a promise that is fulfilled when async tasks are complete.
-	 * This method is not part of the HTML standard.
-	 *
-	 * @returns Promise.
-	 */
-	public async whenAsyncComplete(): Promise<void> {
-		return this._asyncTaskManager.whenComplete();
-	}
-
-	/**
-	 * Cancels all async tasks running.
-	 */
-	public cancelAsync(): void {
-		this._asyncTaskManager.cancelAllTasks();
-	}
 }
diff --git a/packages/jest-environment/README.md b/packages/jest-environment/README.md
index e6eef15b..8fba9836 100644
--- a/packages/jest-environment/README.md
+++ b/packages/jest-environment/README.md
@@ -109,7 +109,7 @@ When testing asynchrounous code it is possible to wait for asynchrounous tasks t
 describe('scrollToTop()', () => {
     it('scrolls to top using the built in browser animation', async () => {
         element.scrollToTop();
-        await whenAsyncComplete();
+        await happyDOM.whenAsyncComplete();
         expect(document.documentElement.scrollTop).toBe(0);
     });
 });
diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts
index 97f09352..1a7b6197 100644
--- a/packages/jest-environment/src/index.ts
+++ b/packages/jest-environment/src/index.ts
@@ -73,7 +73,7 @@ export default class HappyDOMEnvironment implements JestEnvironment {
 	public async teardown(): Promise<void> {
 		this.fakeTimers.dispose();
 		this.fakeTimersModern.dispose();
-		this.global.cancelAsync();
+		this.global.happyDOM.cancelAsync();
 
 		this.global = null;
 		this.moduleMocker = null;
diff --git a/packages/server-rendering/src/context/HappyDOMContext.ts b/packages/server-rendering/src/context/HappyDOMContext.ts
index 150ac686..a222cd6c 100644
--- a/packages/server-rendering/src/context/HappyDOMContext.ts
+++ b/packages/server-rendering/src/context/HappyDOMContext.ts
@@ -64,7 +64,7 @@ export default class HappyDOMContext {
 				global.eval = () => {};
 			}
 
-			window
+			window.happyDOM
 				.whenAsyncComplete()
 				.then(() => {
 					if (customElements.addCSSToHead) {
@@ -95,7 +95,7 @@ export default class HappyDOMContext {
 	 * Disposes the render.
 	 */
 	public dispose(): void {
-		this.window.cancelAsync();
+		this.window.happyDOM.cancelAsync();
 	}
 
 	/**


Test patch:
diff --git a/packages/happy-dom/test/file/FileReader.test.ts b/packages/happy-dom/test/file/FileReader.test.ts
index 932458d2..a3480227 100644
--- a/packages/happy-dom/test/file/FileReader.test.ts
+++ b/packages/happy-dom/test/file/FileReader.test.ts
@@ -22,7 +22,7 @@ describe('FileReader', () => {
 				result = fileReader.result;
 			});
 			fileReader.readAsDataURL(blob);
-			await window.whenAsyncComplete();
+			await window.happyDOM.whenAsyncComplete();
 			expect(result).toBe('data:text/plain;charset=utf-8;base64,VEVTVA==');
 		});
 	});
diff --git a/packages/happy-dom/test/nodes/element/Element.test.ts b/packages/happy-dom/test/nodes/element/Element.test.ts
index 7ac35954..6c890605 100644
--- a/packages/happy-dom/test/nodes/element/Element.test.ts
+++ b/packages/happy-dom/test/nodes/element/Element.test.ts
@@ -755,7 +755,7 @@ describe('Element', () => {
 				element[functionName]({ left: 50, top: 60, behavior: 'smooth' });
 				expect(element.scrollLeft).toBe(0);
 				expect(element.scrollTop).toBe(0);
-				await window.whenAsyncComplete();
+				await window.happyDOM.whenAsyncComplete();
 				expect(element.scrollLeft).toBe(50);
 				expect(element.scrollTop).toBe(60);
 			});
diff --git a/packages/happy-dom/test/window/AsyncWindow.test.ts b/packages/happy-dom/test/window/AsyncWindow.test.ts
index b2419ac1..a7124ad4 100644
--- a/packages/happy-dom/test/window/AsyncWindow.test.ts
+++ b/packages/happy-dom/test/window/AsyncWindow.test.ts
@@ -115,7 +115,7 @@ describe('AsyncWindow', () => {
 		});
 	});
 
-	describe('whenAsyncComplete()', () => {
+	describe('happyDOM.whenAsyncComplete()', () => {
 		test('Resolves the Promise returned by whenAsyncComplete() when all async tasks has been completed.', async () => {
 			jest.spyOn(window, 'fetch').mockImplementation(() => {
 				return Promise.resolve({
@@ -125,7 +125,7 @@ describe('AsyncWindow', () => {
 			});
 
 			let isFirstWhenAsyncCompleteCalled = false;
-			window.whenAsyncComplete().then(() => {
+			window.happyDOM.whenAsyncComplete().then(() => {
 				isFirstWhenAsyncCompleteCalled = true;
 			});
 			let tasksDone = 0;
@@ -155,13 +155,13 @@ describe('AsyncWindow', () => {
 					tasksDone++;
 				})
 			);
-			await window.whenAsyncComplete();
+			await window.happyDOM.whenAsyncComplete();
 			expect(tasksDone).toBe(6);
 			expect(isFirstWhenAsyncCompleteCalled).toBe(true);
 		});
 	});
 
-	describe('cancelAsync()', () => {
+	describe('happyDOM.cancelAsync()', () => {
 		test('Cancels all ongoing asynchrounous tasks.', done => {
 			jest.spyOn(window, 'fetch').mockImplementation(() => {
 				return Promise.resolve({
@@ -171,7 +171,7 @@ describe('AsyncWindow', () => {
 			});
 
 			let isFirstWhenAsyncCompleteCalled = false;
-			window.whenAsyncComplete().then(() => {
+			window.happyDOM.whenAsyncComplete().then(() => {
 				isFirstWhenAsyncCompleteCalled = true;
 			});
 			let tasksDone = 0;
@@ -203,11 +203,11 @@ describe('AsyncWindow', () => {
 			);
 
 			let isSecondWhenAsyncCompleteCalled = false;
-			window.whenAsyncComplete().then(() => {
+			window.happyDOM.whenAsyncComplete().then(() => {
 				isSecondWhenAsyncCompleteCalled = true;
 			});
 
-			window.cancelAsync();
+			window.happyDOM.cancelAsync();
 
 			expect(tasksDone).toBe(0);
 
@@ -225,7 +225,7 @@ describe('AsyncWindow', () => {
 				window[functionName]({ left: 50, top: 60, behavior: 'smooth' });
 				expect(window.document.documentElement.scrollLeft).toBe(0);
 				expect(window.document.documentElement.scrollTop).toBe(0);
-				await window.whenAsyncComplete();
+				await window.happyDOM.whenAsyncComplete();
 				expect(window.document.documentElement.scrollLeft).toBe(50);
 				expect(window.document.documentElement.scrollTop).toBe(60);
 			});