-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathdom-utils.test.js
More file actions
196 lines (161 loc) · 6.03 KB
/
dom-utils.test.js
File metadata and controls
196 lines (161 loc) · 6.03 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { expect } from '@vaadin/chai-plugins';
import { defineCE, fixtureSync } from '@vaadin/testing-helpers';
import {
addValueToAttribute,
getAncestorRootNodes,
getClosestElement,
getFlattenedElements,
isEmptyTextNode,
removeValueFromAttribute,
} from '../src/dom-utils.js';
describe('dom-utils', () => {
describe('getAncestorRootNodes', () => {
let element, child;
const tag = defineCE(
class ShadowElement extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<div class="root">
<slot></slot>
</div>
`;
}
},
);
beforeEach(() => {
element = fixtureSync(`
<${tag}>
<div class="child"></div>
</${tag}>
`);
child = element.querySelector('.child');
});
it('should return an array of the ancestor root nodes for a node', () => {
const nodes = getAncestorRootNodes(child);
expect(nodes).to.have.lengthOf(2);
expect(nodes[0]).to.equal(element.shadowRoot);
expect(nodes[1]).to.equal(document);
});
});
describe('getClosestElement', () => {
let element, node;
const tag = defineCE(
class ShadowElement extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<div class="parent parent-1">
<div class="parent parent-2">
<div class="child"></div>
</div>
</div>
`;
}
},
);
it('should return the closest element matching the selector within the shadow root', () => {
element = fixtureSync(`<${tag}></${tag}>`);
node = element.shadowRoot.querySelector('.child');
const expected = element.shadowRoot.querySelector('.parent-2');
expect(getClosestElement('.parent', node)).to.equal(expected);
});
it('should return the closest element matching the selector across the shadow root', () => {
element = fixtureSync(`<div class="wrapper"><${tag}></${tag}></div>`);
node = element.querySelector(tag).shadowRoot;
expect(getClosestElement('.wrapper', node)).to.equal(element);
node = element.querySelector(tag).shadowRoot.querySelector('.parent');
expect(getClosestElement('.wrapper', node)).to.equal(element);
});
it('should return null when no closest element is found', () => {
element = fixtureSync(`<${tag}></${tag}>`);
node = element.shadowRoot.querySelector('.child');
expect(getClosestElement('.not-existing-class', node)).to.be.null;
});
it('should return the passed element if it matches the selector', () => {
element = fixtureSync(`<${tag}></${tag}>`);
node = element.shadowRoot.querySelector('.child');
expect(getClosestElement('.child', node)).to.equal(node);
});
});
describe('addValueToAttribute', () => {
let element;
beforeEach(() => {
element = document.createElement('div');
});
it('should add a value to an attribute', () => {
addValueToAttribute(element, 'aria-labelledby', 'label-id');
expect(element.getAttribute('aria-labelledby')).to.equal('label-id');
addValueToAttribute(element, 'aria-labelledby', 'error-id');
expect(element.getAttribute('aria-labelledby')).to.equal('label-id error-id');
});
it('should not duplicate values in the attribute', () => {
addValueToAttribute(element, 'aria-labelledby', 'label-id');
addValueToAttribute(element, 'aria-labelledby', 'label-id');
expect(element.getAttribute('aria-labelledby')).to.equal('label-id');
});
});
describe('removeValueFromAttribute', () => {
let element;
beforeEach(() => {
element = document.createElement('div');
element.setAttribute('aria-labelledby', 'label-id error-id');
});
it('should remove a value from an attribute', () => {
removeValueFromAttribute(element, 'aria-labelledby', 'error-id');
expect(element.getAttribute('aria-labelledby')).to.equal('label-id');
removeValueFromAttribute(element, 'aria-labelledby', 'label-id');
expect(element.hasAttribute('aria-labelledby')).to.be.false;
});
});
describe('isEmptyTextNode', () => {
let node;
beforeEach(() => {
node = document.createTextNode('');
});
it('should return true when node has empty text content', () => {
expect(isEmptyTextNode(node)).to.be.true;
});
it('should return true when node has whitespace text content', () => {
node.textContent = ' ';
expect(isEmptyTextNode(node)).to.be.true;
});
it('should return false when node has non-empty text content', () => {
node.textContent = '0';
expect(isEmptyTextNode(node)).to.be.false;
});
});
describe('getFlattenedElements', () => {
let foo, bar, baz;
beforeEach(() => {
foo = document.createElement('div');
foo.attachShadow({ mode: 'open' });
foo.shadowRoot.innerHTML = '<slot></slot>';
bar = document.createElement('div');
bar.attachShadow({ mode: 'open' });
bar.shadowRoot.innerHTML = '<span>A</span><slot></slot><span>B</span>';
baz = document.createElement('span');
baz.textContent = 'C';
document.body.appendChild(foo);
bar.appendChild(baz);
foo.appendChild(bar);
});
afterEach(() => {
document.body.removeChild(foo);
});
it('should return flattened elements for the element itself', () => {
expect(getFlattenedElements(foo)).to.eql([foo, bar, baz]);
expect(getFlattenedElements(bar)).to.eql([bar, baz]);
});
it('should return flatted elements for the parent slot element', () => {
const slot = foo.shadowRoot.querySelector('slot');
expect(getFlattenedElements(slot)).to.eql([bar, baz]);
});
it('should return flatted elements for the child slot element', () => {
const slot = bar.shadowRoot.querySelector('slot');
expect(getFlattenedElements(slot)).to.eql([baz]);
});
});
});