Skip to content

Commit

Permalink
feat(mock-doc): try adding Node to mock-doc (#1947)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelyoobic95 authored and manucorporat committed Oct 11, 2019
1 parent 59d7a55 commit 3b6177b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/mock-doc/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const WINDOW_PROPS = [
'Event',
'Element',
'HTMLElement',
'Node',
'NodeList',
'KeyboardEvent',
'MouseEvent'
Expand Down
17 changes: 16 additions & 1 deletion src/mock-doc/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -14,6 +14,7 @@ import { URL } from 'url';
const historyMap = new WeakMap<MockWindow, MockHistory>();
const elementCstrMap = new WeakMap<MockWindow, any>();
const htmlElementCstrMap = new WeakMap<MockWindow, any>();
const nodeCstrMap = new WeakMap<MockWindow, any>();
const nodeListCstrMap = new WeakMap<MockWindow, any>();
const localStorageMap = new WeakMap<MockWindow, MockStorage>();
const locMap = new WeakMap<MockWindow, MockLocation>();
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions src/runtime/test/globals.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -53,6 +55,14 @@ describe('globals', () => {
html: `<cmp-el></cmp-el>`
});
});

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);
Expand Down

0 comments on commit 3b6177b

Please sign in to comment.