-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathMutationObserver.spec.js
More file actions
481 lines (443 loc) · 25.4 KB
/
MutationObserver.spec.js
File metadata and controls
481 lines (443 loc) · 25.4 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import { createElement } from 'lwc';
import XParent from 'x/parent';
import XSlottedChild from 'x/slottedChild';
import XNestedSlotContainer from 'x/nestedSlotContainer';
import XTemplateMutations from 'x/templateMutations';
import { jasmine } from '../../../helpers/jasmine.js';
const observerConfig = { childList: true, subtree: true };
function waitForMutationObservedToBeInvoked() {
return Promise.resolve();
}
describe('MutationObserver is synthetic shadow dom aware.', () => {
describe('mutations do not leak shadow boundary', () => {
let globalObserverSpy;
let container;
beforeEach(() => {
globalObserverSpy = jasmine.createSpy();
const globalObserver = new MutationObserver(globalObserverSpy);
container = document.createElement('div');
document.body.appendChild(container);
// Attach to container node instead of document or body to not affect other tests
globalObserver.observe(container, observerConfig);
});
it('global observer should be called 1 time, when the host element is attached to document', async () => {
// Prepare body for new lwc element
const host = createElement('x-parent', { is: XParent });
const container = document.createElement('div');
document.body.appendChild(container);
const callback = function (actualMutationRecords, actualObserver) {
expect(actualObserver).toBe(containerObserver);
expect(actualMutationRecords.length).toBe(1);
expect(actualMutationRecords[0].target).toBe(container);
expect(actualMutationRecords[0].addedNodes.length).toBe(1);
expect(actualMutationRecords[0].addedNodes[0].tagName).toBe('X-PARENT');
};
const containerObserver = new MutationObserver(callback);
// Attach to container node instead of document or body to not affect other tests
containerObserver.observe(container, observerConfig);
container.appendChild(host);
await waitForMutationObservedToBeInvoked();
});
it('global observer is not called when mutations occur inside shadow tree', () => {
const host = createElement('x-parent', { is: XParent });
container.appendChild(host);
return waitForMutationObservedToBeInvoked()
.then(() => {
// The first call will be when x-parent is appended to the container
globalObserverSpy.calls.reset();
// Mutate the shadow tree of x-parent
const parentDiv = host.shadowRoot.querySelector('div');
parentDiv.appendChild(document.createElement('p'));
return waitForMutationObservedToBeInvoked().then(() => {
expect(globalObserverSpy).not.toHaveBeenCalled();
});
})
.then(() => {
// Mutate the shadow tree of x-child
const childElm = host.shadowRoot.querySelector('x-child');
const childDiv = childElm.shadowRoot.querySelector('div');
childDiv.appendChild(document.createElement('p'));
return waitForMutationObservedToBeInvoked().then(() => {
expect(globalObserverSpy).not.toHaveBeenCalled();
});
});
});
it('global observer is not called when mutations occur in slotted content', () => {
const parent = createElement('x-slotted-child', { is: XSlottedChild });
container.appendChild(parent);
return waitForMutationObservedToBeInvoked().then(() => {
// The first call will be when x-slotted-child is appended to the container
globalObserverSpy.calls.reset();
const slottedDiv = parent.shadowRoot.querySelector('div.manual');
slottedDiv.appendChild(document.createElement('p'));
return waitForMutationObservedToBeInvoked().then(() => {
expect(globalObserverSpy).not.toHaveBeenCalled();
});
});
});
it('should invoke observer on parent when slotted content is altered', () => {
const parent = createElement('x-slotted-child', { is: XSlottedChild });
container.appendChild(parent);
const slottedDiv = parent.shadowRoot.querySelector('div.manual');
// observer on parent element's shadowRoot will be notified
// because the slot content being mutated belongs to that shadow tree
const callback = function (actualMutationRecords, actualObserver) {
expect(actualObserver).toBe(parentSRObserver);
expect(actualMutationRecords.length).toBe(1);
expect(actualMutationRecords[0].target).toBe(slottedDiv);
expect(actualMutationRecords[0].addedNodes.length).toBe(1);
expect(actualMutationRecords[0].addedNodes[0].tagName).toBe('P');
};
// Start observing the parent and child shadow trees
const parentHostSpy = jasmine.createSpy();
new MutationObserver(parentHostSpy).observe(parent, observerConfig);
const parentSRObserver = new MutationObserver(callback);
parentSRObserver.observe(parent.shadowRoot, observerConfig);
const childSRSpy = jasmine.createSpy();
new MutationObserver(childSRSpy).observe(
parent.shadowRoot.querySelector('x-child').shadowRoot,
observerConfig
);
slottedDiv.appendChild(document.createElement('p'));
return waitForMutationObservedToBeInvoked().then(() => {
// observer on parent host element should not see mutation
expect(parentHostSpy).not.toHaveBeenCalled();
// observer on the slot receiver should not see mutation
expect(childSRSpy).not.toHaveBeenCalled();
});
});
it('should invoke observer on slot content owner', () => {
const parent = createElement('x-nested-slot-container', { is: XNestedSlotContainer });
container.appendChild(parent);
const slottedDiv = parent.shadowRoot.querySelector('div.manual');
// observer on parent element's shadowRoot will be notified
// because the slot content being mutated belongs to that shadow tree
const callback = function (actualMutationRecords, actualObserver) {
expect(actualObserver).toBe(parentSRObserver);
expect(actualMutationRecords.length).toBe(1);
expect(actualMutationRecords[0].target).toBe(slottedDiv);
expect(actualMutationRecords[0].addedNodes.length).toBe(1);
expect(actualMutationRecords[0].addedNodes[0].tagName).toBe('P');
};
// Start observing the parent and child shadow trees
// x-nested-slot-container
const parentSRObserver = new MutationObserver(callback);
parentSRObserver.observe(parent.shadowRoot, observerConfig);
// x-nested-slot
const childSRSpy = jasmine.createSpy();
const child = parent.shadowRoot.querySelector('x-nested-slot');
new MutationObserver(childSRSpy).observe(child.shadowRoot, observerConfig);
// x-child
const grandChildSRSpy = jasmine.createSpy();
const grandChild = child.shadowRoot.querySelector('x-child');
new MutationObserver(grandChildSRSpy).observe(grandChild.shadowRoot, observerConfig);
// x-child > slot
const grandChildSlotSpy = jasmine.createSpy();
const grandChildSlot = grandChild.shadowRoot.querySelector('slot');
new MutationObserver(grandChildSlotSpy).observe(grandChildSlot, observerConfig);
slottedDiv.appendChild(document.createElement('p'));
// Skip a macrotask to allow for observers to be invoked, if any
return waitForMutationObservedToBeInvoked().then(() => {
// observers on the slot receiver should not see mutation
expect(childSRSpy).not.toHaveBeenCalled();
expect(grandChildSRSpy).not.toHaveBeenCalled();
expect(grandChildSlotSpy).not.toHaveBeenCalled();
});
});
it('parent observer not invoked when mutations occur in a nested lwc', () => {
const parent = createElement('x-parent', { is: XParent });
container.appendChild(parent);
// Start observing the parent and child shadow trees
const parentSpy = jasmine.createSpy();
new MutationObserver(parentSpy).observe(parent.shadowRoot, observerConfig);
const childElm = parent.shadowRoot.querySelector('x-child');
const childDiv = childElm.shadowRoot.querySelector('div');
childDiv.appendChild(document.createElement('p'));
// Skip a macrotask to allow for observers to be invoked, if any
return waitForMutationObservedToBeInvoked().then(() => {
expect(parentSpy).not.toHaveBeenCalled();
});
});
});
describe('should handle mutations in shadow tree', () => {
let container;
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
describe.skipIf(process.env.FORCE_NATIVE_SHADOW_MODE_FOR_TEST)('MutationObserver', () => {
it('should invoke observer with correct MutationRecords when adding child nodes using innerHTML', async () => {
const parent = createElement('x-parent', { is: XParent });
container.appendChild(parent);
const parentDiv = parent.shadowRoot.querySelector('div');
const observer = new MutationObserver((actualMutationRecords, actualObserver) => {
expect(actualObserver).toBe(observer);
expect(actualMutationRecords.length).toBe(1);
expect(actualMutationRecords[0].target).toBe(parentDiv);
expect(actualMutationRecords[0].addedNodes.length).toBe(2);
expect(actualMutationRecords[0].addedNodes[0].tagName).toBe('H3');
expect(actualMutationRecords[0].addedNodes[1].tagName).toBe('P');
});
observer.observe(parent.shadowRoot, observerConfig);
// Mutate the shadow tree of x-parent
parentDiv.innerHTML = `<h3></h3><p></p>`;
await waitForMutationObservedToBeInvoked();
});
it('should invoke observer with correct MutationRecords when adding child nodes using appendChild', () => {
const parent = createElement('x-parent', { is: XParent });
container.appendChild(parent);
return new Promise((resolve) => {
let observer;
const parentDiv = parent.shadowRoot.querySelector('div');
const callback = function (actualMutationRecords, actualObserver) {
expect(actualObserver).toBe(observer);
expect(actualMutationRecords.length).toBe(1);
expect(actualMutationRecords[0].target).toBe(parentDiv);
expect(actualMutationRecords[0].addedNodes.length).toBe(1);
expect(actualMutationRecords[0].addedNodes[0].tagName).toBe('P');
resolve();
};
observer = new MutationObserver(callback);
observer.observe(parent.shadowRoot, observerConfig);
// Mutate the shadow tree of x-parent
parentDiv.appendChild(document.createElement('p'));
}).then(() => {
const childElm = parent.shadowRoot.querySelector('x-child');
const childDiv = childElm.shadowRoot.querySelector('div');
const promise = new Promise((resolve) => {
const callback = function (actualMutationRecords) {
expect(actualMutationRecords.length).toBe(2);
expect(actualMutationRecords[0].target).toBe(childDiv);
expect(actualMutationRecords[0].addedNodes.length).toBe(1);
expect(actualMutationRecords[0].addedNodes[0].tagName).toBe('UL');
expect(actualMutationRecords[1].target).toBe(childDiv);
expect(actualMutationRecords[1].addedNodes.length).toBe(1);
expect(actualMutationRecords[1].addedNodes[0].tagName).toBe('OL');
resolve();
};
new MutationObserver(callback).observe(childElm.shadowRoot, observerConfig);
// Mutate the shadow tree of x-child
childDiv.appendChild(document.createElement('ul'));
childDiv.appendChild(document.createElement('ol'));
});
return promise;
});
});
it('should invoke observer with correct MutationRecords when removing child nodes using innerHTML', async () => {
const parent = createElement('x-parent', { is: XParent });
container.appendChild(parent);
const parentDiv = parent.shadowRoot.querySelector('div');
parentDiv.innerHTML = `<h3></h3><p></p>`;
let observer;
const callback = function (actualMutationRecords, actualObserver) {
expect(actualObserver).toBe(observer);
expect(actualMutationRecords.length).toBe(1);
expect(actualMutationRecords[0].target).toBe(parentDiv);
expect(actualMutationRecords[0].removedNodes.length).toBe(2);
const removedNodes = Array.prototype.slice.call(
actualMutationRecords[0].removedNodes,
0
);
// In IE11, the order of nodes removal is reverse. Sorting the records to make the result deterministic
removedNodes.sort((nodeA, nodeB) => {
return nodeA.tagName > nodeB.tagName ? 1 : -1;
});
expect(removedNodes[0].tagName).toBe('H3');
expect(removedNodes[1].tagName).toBe('P');
};
observer = new MutationObserver(callback);
observer.observe(parent.shadowRoot, observerConfig);
parentDiv.innerHTML = '';
await waitForMutationObservedToBeInvoked();
});
it('all observers of a given node are invoked', () => {
const parent = createElement('x-parent', { is: XParent });
container.appendChild(parent);
let firstObserverCallback;
let secondObserverCallback;
const promise1 = new Promise((resolve) => {
firstObserverCallback = resolve;
});
const promise2 = new Promise((resolve) => {
secondObserverCallback = resolve;
});
const parentDiv = parent.shadowRoot.querySelector('div');
const observer1 = new MutationObserver(function (
actualMutationRecords,
actualObserver
) {
expect(actualObserver).toBe(observer1);
expect(actualMutationRecords.length).toBe(1);
expect(actualMutationRecords[0].target).toBe(parentDiv);
expect(actualMutationRecords[0].addedNodes.length).toBe(1);
expect(actualMutationRecords[0].addedNodes[0].tagName).toBe('P');
firstObserverCallback();
});
observer1.observe(parent.shadowRoot, observerConfig);
const observer2 = new MutationObserver(function (
actualMutationRecords,
actualObserver
) {
expect(actualObserver).toBe(observer2);
expect(actualMutationRecords.length).toBe(1);
expect(actualMutationRecords[0].target).toBe(parentDiv);
expect(actualMutationRecords[0].addedNodes.length).toBe(1);
expect(actualMutationRecords[0].addedNodes[0].tagName).toBe('P');
secondObserverCallback();
});
observer2.observe(parent.shadowRoot, observerConfig);
// Mutate the shadow tree of x-parent
parentDiv.appendChild(document.createElement('p'));
return Promise.all([promise1, promise2]);
});
it('should not get notifications after disconnecting observer', () => {
const parent = createElement('x-parent', { is: XParent });
container.appendChild(parent);
const parentSRSpy = jasmine.createSpy();
const observer = new MutationObserver(parentSRSpy);
observer.observe(parent.shadowRoot, observerConfig);
const parentDiv = parent.shadowRoot.querySelector('div');
// Mutate the shadow tree of x-parent
parentDiv.appendChild(document.createElement('p'));
// Wait for a macro task
return waitForMutationObservedToBeInvoked().then(() => {
// Make sure the spy is getting called
expect(parentSRSpy).toHaveBeenCalledTimes(1);
parentSRSpy.calls.reset();
// disconnect and verify that spy does not get invoked after
observer.disconnect();
parentDiv.appendChild(document.createElement('ul'));
// Wait for a macro task
return waitForMutationObservedToBeInvoked().then(() => {
expect(parentSRSpy).not.toHaveBeenCalled();
});
});
});
it('should return expected records when takeRecords is invoked', () => {
const parent = createElement('x-parent', { is: XParent });
container.appendChild(parent);
const parentDiv = parent.shadowRoot.querySelector('div');
const observer = new MutationObserver(() => {});
observer.observe(parent.shadowRoot, observerConfig);
// Mutate the shadow tree of x-parent
parentDiv.appendChild(document.createElement('ul'));
parentDiv.appendChild(document.createElement('ol'));
const actualMutationRecords = observer.takeRecords();
expect(actualMutationRecords.length).toBe(2);
expect(actualMutationRecords[0].target).toBe(parentDiv);
expect(actualMutationRecords[0].addedNodes.length).toBe(1);
expect(actualMutationRecords[0].addedNodes[0].tagName).toBe('UL');
expect(actualMutationRecords[1].target).toBe(parentDiv);
expect(actualMutationRecords[1].addedNodes.length).toBe(1);
expect(actualMutationRecords[1].addedNodes[0].tagName).toBe('OL');
});
});
it('should retarget MutationRecord for mutations directly under shadowRoot - added nodes', () => {
const host = createElement('x-template-mutations', { is: XTemplateMutations });
container.appendChild(host);
const callback = function (actualMutationRecords, actualObserver) {
expect(actualObserver).toBe(shadowRootObserver);
expect(actualMutationRecords.length).toBe(1);
expect(actualMutationRecords[0].target).toBe(host.shadowRoot);
expect(actualMutationRecords[0].addedNodes.length).toBe(1);
expect(actualMutationRecords[0].addedNodes[0].tagName).toBe('DIV');
expect(actualMutationRecords[0].removedNodes.length).toBe(0);
expect(actualMutationRecords[0].type).toBe('childList');
};
const globalObserverSpy = jasmine.createSpy();
const globalObserver = new MutationObserver(globalObserverSpy);
globalObserver.observe(container, observerConfig);
const hostSpy = jasmine.createSpy();
new MutationObserver(hostSpy).observe(host, observerConfig);
const shadowRootObserver = new MutationObserver(callback);
shadowRootObserver.observe(host.shadowRoot, observerConfig);
// Trigger a mutation directly under the shadowRoot
host.addNode = true;
return waitForMutationObservedToBeInvoked().then(() => {
expect(globalObserverSpy).not.toHaveBeenCalled();
expect(hostSpy).not.toHaveBeenCalled();
});
});
it('should retarget MutationRecord for mutations directly under shadowRoot - removed nodes', () => {
const host = createElement('x-template-mutations', { is: XTemplateMutations });
container.appendChild(host);
const callback = function (actualMutationRecords, actualObserver) {
expect(actualObserver).toBe(shadowRootObserver);
expect(actualMutationRecords.length).toBe(1);
expect(actualMutationRecords[0].target).toBe(host.shadowRoot);
expect(actualMutationRecords[0].addedNodes.length).toBe(0);
expect(actualMutationRecords[0].removedNodes.length).toBe(1);
expect(actualMutationRecords[0].removedNodes[0].tagName).toBe('DIV');
};
const globalObserverSpy = jasmine.createSpy();
const globalObserver = new MutationObserver(globalObserverSpy);
globalObserver.observe(container, observerConfig);
const hostSpy = jasmine.createSpy();
new MutationObserver(hostSpy).observe(host, observerConfig);
const shadowRootObserver = new MutationObserver(callback);
shadowRootObserver.observe(host.shadowRoot, observerConfig);
// Trigger a mutation directly under the shadowRoot
host.hideNode = true;
return waitForMutationObservedToBeInvoked().then(() => {
expect(globalObserverSpy).not.toHaveBeenCalled();
expect(hostSpy).not.toHaveBeenCalled();
});
});
});
describe.skipIf(process.env.NATIVE_SHADOW)(
'References to mutation observers are not leaked',
() => {
let container;
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
it('should not leak after disconnect', () => {
const node = document.createElement('div');
container.appendChild(node);
const observer = new MutationObserver(() => {});
observer.observe(node, observerConfig);
expect(node.$$lwcNodeObservers$$.length).toBe(1);
observer.disconnect();
expect(node.$$lwcNodeObservers$$.length).toBe(0);
});
it('should not leak after disconnect - multiple nodes', () => {
const node1 = document.createElement('div');
const node2 = document.createElement('div');
container.appendChild(node1);
container.appendChild(node2);
const observer = new MutationObserver(() => {});
observer.observe(node1, observerConfig);
observer.observe(node2, observerConfig);
expect(node1.$$lwcNodeObservers$$.length).toBe(1);
expect(node2.$$lwcNodeObservers$$.length).toBe(1);
observer.disconnect();
expect(node1.$$lwcNodeObservers$$.length).toBe(0);
expect(node2.$$lwcNodeObservers$$.length).toBe(0);
});
it('should not leak after disconnect - multiple observers', () => {
const node = document.createElement('div');
container.appendChild(node);
const observer1 = new MutationObserver(() => {});
const observer2 = new MutationObserver(() => {});
observer1.observe(node, observerConfig);
expect(node.$$lwcNodeObservers$$.length).toBe(1);
observer2.observe(node, observerConfig);
expect(node.$$lwcNodeObservers$$.length).toBe(2);
observer1.disconnect();
expect(node.$$lwcNodeObservers$$.length).toBe(1);
observer2.disconnect();
expect(node.$$lwcNodeObservers$$.length).toBe(0);
});
it('should not leak after disconnect - duplicate observe()s', () => {
const node = document.createElement('div');
container.appendChild(node);
const observer = new MutationObserver(() => {});
observer.observe(node, observerConfig);
observer.observe(node, observerConfig);
observer.disconnect();
expect(node.$$lwcNodeObservers$$.length).toBe(0);
});
}
);
});