-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
288 lines (252 loc) · 10.2 KB
/
index.spec.js
File metadata and controls
288 lines (252 loc) · 10.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
import { LightningElement, api, getComponentDef, createElement } from 'lwc';
import PublicProperties from 'x/publicProperties';
import PublicAccessors from 'x/publicAccessors';
import PublicMethods from 'x/publicMethods';
import PublicPropertiesInheritance from 'x/publicPropertiesInheritance';
import PublicMethodsInheritance from 'x/publicMethodsInheritance';
import PrivateAccessors from 'x/privateAccessors';
import HtmlElementProps from 'x/htmlElementProps';
import { jasmine } from '../../../helpers/jasmine.js';
import { ariaProperties } from '../../../helpers/aria.js';
function testInvalidComponentConstructor(name, ctor) {
it(`should throw for ${name}`, () => {
expect(() => getComponentDef(ctor)).toThrowError(
/.+ is not a valid component, or does not extends LightningElement from "lwc". You probably forgot to add the extend clause on the class declaration./
);
});
}
testInvalidComponentConstructor('null', null);
testInvalidComponentConstructor('undefined', undefined);
testInvalidComponentConstructor('String', 'component');
testInvalidComponentConstructor('Object', {});
testInvalidComponentConstructor('Function', function () {});
testInvalidComponentConstructor('Class not extending LightningElement', class Component {});
const GLOBAL_HTML_ATTRIBUTES = [
'accessKey',
'dir',
'draggable',
'hidden',
'id',
'lang',
'spellcheck',
'tabIndex',
'title',
...ariaProperties,
].sort();
const message = (propName) =>
`Error: [LWC warn]: The property "${propName}" is not publicly accessible. Add the @api annotation to the property declaration or getter/setter in the component to make it accessible.`;
it('it should return the global HTML attributes in props', () => {
class Component extends LightningElement {}
const def = getComponentDef(Component);
const defaultProps = Object.keys(def.props).sort();
expect(defaultProps).toEqual(GLOBAL_HTML_ATTRIBUTES);
});
describe('@api', () => {
it('should return the public properties in the props object', () => {
const { props } = getComponentDef(PublicProperties);
expect(props).toEqual(
jasmine.objectContaining({
foo: {
config: 0,
type: 'any',
attr: 'foo',
},
bar: {
config: 0,
type: 'any',
attr: 'bar',
},
})
);
});
it('should return the public accessors in the props object', () => {
const { props } = getComponentDef(PublicAccessors);
expect(props).toEqual(
jasmine.objectContaining({
getterOnly: {
config: 1,
type: 'any',
attr: 'getter-only',
},
getterAndSetter: {
config: 3,
type: 'any',
attr: 'getter-and-setter',
},
})
);
});
it('should return the public methods in the methods object', () => {
const { methods } = getComponentDef(PublicMethods);
expect(methods).toEqual({
foo: PublicMethods.prototype.foo,
bar: PublicMethods.prototype.bar,
});
});
it('should return also the public properties inherited from the base class', () => {
const { props } = getComponentDef(PublicPropertiesInheritance);
expect(props).toEqual(
jasmine.objectContaining({
parentProp: {
config: 3,
type: 'any',
attr: 'parent-prop',
},
childProp: {
config: 0,
type: 'any',
attr: 'child-prop',
},
overriddenInChild: {
config: 0,
type: 'any',
attr: 'overridden-in-child',
},
})
);
});
it('should return also the public methods inherited from the base class', () => {
const { methods } = getComponentDef(PublicMethodsInheritance);
expect(methods).toEqual({
parentMethod: Object.getPrototypeOf(PublicMethodsInheritance.prototype).parentMethod,
overriddenInChild: PublicMethodsInheritance.prototype.overriddenInChild,
childMethod: PublicMethodsInheritance.prototype.childMethod,
});
});
it('should log warning when accessing a private prop', () => {
const elm = createElement('x-private-accessor', { is: PrivateAccessors });
document.body.appendChild(elm);
expect(() => {
// Testing the getter; don't need to use the return value
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
elm['privateProp'];
}).toLogWarningDev(message('privateProp'));
});
it('should log warning when setting a private prop', () => {
const elm = createElement('x-private-accessor', { is: PrivateAccessors });
document.body.appendChild(elm);
expect(() => {
elm['privateProp'] = 'foo';
}).toLogWarningDev(message('privateProp'));
});
it('should not log warning when accessing a public prop', () => {
const elm = createElement('x-private-accessor', { is: PrivateAccessors });
document.body.appendChild(elm);
expect(() => {
// Testing the getter; don't need to use the return value
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
elm['publicProb'];
}).not.toLogWarningDev();
});
it('should not log warning when setting a public prop', () => {
const elm = createElement('x-private-accessor', { is: PrivateAccessors });
document.body.appendChild(elm);
expect(() => {
elm['publicProb'] = 'foo';
}).not.toLogWarningDev();
});
it('should log warning when accessing a private prop without a getter', () => {
const elm = createElement('x-private-accessor', { is: PrivateAccessors });
document.body.appendChild(elm);
expect(() => {
// Testing the getter; don't need to use the return value
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
elm['nonDecoratedPrivateProp'];
}).toLogWarningDev(message('nonDecoratedPrivateProp'));
});
it('should log warning when accessing a tracked private prop', () => {
const elm = createElement('x-private-accessor', { is: PrivateAccessors });
document.body.appendChild(elm);
expect(() => {
// Testing the getter; don't need to use the return value
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
elm['trackedProp'];
}).toLogWarningDev(message('trackedProp'));
});
it('should not log a warning on HTMLElement props', () => {
const elm = createElement('x-html-element-props', { is: HtmlElementProps });
document.body.appendChild(elm);
expect(() => {
// Testing the getter; don't need to use the return value
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
elm.constructor;
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
elm.tabIndex;
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
elm.title;
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
elm.attributes;
}).not.toLogWarningDev();
expect(elm.attributes.length).toBe(0);
});
});
describe('circular dependencies', () => {
// Emulates an AMD module with circular dependency.
function circularDependency(klass) {
const ctor = function () {
return klass;
};
ctor.__circular__ = true;
return ctor;
}
it('should resolve parent class with circular dependencies', () => {
const Circular = circularDependency(
class extends LightningElement {
@api foo;
}
);
class Component extends Circular {
@api bar;
}
const { props } = getComponentDef(Component);
expect(props).toEqual(
jasmine.objectContaining({
foo: {
config: 0,
type: 'any',
attr: 'foo',
},
bar: {
config: 0,
type: 'any',
attr: 'bar',
},
})
);
});
it('should throw when parent class is a circular dependency not extending LightningElement', () => {
const Circular = circularDependency(class {});
class Component extends Circular {}
expect(() => getComponentDef(Component)).toThrowError(
/.+ is not a valid component, or does not extends LightningElement from "lwc". You probably forgot to add the extend clause on the class declaration./
);
});
it('should throw when parent class is a circular dependency resolving null', () => {
const Circular = circularDependency(null);
class Component extends Circular {}
expect(() => getComponentDef(Component)).toThrowError(
/.+ is not a valid component, or does not extends LightningElement from "lwc". You probably forgot to add the extend clause on the class declaration./
);
});
describe('locker integration', () => {
it('should resolve parent class with circular dependency that resoves to itself', () => {
function SecureBaseClass() {
return SecureBaseClass;
}
SecureBaseClass.__circular__ = true;
class Component extends SecureBaseClass {
@api bar;
}
const { props } = getComponentDef(Component);
expect(props).toEqual(
jasmine.objectContaining({
bar: {
config: 0,
type: 'any',
attr: 'bar',
},
})
);
});
});
});