-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
80 lines (65 loc) · 2.9 KB
/
index.spec.js
File metadata and controls
80 lines (65 loc) · 2.9 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
import { createElement, isNodeFromTemplate } from 'lwc';
import Test from 'x/test';
import { jasmineSpyOn as spyOn } from '../../../helpers/jasmine.js';
function testNonNodes(type, obj) {
it(`should return false if the passed object if a ${type}`, () => {
expect(isNodeFromTemplate(obj)).toBe(false);
});
}
testNonNodes('null', null);
testNonNodes('undefined', undefined);
testNonNodes('Object', {});
it('should return false for nodes not rendered in LWC', () => {
const div = document.createElement('div');
expect(isNodeFromTemplate(div)).toBe(false);
});
it('should return false on host element', () => {
const elm = createElement('x-test', { is: Test });
document.body.appendChild(elm);
expect(isNodeFromTemplate(elm)).toBe(false);
});
it('should return false on the shadow root', () => {
const elm = createElement('x-test', { is: Test });
document.body.appendChild(elm);
expect(isNodeFromTemplate(elm.shadowRoot)).toBe(false);
});
it('should return true on elements rendered from the template', () => {
const elm = createElement('x-test', { is: Test });
document.body.appendChild(elm);
const div = elm.shadowRoot.querySelector('div');
expect(isNodeFromTemplate(div)).toBe(true);
});
it('should return true on elements manually inserted in the DOM inside an element with lwc:dom="manual"', () => {
const elm = createElement('x-test', { is: Test });
document.body.appendChild(elm);
const div = document.createElement('div');
elm.shadowRoot.querySelector('div').appendChild(div);
// TODO [#1253]: optimization to synchronously adopt new child nodes added
// to this elm, we can do that by patching the most common operations
// on the node itself
if (!process.env.NATIVE_SHADOW) {
expect(isNodeFromTemplate(div)).toBe(false); // it is false sync because MO hasn't pick up the element yet
}
return new Promise((resolve) => {
setTimeout(resolve);
}).then(() => {
expect(isNodeFromTemplate(div)).toBe(true); // it is true async because MO has already pick up the element
});
});
// TODO [#1252]: old behavior that is still used by some pieces of the platform
// if isNodeFromTemplate() returns true, locker will prevent traversing to such elements from document
it.skipIf(process.env.NATIVE_SHADOW)(
'should return false on elements manually inserted in the DOM inside an element NOT marked with lwc:dom="manual"',
() => {
const elm = createElement('x-test', { is: Test });
document.body.appendChild(elm);
spyOn(console, 'warn'); // ignore warning about manipulating node without lwc:dom="manual"
const span = document.createElement('span');
elm.shadowRoot.querySelector('h2').appendChild(span);
return new Promise((resolve) => {
setTimeout(resolve);
}).then(() => {
expect(isNodeFromTemplate(span)).toBe(false);
});
}
);