-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
211 lines (156 loc) · 8.39 KB
/
index.spec.js
File metadata and controls
211 lines (156 loc) · 8.39 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
import { createElement } from 'lwc';
import Slotted from 'x/slotted';
import Nested from 'x/nested';
import NestedFallback from 'x/nestedFallback';
import TestWithDiv from 'x/testWithDiv';
import { jasmineSpyOn as spyOn } from '../../../helpers/jasmine.js';
describe('Element.querySelector', () => {
it('should return null if no Element match', () => {
const elm = createElement('x-slotted', { is: Slotted });
document.body.appendChild(elm);
expect(elm.querySelector('.foo')).toBe(null);
expect(elm.shadowRoot.querySelector('.foo')).toBe(null);
expect(elm.shadowRoot.firstChild.querySelector('.foo')).toBe(null);
});
it('should return the first matching element', () => {
const elm = createElement('x-slotted', { is: Slotted });
document.body.appendChild(elm);
const slotted1 = elm.shadowRoot.firstChild.firstChild;
expect(elm.shadowRoot.querySelector('.slotted')).toBe(slotted1);
});
it('should return matching elements when are manually inserted in same shadow', () => {
const elm = createElement('x-test-with-div', { is: TestWithDiv });
document.body.appendChild(elm);
const divInsideShadow = elm.shadowRoot.querySelector('div');
const manuallyInsertedElement = document.createElement('span');
spyOn(console, 'warn'); // ignore warning about manipulating node without lwc:dom="manual"
divInsideShadow.appendChild(manuallyInsertedElement);
const qsResult = divInsideShadow.querySelector('span');
expect(qsResult).toBe(manuallyInsertedElement);
});
});
describe('Element.querySelectorAll', () => {
it('should return an empty NodeList if no Elements match', () => {
const elm = createElement('x-slotted', { is: Slotted });
document.body.appendChild(elm);
const hostQuery = elm.querySelectorAll('.foo');
expect(hostQuery instanceof NodeList).toBe(true);
expect(hostQuery.length).toBe(0);
const shadowRootQuery = elm.shadowRoot.querySelectorAll('.foo');
expect(shadowRootQuery instanceof NodeList).toBe(true);
expect(shadowRootQuery.length).toBe(0);
const shadowTreeQuery = elm.shadowRoot.firstChild.querySelectorAll('.foo');
expect(shadowTreeQuery instanceof NodeList).toBe(true);
expect(shadowTreeQuery.length).toBe(0);
});
it('should return the all the matching elements', () => {
const elm = createElement('x-slotted', { is: Slotted });
document.body.appendChild(elm);
const slotted1 = elm.shadowRoot.firstChild.firstChild;
const slotted2 = elm.shadowRoot.firstChild.lastChild;
const nodeList = elm.shadowRoot.querySelectorAll('.slotted');
expect(nodeList.length).toBe(2);
expect(nodeList[0]).toBe(slotted1);
expect(nodeList[1]).toBe(slotted2);
});
it('should return matching elements when are manually inserted in same shadow', () => {
const elm = createElement('x-test-with-div', { is: TestWithDiv });
document.body.appendChild(elm);
const divInsideShadow = elm.shadowRoot.querySelector('div');
const manuallyInsertedElement = document.createElement('span');
spyOn(console, 'warn'); // ignore warning about manipulating node without lwc:dom="manual"
divInsideShadow.appendChild(manuallyInsertedElement);
const qsResult = divInsideShadow.querySelectorAll('span');
expect(qsResult.length).toBe(1);
expect(qsResult[0]).toBe(manuallyInsertedElement);
});
});
describe('Element.getElementsByTagName', () => {
it('should return matching elements when are manually inserted in same shadow', () => {
const elm = createElement('x-test-with-div', { is: TestWithDiv });
document.body.appendChild(elm);
const divInsideShadow = elm.shadowRoot.querySelector('div');
const manuallyInsertedElement = document.createElement('span');
spyOn(console, 'warn'); // ignore warning about manipulating node without lwc:dom="manual"
divInsideShadow.appendChild(manuallyInsertedElement);
const qsResult = divInsideShadow.getElementsByTagName('span');
expect(qsResult.length).toBe(1);
expect(qsResult[0]).toBe(manuallyInsertedElement);
});
});
describe('Element.getElementsByClassName', () => {
it('should return matching elements when are manually inserted in same shadow', () => {
const elm = createElement('x-test-with-div', { is: TestWithDiv });
document.body.appendChild(elm);
const divInsideShadow = elm.shadowRoot.querySelector('div');
const manuallyInsertedElement = document.createElement('span');
manuallyInsertedElement.className = 'test-class';
spyOn(console, 'warn'); // ignore warning about manipulating node without lwc:dom="manual"
divInsideShadow.appendChild(manuallyInsertedElement);
const qsResult = divInsideShadow.getElementsByClassName('test-class');
expect(qsResult.length).toBe(1);
expect(qsResult[0]).toBe(manuallyInsertedElement);
});
});
it('should not match on elements in a different shadow tree', () => {
const elm = createElement('x-slotted', { is: Slotted });
document.body.appendChild(elm);
expect(elm.querySelector('x-slot')).toBe(null);
expect([...elm.querySelectorAll('x-slot')]).toEqual([]);
expect(elm.shadowRoot.querySelector('slot')).toBe(null);
expect([...elm.shadowRoot.querySelectorAll('slot')]).toEqual([]);
});
it('should match on elements in the same shadow tree', () => {
const elm = createElement('x-slotted', { is: Slotted });
document.body.appendChild(elm);
const slotHost = elm.shadowRoot.firstChild;
const slotted1 = elm.shadowRoot.firstChild.firstChild;
const slotted2 = elm.shadowRoot.firstChild.lastChild;
expect(elm.shadowRoot.querySelector('x-slot')).toBe(slotHost);
expect([...elm.shadowRoot.querySelectorAll('x-slot')]).toEqual([slotHost]);
expect(elm.shadowRoot.querySelector('.slotted')).toBe(slotted1);
expect([...elm.shadowRoot.querySelectorAll('.slotted')]).toEqual([slotted1, slotted2]);
expect(slotHost.querySelector('.slotted')).toBe(slotted1);
expect([...slotHost.querySelectorAll('.slotted')]).toEqual([slotted1, slotted2]);
});
it('should not match on slotted content', () => {
const elm = createElement('x-slotted', { is: Slotted });
document.body.appendChild(elm);
const slotHost = elm.shadowRoot.firstChild;
const slot = slotHost.shadowRoot.firstChild.firstChild;
expect(slotHost.shadowRoot.querySelector('.slotted')).toBe(null);
expect([...slotHost.shadowRoot.querySelectorAll('.slotted')]).toEqual([]);
expect(slot.querySelector('.slotted')).toBe(null);
expect([...slot.querySelectorAll('.slotted')]).toEqual([]);
});
it('should support chaining querySelectors', () => {
const elm = createElement('x-slotted', { is: Slotted });
document.body.appendChild(elm);
const slotted = elm.shadowRoot.firstChild.firstChild;
expect(elm.shadowRoot.querySelector('x-slot').querySelector('.slotted')).toBe(slotted);
});
it('should support nested slots - multi-level', () => {
const elm = createElement('x-nested', { is: Nested });
document.body.appendChild(elm);
const slotHost = elm.shadowRoot.firstChild;
const slotsInSlotsHost = slotHost.firstChild;
const slotted = slotsInSlotsHost.firstChild;
expect(elm.shadowRoot.querySelector('.slotted')).toBe(slotted);
expect(slotHost.querySelector('.slotted')).toBe(slotted);
expect(slotsInSlotsHost.querySelector('.slotted')).toBe(slotted);
expect(elm.querySelector('.slotted')).toBe(null);
expect(slotHost.shadowRoot.querySelector('.slotted')).toBe(null);
expect(slotsInSlotsHost.shadowRoot.querySelector('.slotted')).toBe(null);
});
it('should support nested slots - slotted fallback content', () => {
const elm = createElement('x-nested-fallback', { is: NestedFallback });
document.body.appendChild(elm);
const slotHost = elm.shadowRoot.firstChild;
const slot = slotHost.firstChild;
const slotFallback = slot.firstChild;
expect(elm.shadowRoot.querySelector('.slotted-fallback')).toBe(slotFallback);
expect(slotHost.querySelector('.slotted-fallback')).toBe(slotFallback);
expect(slot.querySelector('.slotted-fallback')).toBe(slotFallback);
expect(elm.querySelector('.slotted-fallback')).toBe(null);
expect(slotHost.shadowRoot.querySelector('.slotted-fallback')).toBe(null);
});