diff --git a/src/mock-doc/global.ts b/src/mock-doc/global.ts index 9f872fb12e6..f41972cd51e 100644 --- a/src/mock-doc/global.ts +++ b/src/mock-doc/global.ts @@ -93,6 +93,7 @@ const WINDOW_PROPS = [ 'Event', 'Element', 'HTMLElement', + 'Node', 'NodeList', 'KeyboardEvent', 'MouseEvent' diff --git a/src/mock-doc/window.ts b/src/mock-doc/window.ts index 381443a616d..76d71675716 100644 --- a/src/mock-doc/window.ts +++ b/src/mock-doc/window.ts @@ -2,7 +2,7 @@ import { createConsole } from './console'; import { MockCustomElementRegistry, resetCustomElementRegistry } from './custom-element-registry'; import { MockCustomEvent, MockEvent, MockKeyboardEvent, MockMouseEvent, addEventListener, dispatchEvent, removeEventListener, resetEventListeners } from './event'; import { MockDocument, resetDocument } from './document'; -import { MockElement, MockHTMLElement, MockNodeList} from './node'; +import { MockElement, MockHTMLElement, MockNode, MockNodeList } from './node'; import { MockHistory } from './history'; import { MockLocation } from './location'; import { MockNavigator } from './navigator'; @@ -14,6 +14,7 @@ import { URL } from 'url'; const historyMap = new WeakMap(); const elementCstrMap = new WeakMap(); const htmlElementCstrMap = new WeakMap(); +const nodeCstrMap = new WeakMap(); const nodeListCstrMap = new WeakMap(); const localStorageMap = new WeakMap(); const locMap = new WeakMap(); @@ -213,6 +214,20 @@ export class MockWindow { return ElementCstr; } + get Node() { + let NodeCstr = nodeCstrMap.get(this); + if (NodeCstr == null) { + const ownerDocument = this.document; + NodeCstr = class extends MockNode { + constructor() { + super(ownerDocument, 0, 'test', ''); + throw (new Error('Illegal constructor: cannot constructor')); + } + }; + return NodeCstr; + } + } + get NodeList() { let NodeListCstr = nodeListCstrMap.get(this); if (NodeListCstr == null) { diff --git a/src/runtime/test/globals.spec.tsx b/src/runtime/test/globals.spec.tsx index 652f02242fc..e2d89b5fa72 100644 --- a/src/runtime/test/globals.spec.tsx +++ b/src/runtime/test/globals.spec.tsx @@ -40,10 +40,12 @@ describe('globals', () => { class CmpEl { // @ts-ignore private protoEl: any; + private protoNode: any; private protoNodeList: any; constructor() { this.protoEl = Element.prototype; + this.protoNode = Node.prototype; this.protoNodeList = NodeList.prototype; } } @@ -53,6 +55,14 @@ describe('globals', () => { html: `` }); }); + + it('allows access to the Node prototype', async () => { + expect(page.rootInstance.protoNode).toEqual(Node.prototype); + expect(page.rootInstance.protoNode).toEqual((page.win as any).Node.prototype); + expect(page.rootInstance.protoNode).toEqual((window as any).Node.prototype); + expect(page.rootInstance.protoNode).toEqual((global as any).Node.prototype); + expect(page.rootInstance.protoNode).toBeTruthy(); + }); it('allows access to the NodeList prototype', async () => { expect(page.rootInstance.protoNodeList).toEqual(NodeList.prototype); expect(page.rootInstance.protoNodeList).toEqual((page.win as any).NodeList.prototype);