-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
162 lines (126 loc) · 5.83 KB
/
index.spec.js
File metadata and controls
162 lines (126 loc) · 5.83 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
import { createElement } from 'lwc';
import withLwcDomManual from 'x/withLwcDomManual';
import withoutLwcDomManual from 'x/withoutLwcDomManual';
import SvgWithLwcDomManual from 'x/svgWithLwcDomManual';
import { jasmineSpyOn as spyOn } from '../../../helpers/jasmine.js';
function waitForStyleToBeApplied() {
return Promise.resolve();
}
afterEach(() => {
window.__lwcResetGlobalStylesheets();
});
describe('dom mutation without the lwc:dom="manual" directive', () => {
function testErrorOnDomMutation(method, fn) {
it(`should log a warning when calling ${method} on an element without the lwc:dom="manual" directive only in synthetic mode`, () => {
const root = createElement('x-without-lwc-dom-manual', { is: withoutLwcDomManual });
document.body.appendChild(root);
const elm = root.shadowRoot.querySelector('div');
const mutate = () => fn(elm);
const warning = new RegExp(
`\\[LWC warn\\]: The \`${method}\` method is available only on elements that use the \`lwc:dom="manual"\` directive.`
);
if (process.env.NATIVE_SHADOW) {
expect(mutate).not.toLogWarningDev(warning);
} else {
expect(mutate).toLogWarningDev(warning);
}
});
}
testErrorOnDomMutation('appendChild', (elm) => {
const child = document.createElement('div');
elm.appendChild(child);
});
testErrorOnDomMutation('insertBefore', (elm) => {
const child = document.createElement('div');
const span = elm.firstElementChild;
elm.insertBefore(child, span);
});
testErrorOnDomMutation('removeChild', (elm) => {
const span = elm.firstElementChild;
elm.removeChild(span);
});
testErrorOnDomMutation('replaceChild', (elm) => {
const child = document.createElement('div');
const span = elm.firstElementChild;
elm.replaceChild(child, span);
});
});
describe('dom mutation with the lwc:dom="manual" directive', () => {
function testAllowDomMutationWithLwcDomDirective(method, fn) {
it(`should not log an error when calling ${method} on an element with the lwc:dom="manual" directive`, () => {
const root = createElement('x-with-lwc-dom-manual', { is: withLwcDomManual });
document.body.appendChild(root);
spyOn(console, 'error');
const elm = root.shadowRoot.querySelector('div');
fn(elm);
/* eslint-disable-next-line no-console */
expect(console.error).not.toHaveBeenCalled();
});
}
testAllowDomMutationWithLwcDomDirective('appendChild', (elm) => {
const child = document.createElement('div');
elm.appendChild(child);
});
testAllowDomMutationWithLwcDomDirective('innerHTML', (elm) => {
elm.innerHTML = `<div></div>`;
});
it('#874 - should not warn when removing a node previously inserted in an element with the lwc:dom="manual" directive', () => {
const root = createElement('x-test', { is: withLwcDomManual });
document.body.appendChild(root);
spyOn(console, 'error');
const elm = root.shadowRoot.querySelector('div');
const child = document.createElement('div');
elm.appendChild(child);
elm.removeChild(child);
/* eslint-disable-next-line no-console */
expect(console.error).not.toHaveBeenCalled();
});
it('#879 - should not throw when a mutated child was removed sync after being updated', () => {
const root = createElement('x-test', { is: withLwcDomManual });
document.body.appendChild(root);
spyOn(console, 'error');
const elm = root.shadowRoot.querySelector('div');
const div = document.createElement('div');
div.innerHTML = '<span>span</span>';
const span = div.querySelector('span');
elm.appendChild(div);
span.textContent = '';
span.parentNode.removeChild(span);
/* eslint-disable-next-line no-console */
expect(console.error).not.toHaveBeenCalled();
});
});
describe('adopt node in the shadow', () => {
it('should returns the return the inserted elements when querying using querySelector', () => {
const root = createElement('x-test', { is: withLwcDomManual });
document.body.appendChild(root);
const elm = root.shadowRoot.querySelector('div');
elm.innerHTML = '<div class="outer"><div class="inner"></div></div>';
const outer = root.shadowRoot.querySelector('.outer');
expect(outer).not.toBe(null);
expect(outer.parentElement).toBe(elm);
const inner = root.shadowRoot.querySelector('.inner');
expect(inner).not.toBe(null);
expect(inner.parentElement.parentElement).toBe(elm);
});
it('should apply the component styles to inserted elements', () => {
const root = createElement('x-test', { is: withLwcDomManual });
document.body.appendChild(root);
const elm = root.shadowRoot.querySelector('div');
elm.innerHTML = '<div class="foo"></div>';
return waitForStyleToBeApplied().then(() => {
expect(window.getComputedStyle(elm.firstElementChild).fontSize).toBe('10px');
});
});
it('#871 - should apply style to inserted SVG elements', () => {
const root = createElement('x-test', { is: SvgWithLwcDomManual });
document.body.appendChild(root);
const svgElm = root.shadowRoot.querySelector('svg');
svgElm.innerHTML = '<rect></rect>';
return waitForStyleToBeApplied().then(() => {
// Use firstChild instead of firstElementChild since accessing firstElementChild in an SVG element on
// IE11 throws an error.
expect(window.getComputedStyle(svgElm.firstChild).fill).toBe('rgb(0, 255, 0)');
});
});
});