Skip to content

Commit f88fb2e

Browse files
authored
fix(mock-doc): Add missing DOMParser stub to MockWindow (#3279)
this commit adds a DOMParser stub to MockWindow, as well as tests for the presence of globals: - DOMParser - Request - Response
1 parent 7a20cad commit f88fb2e

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/mock-doc/global.ts

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { MockCustomEvent, MockEvent, MockKeyboardEvent, MockMouseEvent } from './event';
1717
import { MockHeaders } from './headers';
1818
import { MockRequest, MockResponse } from './request-response';
19+
import { MockDOMParser } from './parser';
1920
import { MockWindow } from './window';
2021

2122
export function setupGlobal(gbl: any) {
@@ -159,6 +160,7 @@ const GLOBAL_CONSTRUCTORS: [string, any][] = [
159160
['MouseEvent', MockMouseEvent],
160161
['Request', MockRequest],
161162
['Response', MockResponse],
163+
['DOMParser', MockDOMParser],
162164

163165
['HTMLAnchorElement', MockAnchorElement],
164166
['HTMLBaseElement', MockBaseElement],

src/mock-doc/parser.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { MockDocument } from './document';
2+
import { parseHtmlToDocument } from './parse-html';
3+
4+
export type DOMParserSupportedType =
5+
| 'text/html'
6+
| 'text/xml'
7+
| 'application/xml'
8+
| 'application/xhtml+xml'
9+
| 'image/svg+xml';
10+
11+
export class MockDOMParser {
12+
parseFromString(htmlToParse: string, mimeType: DOMParserSupportedType): MockDocument {
13+
if (mimeType !== 'text/html') {
14+
console.error('XML parsing not implemented yet, continuing as html');
15+
}
16+
return parseHtmlToDocument(htmlToParse);
17+
}
18+
}

src/mock-doc/test/global.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,13 @@ describe('global', () => {
2828
expect(KeyboardEvent).toBeDefined();
2929
expect(MouseEvent).toBeDefined();
3030
});
31+
32+
it('Fetch', () => {
33+
expect(Request).toBeDefined();
34+
expect(Response).toBeDefined();
35+
});
36+
37+
it('Parse', () => {
38+
expect(DOMParser).toBeDefined();
39+
});
3140
});

0 commit comments

Comments
 (0)