-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathDOMUtilities.test.ts
65 lines (55 loc) · 2.35 KB
/
DOMUtilities.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
// Copyright (c) 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {renderElementIntoDOM} from '../../testing/DOMHelpers.js';
import * as Platform from './platform.js';
describe('DOMUtilities', () => {
describe('deepActiveElement', () => {
it('returns the active element if there is no shadow root', () => {
const btn = document.createElement('button');
btn.innerText = 'Click me!';
renderElementIntoDOM(btn);
btn.focus();
const activeElement = Platform.DOMUtilities.deepActiveElement(document);
assert.strictEqual(activeElement, btn);
});
it('dives through the shadow root and finds the right active element', () => {
class TestComponent extends HTMLElement {
shadow = this.attachShadow({mode: 'open'});
button = document.createElement('button');
connectedCallback(): void {
this.button.innerText = 'Click me from the shadow root!';
this.shadow.appendChild(this.button);
this.button.focus();
}
}
customElements.define('dom-utilities-test-component', TestComponent);
const component = new TestComponent();
renderElementIntoDOM(component);
const activeElement = Platform.DOMUtilities.deepActiveElement(document);
assert.strictEqual(activeElement, component.button);
});
});
describe('getEnclosingShadowRootForNode', () => {
it('returns null if no shadow root is found up the tree', () => {
const parent = document.createElement('div');
const child = document.createElement('p');
parent.appendChild(child);
renderElementIntoDOM(parent);
assert.isNull(Platform.DOMUtilities.getEnclosingShadowRootForNode(child));
});
it('returns the shadow root in the tree', () => {
const div = document.createElement('div');
class TestComponent extends HTMLElement {
readonly #shadow = this.attachShadow({mode: 'open'});
connectedCallback() {
this.#shadow.appendChild(div);
}
}
customElements.define('shadow-root-test', TestComponent);
const component = new TestComponent();
renderElementIntoDOM(component);
assert.strictEqual(Platform.DOMUtilities.getEnclosingShadowRootForNode(div), component.shadowRoot);
});
});
});