Skip to content

Commit

Permalink
feat(mock-doc): add NodeList (#1614)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelyoobic95 authored and kensodemann committed Jun 29, 2019
1 parent 283d395 commit ee3fdf9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/mock-doc/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ const WINDOW_PROPS = [
'Event',
'Element',
'HTMLElement',
'NodeList',
'KeyboardEvent'
];
12 changes: 12 additions & 0 deletions src/mock-doc/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ export class MockNode {
}


export class MockNodeList {
childNodes: MockNode[];
length: number;
ownerDocument: any;

constructor(ownerDocument: any, childNodes: MockNode[], length: number) {
this.ownerDocument = ownerDocument;
this.childNodes = childNodes;
this.length = length;
}
}

const attrsMap = new WeakMap<MockElement, MockAttributeMap>();
const shadowRootMap = new WeakMap<MockElement, ShadowRoot>();
const stylesMap = new WeakMap<MockElement, CSSStyleDeclaration>();
Expand Down
18 changes: 17 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, addEventListener, dispatchEvent, removeEventListener, resetEventListeners } from './event';
import { MockDocument, resetDocument } from './document';
import { MockElement, MockHTMLElement } from './node';
import { MockElement, MockHTMLElement, 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 nodeListCstrMap = new WeakMap<MockWindow, any>();
const localStorageMap = new WeakMap<MockWindow, MockStorage>();
const locMap = new WeakMap<MockWindow, MockLocation>();
const navMap = new WeakMap<MockWindow, MockNavigator>();
Expand Down Expand Up @@ -200,6 +201,20 @@ export class MockWindow {
return ElementCstr;
}

get NodeList() {
let NodeListCstr = nodeListCstrMap.get(this);
if (NodeListCstr == null) {
const ownerDocument = this.document;
NodeListCstr = class extends MockNodeList {
constructor() {
super(ownerDocument, [], 0);
throw (new Error('Illegal constructor: cannot constructor'))
}
};
return NodeListCstr;
}
}

get HTMLElement() {
let HtmlElementCstr = htmlElementCstrMap.get(this);
if (HtmlElementCstr == null) {
Expand Down Expand Up @@ -442,6 +457,7 @@ export function resetWindow(win: Window) {
historyMap.delete(win as any);
htmlElementCstrMap.delete(win as any);
elementCstrMap.delete(win as any);
nodeListCstrMap.delete(win as any);
localStorageMap.delete(win as any);
locMap.delete(win as any);
navMap.delete(win as any);
Expand Down
50 changes: 32 additions & 18 deletions src/runtime/test/globals.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,40 @@ describe('globals', () => {
expect((window as any).JSON.stringify([0])).toEqual('[0]');
});

it('allows access to the Element prototype', async () => {
@Component({ tag: 'cmp-el' })
class CmpEl {
// @ts-ignore
private proto: any;

constructor() {
this.proto = Element.prototype;
describe('globals/prototypes', () => {
let page;
beforeEach(async () => {
@Component({ tag: 'cmp-el' })
class CmpEl {
// @ts-ignore
private protoEl: any;
private protoNodeList: any;

constructor() {
this.protoEl = Element.prototype;
this.protoNodeList = NodeList.prototype;
}
}
}

const page = await newSpecPage({
components: [CmpEl],
html: `<cmp-el></cmp-el>`
page = await newSpecPage({
components: [CmpEl],
html: `<cmp-el></cmp-el>`
});
})
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);
expect(page.rootInstance.protoNodeList).toEqual((window as any).NodeList.prototype);
expect(page.rootInstance.protoNodeList).toEqual((global as any).NodeList.prototype);
expect(page.rootInstance.protoNodeList).toBeTruthy();
});

it('allows access to the Element prototype', async () => {
expect(page.rootInstance.protoEl).toEqual(Element.prototype);
expect(page.rootInstance.protoEl).toEqual((page.win as any).Element.prototype);
expect(page.rootInstance.protoEl).toEqual((window as any).Element.prototype);
expect(page.rootInstance.protoEl).toEqual((global as any).Element.prototype);
expect(page.rootInstance.protoEl).toBeTruthy();
});

expect(page.rootInstance.proto).toEqual(Element.prototype);
expect(page.rootInstance.proto).toEqual((page.win as any).Element.prototype);
expect(page.rootInstance.proto).toEqual((window as any).Element.prototype);
expect(page.rootInstance.proto).toEqual((global as any).Element.prototype);
expect(page.rootInstance.proto).toBeTruthy();
});
});

0 comments on commit ee3fdf9

Please sign in to comment.