-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathhtmlDomUtils.spec.ts
More file actions
220 lines (189 loc) · 7.22 KB
/
htmlDomUtils.spec.ts
File metadata and controls
220 lines (189 loc) · 7.22 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { appendElement, appendText } from '../../test'
import {
isTextNode,
isCommentNode,
isElementNode,
isNodeShadowRoot,
getParentNode,
getParentElement,
isNodeShadowHost,
forEachChildNodes,
hasChildNodes,
} from './htmlDomUtils'
describe('isTextNode', () => {
const parameters: Array<[Node, boolean]> = [
[document.createTextNode('hello'), true],
[document.createElement('div'), false],
[document.body, false],
[document.createComment('hello'), false],
['hello' as unknown as Node, false],
]
parameters.forEach(([element, result]) => {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
it(`should return ${String(result)} for "${String(element)}"`, () => {
expect(isTextNode(element)).toBe(result)
})
})
})
describe('isCommentNode', () => {
const parameters: Array<[Node, boolean]> = [
[document.createComment('hello'), true],
[document.createTextNode('hello'), false],
[document.createElement('div'), false],
[document.body, false],
['hello' as unknown as Node, false],
]
parameters.forEach(([element, result]) => {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
it(`should return ${String(result)} for "${String(element)}"`, () => {
expect(isCommentNode(element)).toBe(result)
})
})
})
describe('isElementNode', () => {
const parameters: Array<[Node, boolean]> = [
[document.createElement('div'), true],
[document.body, true],
[document.createTextNode('hello'), false],
[document.createComment('hello'), false],
['hello' as unknown as Node, false],
]
parameters.forEach(([element, result]) => {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
it(`should return ${String(result)} for "${String(element)}"`, () => {
expect(isElementNode(element)).toBe(result)
})
})
})
describe('isShadowRoot', () => {
const notShadowDomNodes: Node[] = [
document,
document.head,
document.body,
document.createElement('div'),
document.createTextNode('hello'),
document.createComment('hello'),
]
notShadowDomNodes.forEach((element) => {
it(`should return false for "${String(element.nodeName)}"`, () => {
expect(isNodeShadowRoot(element)).toBe(false)
})
})
it('should return true for shadow root but not its host', () => {
const parent = document.createElement('div')
const shadowRoot = parent.attachShadow({ mode: 'open' })
expect(isNodeShadowRoot(parent)).toBe(false)
expect(isNodeShadowRoot(shadowRoot)).toBe(true)
})
it('should return false for a[href] despite it has a host property', () => {
const link = document.createElement('a')
link.setAttribute('href', 'http://localhost/some/path')
expect(link.host).toBeTruthy()
expect(isNodeShadowRoot(link)).toBe(false)
})
it('should return false for a form with an input[name="host"] despite it has a host property', () => {
const form = document.createElement('form')
const input = document.createElement('input')
input.setAttribute('name', 'host')
form.appendChild(input)
expect(isNodeShadowRoot(form)).toBe(false)
})
})
describe('isShadowHost', () => {
const host = document.createElement('div')
host.attachShadow({ mode: 'open' })
// Edge 18 and before doesn't support shadow dom, so `Element#shadowRoot` is undefined.
const oldEdgeElement = document.createElement('div')
Object.defineProperty(oldEdgeElement, 'shadowRoot', { value: undefined })
const parameters: Array<[Node, boolean]> = [
[host, true],
[host.shadowRoot!, false],
[document.body, false],
[document.createTextNode('hello'), false],
[document.createComment('hello'), false],
[oldEdgeElement, false],
]
parameters.forEach(([element, result]) => {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
it(`should return ${String(result)} for "${String(element)}"`, () => {
expect(isNodeShadowHost(element)).toBe(result)
})
})
})
describe('hasChildNode', () => {
it('should return `true` if the element has a direct child node', () => {
expect(hasChildNodes(appendElement('<div>foo</div>'))).toBe(true)
expect(hasChildNodes(appendElement('<div><hr /></div>'))).toBe(true)
expect(hasChildNodes(appendElement('<div><!-- --></div>'))).toBe(true)
})
it('should return `true` if the element is a shadow host', () => {
const container = appendElement('<div></div>')
container.attachShadow({ mode: 'open' })
expect(hasChildNodes(container)).toBe(true)
})
it('should return `false` otherwise', () => {
expect(hasChildNodes(appendElement('<div></div>'))).toBe(false)
expect(hasChildNodes(appendText('foo'))).toBe(false)
})
})
describe('forEachChildNodes', () => {
it('should iterate over the direct children for a normal node', () => {
const container = appendElement(`
<div>toto<span></span><!-- --></div>
`)
const spy = jasmine.createSpy()
forEachChildNodes(container, spy)
expect(spy).toHaveBeenCalledTimes(3)
})
it('should iterate over the the shadow root for a node that is a host', () => {
const container = appendElement('<div></div>')
const shadowRoot = container.attachShadow({ mode: 'open' })
const spy = jasmine.createSpy()
forEachChildNodes(container, spy)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.calls.argsFor(0)[0]).toBe(shadowRoot)
})
it('should iterate over the the shadow root and direct children for a node that is a host', () => {
const container = appendElement('<div><span></span></div>')
const shadowRoot = container.attachShadow({ mode: 'open' })
const spy = jasmine.createSpy()
forEachChildNodes(container, spy)
expect(spy).toHaveBeenCalledTimes(2)
expect(spy.calls.argsFor(0)[0]).toBe(container.childNodes[0])
expect(spy.calls.argsFor(1)[0]).toBe(shadowRoot)
})
})
describe('getParentNode', () => {
const orphanDiv = document.createElement('div')
const parentWithShadowRoot = document.createElement('div')
const shadowRoot = parentWithShadowRoot.attachShadow({ mode: 'open' })
const parentWithoutShadowRoot = document.createElement('div')
const child = document.createElement('span')
parentWithoutShadowRoot.appendChild(child)
const parameters: Array<[string, Node, Node | null]> = [
['return null if without parent', orphanDiv, null],
['return the host for a shadow root', shadowRoot, parentWithShadowRoot],
['return the parent for normal child', child, parentWithoutShadowRoot],
]
parameters.forEach(([label, element, result]) => {
it(`should ${label}`, () => {
expect(getParentNode(element)).toBe(result)
})
})
})
describe('getParentElement', () => {
it('should return parentElement for normal element', () => {
const parent = document.createElement('div')
const child = document.createElement('span')
parent.appendChild(child)
expect(getParentElement(child)).toBe(parent)
})
it('should return the shadow host for element inside shadow DOM', () => {
const host = document.createElement('div')
const shadowRoot = host.attachShadow({ mode: 'open' })
const button = document.createElement('button')
shadowRoot.appendChild(button)
expect(button.parentElement).toBe(null)
expect(getParentElement(button)).toBe(host)
})
})