Skip to content

Commit 1eeb780

Browse files
kensodemannmanucorporat
authored andcommitted
feat(mock-doc): add Element (#1602)
1 parent 140b164 commit 1eeb780

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

src/mock-doc/global.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const WINDOW_PROPS = [
5858
'CSS',
5959
'CustomEvent',
6060
'Event',
61+
'Element',
6162
'HTMLElement',
6263
'KeyboardEvent'
6364
];

src/mock-doc/window.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { URL } from 'url';
1212

1313

1414
const historyMap = new WeakMap<MockWindow, MockHistory>();
15+
const elementCstrMap = new WeakMap<MockWindow, any>();
1516
const htmlElementCstrMap = new WeakMap<MockWindow, any>();
1617
const localStorageMap = new WeakMap<MockWindow, MockStorage>();
1718
const locMap = new WeakMap<MockWindow, MockLocation>();
@@ -180,6 +181,21 @@ export class MockWindow {
180181
historyMap.set(this, hsty);
181182
}
182183

184+
get Element() {
185+
let ElementCstr = elementCstrMap.get(this);
186+
if (ElementCstr == null) {
187+
const ownerDocument = this.document;
188+
ElementCstr = class extends MockElement {
189+
constructor() {
190+
super(ownerDocument, '');
191+
throw (new Error('Illegal constructor: cannot construct Element'));
192+
}
193+
};
194+
elementCstrMap.set(this, ElementCstr);
195+
}
196+
return ElementCstr;
197+
}
198+
183199
get HTMLElement() {
184200
let HtmlElementCstr = htmlElementCstrMap.get(this);
185201
if (HtmlElementCstr == null) {
@@ -421,6 +437,7 @@ export function resetWindow(win: Window) {
421437

422438
historyMap.delete(win as any);
423439
htmlElementCstrMap.delete(win as any);
440+
elementCstrMap.delete(win as any);
424441
localStorageMap.delete(win as any);
425442
locMap.delete(win as any);
426443
navMap.delete(win as any);

src/runtime/test/element.spec.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { Component, Element, Method } from '@stencil/core';
22
import { newSpecPage } from '@stencil/core/testing';
33

4-
54
describe('element', () => {
6-
7-
it('event normal ionChange event', async () => {
8-
@Component({ tag: 'cmp-a'})
5+
it('allows the class to be set', async () => {
6+
@Component({ tag: 'cmp-a' })
97
class CmpA {
10-
118
@Element() el: HTMLElement;
129

1310
@Method()
@@ -18,7 +15,7 @@ describe('element', () => {
1815
// @ts-ignore
1916
const page = await newSpecPage({
2017
components: [CmpA],
21-
html: `<cmp-a></cmp-a>`,
18+
html: `<cmp-a></cmp-a>`
2219
});
2320

2421
expect(page.root).toEqualHtml(`
@@ -32,5 +29,4 @@ describe('element', () => {
3229
<cmp-a class="new-class"></cmp-a>
3330
`);
3431
});
35-
3632
});

src/runtime/test/globals.spec.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,24 @@ describe('globals', () => {
2828
});
2929
});
3030

31+
it('allows access to the Element prototype', async () => {
32+
@Component({ tag: 'cmp-el' })
33+
class CmpEl {
34+
// @ts-ignore
35+
private proto: any;
36+
37+
constructor() {
38+
this.proto = Element.prototype;
39+
}
40+
}
41+
42+
const page = await newSpecPage({
43+
components: [CmpEl],
44+
html: `<cmp-el></cmp-el>`
45+
});
46+
47+
expect(page.root).toEqualHtml(`
48+
<cmp-el></cmp-el>
49+
`);
50+
});
3151
});

0 commit comments

Comments
 (0)