-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
414 lines (353 loc) · 17.2 KB
/
index.spec.js
File metadata and controls
414 lines (353 loc) · 17.2 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
import {
createElement,
freezeTemplate,
LightningElement,
registerComponent,
registerTemplate,
registerDecorators,
} from 'lwc';
import HtmlTags from 'html/tags';
import UiSomething from 'ui/something';
import UiSomethingElse from 'ui/somethingElse';
import UiBoolean from 'ui/boolean';
import UiAnother from 'ui/another';
import UiOutputPercent from 'ui/outputpercent';
import ForceFoo from 'force/foo';
import NestedHtmlTags from 'nested/htmlTags';
import { extractDataIds } from '../../helpers/utils.js';
import testProps from './act-components/test-props';
import testAttrs from './act-components/test-attrs';
import testBodySlot from './act-components/test-body-slot';
import testClassAttr from './act-components/test-class-attr';
import testConditionalFalseAttribute from './act-components/test-conditional-false-attribute';
import testConditionalTrueAttribute from './act-components/test-conditional-true-attribute';
import testEmptySlot from './act-components/test-empty-slot';
import testEmptySlotElementCreation from './act-components/test-empty-slot-element-creation';
import testHtmlTags from './act-components/test-html-tags';
import testMultipleChildrenInSlot from './act-components/test-multiple-children-in-slot';
import testMultipleSlots from './act-components/test-multiple-slots';
import testNestedHtmlTags from './act-components/test-nested-html-tags';
import testPropertyReference from './act-components/test-property-reference';
import testSlotAdjacentToNamedSlot from './act-components/test-slot-adjacent-to-named-slot';
import testSlotElementCreationWithDuplicateSlotNames from './act-components/test-slot-element-creation-with-duplicate-slot-names';
import testSlotInGrandchild from './act-components/test-slot-in-grandchild';
import testStyleAttr from './act-components/test-style-attr';
// Tests that confirm that the runtime LWC engine-dom is compatible with the compiled templates
// from the ACTCompiler
describe('ACTCompiler', () => {
function createComponentFromTemplate(
template,
{ props = {}, propsToTrack = [], methods = {} } = {}
) {
const publicProps = {};
for (const prop of Object.keys(props)) {
if (!propsToTrack.includes(prop)) {
publicProps[prop] = { config: 0 };
}
}
class CustomElement extends LightningElement {
constructor(...args) {
super(...args);
for (const key of Object.keys(props)) {
this[key] = props[key];
}
}
}
for (const methodName of Object.keys(methods)) {
Object.defineProperty(CustomElement.prototype, methodName, {
value: methods[methodName],
});
}
const track = {};
for (const prop of propsToTrack) {
track[prop] = 1;
}
registerDecorators(CustomElement, {
publicProps,
track,
});
return registerComponent(CustomElement, {
tmpl: template,
});
}
function loadDependencies(dependencies) {
return dependencies.map((depName) => {
switch (depName) {
case 'force/foo':
return ForceFoo;
case 'html/tags':
return HtmlTags;
case 'lwc':
return { freezeTemplate, registerComponent, registerTemplate };
case 'nested/htmlTags':
return NestedHtmlTags;
case 'ui/another':
return UiAnother;
case 'ui/boolean':
return UiBoolean;
case 'ui/outputpercent':
return UiOutputPercent;
case 'ui/something':
return UiSomething;
case 'ui/somethingElse':
return UiSomethingElse;
default:
throw new Error('Unknown dependency: ' + depName);
}
});
}
function directoryNameToComponentName(dirName) {
const [namespace, name] = dirName.split('/');
return namespace + '-' + name.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);
}
function createAndInsertActComponent(createComponent, options) {
const define = (moduleName, dependencyNames, moduleDef) => {
const dependencies = loadDependencies(dependencyNames);
const template = moduleDef(...dependencies);
const componentName = directoryNameToComponentName(moduleName);
const Component = createComponentFromTemplate(template, options);
return createElement(componentName, { is: Component });
};
const component = createComponent(define);
document.body.appendChild(component);
return component;
}
it('props', () => {
const component = createAndInsertActComponent(testProps);
expect(component.tagName.toLowerCase()).toEqual('records-record-layout2');
expect(component.shadowRoot.children.length).toEqual(1);
expect(component.shadowRoot.children[0].tagName.toLowerCase()).toEqual('ui-something');
const nodes = extractDataIds(component);
expect(nodes.mode.textContent).toEqual('VIEW');
expect(nodes.propWithDash.textContent).toEqual('X');
expect(nodes.fieldLabel.textContent).toEqual('');
expect(nodes.trueAttr.textContent).toEqual('true');
expect(nodes.camelCase.textContent).toEqual('YZ');
});
it('attrs', () => {
const component = createAndInsertActComponent(testAttrs);
const uiSomething = component.shadowRoot.querySelector('ui-something');
expect(uiSomething.getAttribute('data-blah-de-blah')).toEqual('special-data-attribute');
expect(uiSomething.getAttribute('data-blah-blah')).toEqual('anotherDataAttribute');
expect(uiSomething.getAttribute('role')).toEqual('listitem');
});
it('body slot', () => {
const component = createAndInsertActComponent(testBodySlot);
expect(component.shadowRoot.children.length).toEqual(1);
expect(component.shadowRoot.children[0].tagName.toLowerCase()).toEqual('force-foo');
const forceFoo = component.shadowRoot.querySelector('force-foo');
expect(forceFoo.children.length).toEqual(1);
expect(forceFoo.children[0].tagName.toLowerCase()).toEqual('ui-something');
const nodes = extractDataIds(component);
expect(nodes.name.textContent).toEqual('Elizabeth Shaw');
expect(nodes.text.textContent).toEqual('Hello');
});
it('class attribute', () => {
const component = createAndInsertActComponent(testClassAttr);
const uiSomething = component.shadowRoot.querySelector('ui-something');
expect(uiSomething.classList.length).toEqual(2);
expect(uiSomething.classList.contains('slds-has-divider_top')).toEqual(true);
expect(uiSomething.classList.contains('slds-grid')).toEqual(true);
});
it('conditional false attribute - false', () => {
const component = createAndInsertActComponent(testConditionalFalseAttribute, {
props: { state: { irrelevant: false } },
propsToTrack: ['state'],
});
expect(component.shadowRoot.children.length).toEqual(1);
expect(component.shadowRoot.children[0].tagName.toLowerCase()).toEqual('ui-boolean');
const nodes = extractDataIds(component);
expect(nodes.other.textContent).toEqual('irrelevant');
});
it('conditional false attribute - true', () => {
const component = createAndInsertActComponent(testConditionalFalseAttribute, {
props: { state: { irrelevant: true } },
propsToTrack: ['state'],
});
expect(component.shadowRoot.children.length).toEqual(0);
});
it('conditional true attribute - true', () => {
const component = createAndInsertActComponent(testConditionalTrueAttribute, {
props: { state: { irrelevant: true } },
propsToTrack: ['state'],
});
expect(component.shadowRoot.children.length).toEqual(1);
expect(component.shadowRoot.children[0].tagName.toLowerCase()).toEqual('ui-boolean');
const nodes = extractDataIds(component);
expect(nodes.other.textContent).toEqual('irrelevant');
});
it('conditional true attribute - false', () => {
const component = createAndInsertActComponent(testConditionalTrueAttribute, {
props: { state: { irrelevant: false } },
propsToTrack: ['state'],
});
expect(component.shadowRoot.children.length).toEqual(0);
});
it('empty slot', () => {
const component = createAndInsertActComponent(testEmptySlot);
expect(component.shadowRoot.children.length).toEqual(1);
expect(component.shadowRoot.children[0].tagName.toLowerCase()).toEqual('force-foo');
expect(component.shadowRoot.querySelector('force-foo').children.length).toEqual(0);
});
it('empty slot element creation', () => {
const component = createAndInsertActComponent(testEmptySlotElementCreation);
const forceFoo = component.shadowRoot.querySelector('force-foo');
const uiAnother = forceFoo.querySelector('ui-another');
expect(uiAnother.children.length).toEqual(0); // slot is empty
const nodes = extractDataIds(component);
expect(nodes.name.textContent).toEqual('Elizabeth Shaw');
expect(nodes.value.textContent).toEqual('Foo');
});
it('html tags', () => {
const component = createAndInsertActComponent(testHtmlTags);
const htmlTags = component.shadowRoot.querySelector('html-tags');
expect(htmlTags.childNodes.length).toEqual(4);
const span = htmlTags.childNodes[0];
const div = htmlTags.childNodes[1];
const img = htmlTags.childNodes[2];
const text = htmlTags.childNodes[3];
expect(span.tagName.toLowerCase()).toEqual('span');
expect(span.style.color).toEqual('blue');
expect(span.className).toEqual('class1');
expect(div.getAttribute('title')).toEqual('test');
expect(div.children.length).toEqual(1);
expect(div.children[0].tagName.toLowerCase()).toEqual('h1');
expect(img.src).toEqual(
'data:image/png;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='
);
expect(img.alt).toEqual('Smiley face');
expect(img.getAttribute('height')).toEqual('42');
expect(text.textContent).toEqual('</img>');
});
it('multiple children in slot', () => {
const component = createAndInsertActComponent(testMultipleChildrenInSlot);
expect(component.shadowRoot.querySelectorAll('ui-something').length).toEqual(2);
expect(
component.shadowRoot
.querySelectorAll('ui-something')[0]
.shadowRoot.querySelector('[data-id="text"]').textContent
).toEqual('Hello 1');
expect(
component.shadowRoot
.querySelectorAll('ui-something')[1]
.shadowRoot.querySelector('[data-id="text"]').textContent
).toEqual('Hello 2');
});
it('multiple slots', () => {
const component = createAndInsertActComponent(testMultipleSlots);
const forceFoo = component.shadowRoot.querySelector('force-foo');
const defaultSlot = forceFoo.shadowRoot.querySelector('slot');
const firstSlot = forceFoo.shadowRoot.querySelector('slot[name="first"]');
const secondSlot = forceFoo.shadowRoot.querySelector('slot[name="second"]');
expect(defaultSlot.assignedNodes().length).toEqual(0);
expect(firstSlot.assignedNodes().length).toEqual(1);
expect(secondSlot.assignedNodes().length).toEqual(1);
const nodes1 = extractDataIds(firstSlot.assignedNodes()[0]);
expect(nodes1.text.textContent).toEqual('Hello');
const nodes2 = extractDataIds(secondSlot.assignedNodes()[0]);
expect(nodes2.value.textContent).toEqual('Foo');
});
it('nested HTML tags', () => {
const component = createAndInsertActComponent(testNestedHtmlTags);
const nestedHtmlTags = component.shadowRoot.querySelector('nested-html-tags');
expect(nestedHtmlTags.children.length).toEqual(1);
expect(nestedHtmlTags.children[0].tagName.toLowerCase()).toEqual('div');
expect(nestedHtmlTags.children[0].children.length).toEqual(1);
expect(nestedHtmlTags.children[0].children[0].tagName.toLowerCase()).toEqual('div');
expect(nestedHtmlTags.children[0].children[0].className).toEqual('inner');
expect(nestedHtmlTags.children[0].children[0].children.length).toEqual(2);
expect(nestedHtmlTags.children[0].children[0].children[0].tagName.toLowerCase()).toEqual(
'div'
);
expect(nestedHtmlTags.children[0].children[0].children[1].tagName.toLowerCase()).toEqual(
'h2'
);
});
it('property reference and events', () => {
let callCount = 0;
const component = createAndInsertActComponent(testPropertyReference, {
props: {
non: {
value: 'foobar',
},
state: {
recordAvatars: 'foobaz',
},
data: {
label: 'bazquux',
record: 'quuxbar',
},
},
propsToTrack: ['non'],
methods: {
handleToggleSectionCollapsed() {
callCount++;
this.non.value = 'foobar' + callCount;
},
},
});
const nodes = extractDataIds(component);
expect(nodes.notWhitelisted.textContent).toEqual('foobar');
expect(nodes.templateExpression.textContent).toEqual('foobaz');
expect(nodes.label.textContent).toEqual('bazquux');
expect(nodes.value.textContent).toEqual('quuxbar');
expect(callCount).toEqual(0);
const outputPercent = component.shadowRoot.querySelector('ui-outputpercent');
outputPercent.fireToggleSectionCollapsedEvent();
return Promise.resolve()
.then(() => {
expect(callCount).toEqual(1);
expect(nodes.notWhitelisted.textContent).toEqual('foobar1');
outputPercent.fireToggleSectionCollapsedEvent();
})
.then(() => {
expect(callCount).toEqual(2);
expect(nodes.notWhitelisted.textContent).toEqual('foobar2');
});
});
it('slot adjacent to named slot', () => {
const component = createAndInsertActComponent(testSlotAdjacentToNamedSlot);
const forceFoo = component.shadowRoot.querySelector('force-foo');
const defaultSlot = forceFoo.shadowRoot.querySelector('slot');
const adjacentSlot = forceFoo.shadowRoot.querySelector('slot[name="adjacent"]');
expect(defaultSlot.assignedNodes().length).toEqual(2);
expect(adjacentSlot.assignedNodes().length).toEqual(1);
expect(defaultSlot.assignedNodes()[0].tagName.toLowerCase()).toEqual('ui-another');
expect(defaultSlot.assignedNodes()[1].tagName.toLowerCase()).toEqual('slot');
expect(defaultSlot.assignedNodes()[1].assignedNodes().length).toEqual(0);
expect(adjacentSlot.assignedNodes()[0].tagName.toLowerCase()).toEqual('ui-another');
expect(extractDataIds(defaultSlot.assignedNodes()[0]).value.textContent).toEqual('Foo');
expect(extractDataIds(adjacentSlot.assignedNodes()[0]).value.textContent).toEqual('Foo');
});
it('slot element creation with duplicate slot names', () => {
const component = createAndInsertActComponent(
testSlotElementCreationWithDuplicateSlotNames
);
const forceFoo = component.shadowRoot.querySelector('force-foo');
const defaultSlot = forceFoo.shadowRoot.querySelector('slot');
expect(defaultSlot.assignedNodes().length).toEqual(2);
expect(defaultSlot.assignedNodes()[0].name).toEqual('first');
expect(defaultSlot.assignedNodes()[1].name).toEqual('first');
expect(defaultSlot.assignedNodes()[0].children.length).toEqual(1);
expect(defaultSlot.assignedNodes()[1].children.length).toEqual(1);
expect(defaultSlot.assignedNodes()[0].children[0].tagName.toLowerCase()).toEqual(
'ui-something'
);
expect(defaultSlot.assignedNodes()[1].children[0].tagName.toLowerCase()).toEqual(
'ui-something-else'
);
});
it('slot in grandchild', () => {
const component = createAndInsertActComponent(testSlotInGrandchild);
const forceFoo = component.shadowRoot.querySelector('force-foo');
const firstSlot = forceFoo.shadowRoot.querySelector('slot[name="first"]');
expect(firstSlot.assignedNodes().length).toEqual(1);
expect(firstSlot.assignedNodes()[0].tagName.toLowerCase()).toEqual('ui-something');
});
it('style attribute', () => {
const component = createAndInsertActComponent(testStyleAttr);
const uiSomething = component.shadowRoot.querySelector('ui-something');
expect(uiSomething.style.color).toEqual('blue');
expect(uiSomething.style.textAlign).toEqual('center');
});
});