-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
ShadowRoot.test.ts
110 lines (82 loc) · 3.26 KB
/
ShadowRoot.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import IHTMLElement from '../../../src/nodes/html-element/IHTMLElement';
import Window from '../../../src/window/Window';
import CustomElement from '../../CustomElement';
describe('ShadowRoot', () => {
let window;
let document;
beforeEach(() => {
window = new Window();
document = window.document;
window.customElements.define('custom-element', CustomElement);
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('set innerHTML()', () => {
it('Sets the innerHTML of the shadow root.', () => {
const shadowRoot = document.createElement('custom-element').shadowRoot;
shadowRoot.innerHTML = '<div attr1="value1" attr2="value2"><span>Test</span></div>';
expect(shadowRoot.childNodes.length).toBe(1);
expect(shadowRoot.childNodes[0].childNodes.length).toBe(1);
expect(shadowRoot.childNodes[0].tagName).toBe('DIV');
expect(shadowRoot.childNodes[0].childNodes[0].tagName).toBe('SPAN');
});
});
describe('get innerHTML()', () => {
it('Returns the innerHTML of the shadow root.', () => {
const html = '<div attr1="value1" attr2="value2"><span>Test</span></div>';
const shadowRoot = document.createElement('custom-element').shadowRoot;
shadowRoot.innerHTML = html;
expect(shadowRoot.innerHTML).toBe(html);
});
});
describe('get activeElement()', () => {
it('Returns the currently active element within the ShadowRoot.', () => {
const customElement = document.createElement('custom-element');
const shadowRoot = customElement.shadowRoot;
const div = <IHTMLElement>document.createElement('div');
const span = <IHTMLElement>document.createElement('span');
document.body.appendChild(customElement);
shadowRoot.appendChild(div);
shadowRoot.appendChild(span);
expect(shadowRoot.activeElement === null).toBe(true);
div.focus();
expect(shadowRoot.activeElement === div).toBe(true);
span.focus();
expect(shadowRoot.activeElement === span).toBe(true);
span.blur();
expect(shadowRoot.activeElement === null).toBe(true);
document.body.appendChild(span);
span.focus();
expect(shadowRoot.activeElement === null).toBe(true);
});
it('Unsets the active element when it gets disconnected.', () => {
const customElement = document.createElement('custom-element');
const shadowRoot = customElement.shadowRoot;
const div = <IHTMLElement>document.createElement('div');
document.body.appendChild(customElement);
shadowRoot.appendChild(div);
expect(shadowRoot.activeElement === null).toBe(true);
div.focus();
expect(shadowRoot.activeElement === div).toBe(true);
customElement.remove();
expect(shadowRoot.activeElement === null).toBe(true);
});
});
describe('toString()', () => {
it('Returns the innerHTML of the shadow root.', () => {
const html = '<div attr1="value1" attr2="value2"><span>Test</span></div>';
const shadowRoot = document.createElement('custom-element').shadowRoot;
shadowRoot.innerHTML = html;
expect(shadowRoot.toString()).toBe(html);
});
});
describe('cloneNode()', () => {
it('Clones the value of the "mode" property when cloned.', () => {
const shadowRoot = document.createElement('custom-element').shadowRoot;
const clone = shadowRoot.cloneNode();
expect(shadowRoot.mode).toBe('open');
expect(clone.mode).toBe('open');
});
});
});