Skip to content

Commit

Permalink
capricorn86#526@minor: Continue Fixes Problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mas0nShi committed Oct 16, 2022
1 parent b72cdd9 commit 608b717
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
17 changes: 13 additions & 4 deletions packages/happy-dom/test/window/Window.test.ts
Expand Up @@ -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]);
});
}
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 8 additions & 10 deletions 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
});
});

0 comments on commit 608b717

Please sign in to comment.