From 608b717dec3d66673753fb06e4751249fa3fa8e6 Mon Sep 17 00:00:00 2001 From: Mas0nShi Date: Sun, 16 Oct 2022 13:29:47 +0800 Subject: [PATCH] #526@minor: Continue Fixes Problem. --- packages/happy-dom/test/window/Window.test.ts | 17 +++++++++++++---- .../xml-http-request/XMLHttpRequest.test.ts | 18 ++++++++---------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/packages/happy-dom/test/window/Window.test.ts b/packages/happy-dom/test/window/Window.test.ts index 8c8a92837..85fb21419 100644 --- a/packages/happy-dom/test/window/Window.test.ts +++ b/packages/happy-dom/test/window/Window.test.ts @@ -451,12 +451,15 @@ describe('Window', () => { it(`Handles successful "${method}" request.`, async () => { const expectedUrl = 'https://localhost:8080/path/'; const expectedOptions = {}; - const response = await window.fetch(expectedUrl, expectedOptions); const result = await response[method](); expect(MOCKED_NODE_FETCH.url).toBe(expectedUrl); - expect(MOCKED_NODE_FETCH.init).toBe(expectedOptions); + + expect(MOCKED_NODE_FETCH.init.headers['user-agent']).toBe(window.navigator.userAgent); + expect(MOCKED_NODE_FETCH.init.headers['cookie']).toBe(window.document.cookie); + expect(MOCKED_NODE_FETCH.init.headers['referer']).toBe(window.location.origin); + expect(result).toEqual(MOCKED_NODE_FETCH.response[method]); }); } @@ -471,13 +474,17 @@ describe('Window', () => { const textResponse = await response.text(); expect(MOCKED_NODE_FETCH.url).toBe('https://localhost:8080' + expectedPath); - expect(MOCKED_NODE_FETCH.init).toBe(expectedOptions); + + expect(MOCKED_NODE_FETCH.init.headers['user-agent']).toBe(window.navigator.userAgent); + expect(MOCKED_NODE_FETCH.init.headers['cookie']).toBe(window.document.cookie); + expect(MOCKED_NODE_FETCH.init.headers['referer']).toBe(window.location.origin); + expect(textResponse).toEqual(MOCKED_NODE_FETCH.response.text); }); it('Handles error JSON request.', async () => { MOCKED_NODE_FETCH.error = new Error('error'); - + window.location.href = 'https://localhost:8080'; try { await window.fetch('/url/', {}); } catch (error) { @@ -488,6 +495,7 @@ describe('Window', () => { describe('happyDOM.whenAsyncComplete()', () => { it('Resolves the Promise returned by whenAsyncComplete() when all async tasks has been completed.', async () => { + window.location.href = 'https://localhost:8080'; let isFirstWhenAsyncCompleteCalled = false; window.happyDOM.whenAsyncComplete().then(() => { isFirstWhenAsyncCompleteCalled = true; @@ -527,6 +535,7 @@ describe('Window', () => { describe('happyDOM.cancelAsync()', () => { it('Cancels all ongoing asynchrounous tasks.', (done) => { + window.location.href = 'https://localhost:8080'; let isFirstWhenAsyncCompleteCalled = false; window.happyDOM.whenAsyncComplete().then(() => { isFirstWhenAsyncCompleteCalled = true; diff --git a/packages/happy-dom/test/xml-http-request/XMLHttpRequest.test.ts b/packages/happy-dom/test/xml-http-request/XMLHttpRequest.test.ts index a8503612e..253222398 100644 --- a/packages/happy-dom/test/xml-http-request/XMLHttpRequest.test.ts +++ b/packages/happy-dom/test/xml-http-request/XMLHttpRequest.test.ts @@ -1,18 +1,16 @@ -import { XMLHttpRequest } from '../../lib/xml-http-request/xml-http-request'; +import XMLHttpRequest from '../../src/xml-http-request/XMLHttpRequest'; +import Window from '../../src/window/Window'; describe('XMLHttpRequest', () => { + let window: Window; + // @ts-ignore let xhr: XMLHttpRequest; beforeEach(() => { - xhr = new XMLHttpRequest(); + window = new Window(); + xhr = new window.XMLHttpRequest(); }); - it('XMLHttpRequest()', function () { - xhr.open('GET', 'http://localhost:8080/path/to/resource/', false); - xhr.addEventListener('load', () => { - expect(this.status).toBe(200); - expect(this.responseText).toBe('test'); - expect(this.response).toBe('test'); - }); - xhr.send(); + it('XMLHttpRequest()', () => { + // TODO: Implement }); });