-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
303 lines (243 loc) · 10.3 KB
/
index.spec.js
File metadata and controls
303 lines (243 loc) · 10.3 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
import { createElement } from 'lwc';
import Parent from 'x/parent';
import Child from 'x/child';
import GetterThrows from 'x/getterThrows';
import { jasmine, jasmineSpyOn as spyOn } from '../../../helpers/jasmine.js';
const arr = jasmine.arrayWithExactContents;
// `jasmine.objectContaining` is long, but the method can't be detached/aliased, ergo wrapper
const obj = (val) => jasmine.objectContaining(val);
let entries = [];
beforeEach(() => {
const perfMeasure = performance.measure.bind(performance);
// We could use PerformanceObserver for this, but it's awkward and unreliable for unit testing
// See: https://github.com/salesforce/lwc/issues/4600
spyOn(performance, 'measure').and.callFake((...args) => {
const entry = perfMeasure(...args);
if (['lwc-render', 'lwc-rerender'].includes(entry.name)) {
entries.push(entry);
}
return entry;
});
});
afterEach(() => {
entries = [];
});
function rerenderEntry(tagName, propString) {
return obj({
name: 'lwc-rerender',
detail: obj({
devtools: obj({
properties: arr([
arr(['Component', `<${tagName}>`]),
arr([`<${tagName}>`, propString]),
]),
}),
}),
});
}
function expectRerenderEntry(tagName, propString) {
expect(entries).toEqual(arr([rerenderEntry(tagName, propString)]));
}
it.runIf(process.env.NODE_ENV === 'production')('No perf measures in prod mode', async () => {
const elm = createElement('x-child', { is: Child });
document.body.appendChild(elm);
await new Promise(requestAnimationFrame);
elm.firstName = 'Ferdinand';
await new Promise(requestAnimationFrame);
expect(entries).toEqual([]);
});
describe.skipIf(process.env.NODE_ENV === 'production')('Perf measures in dev mode', () => {
// dev mode
describe('basic', () => {
let elm;
beforeEach(async () => {
elm = createElement('x-child', { is: Child });
document.body.appendChild(elm);
await new Promise(requestAnimationFrame);
expect(entries).toEqual(arr([obj({ name: 'lwc-render' })]));
entries = []; // reset
});
it('Does basic mutation logging', async () => {
elm.firstName = 'Ferdinand';
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'firstName');
});
it('Logs subsequent mutations on the same component', async () => {
elm.firstName = 'Ferdinand';
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'firstName');
entries = []; // reset
elm.lastName = 'Magellan';
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'lastName');
entries = []; // reset
elm.firstName = 'Vasco';
elm.lastName = 'da Gama';
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'firstName, lastName');
});
it('Logs deep mutation on an object', async () => {
elm.setPreviousName('first', 'Vancouver');
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'previousName.first');
});
it('Logs deep mutation on an object - characters requiring bracket member notation', async () => {
elm.setPreviousNameFullName('George Vancouver');
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'previousName["full name"]');
});
it('Logs doubly-deep mutation on an object', async () => {
elm.setPreviousNameSuffix('short', 'Jr.');
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'previousName.suffix.short');
});
it('Logs deep mutation on an array', async () => {
elm.addAlias('Magellan');
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'aliases.length');
});
it('Logs deep mutation on an object within an array', async () => {
elm.setFavoriteIceCreamFlavor('vanilla');
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'favoriteFlavors[0].flavor');
});
it('Logs multiple mutations on the same component', async () => {
elm.firstName = 'Ferdinand';
elm.setPreviousNameSuffix('short', 'Jr.');
elm.setFavoriteIceCreamFlavor('vanilla');
await new Promise(requestAnimationFrame);
expectRerenderEntry(
'x-child',
'favoriteFlavors[0].flavor, firstName, previousName.suffix.short'
);
});
it('Logs a component mutation while another component is rendered for the first time', async () => {
const elm2 = createElement('x-child', { is: Child });
document.body.appendChild(elm2);
elm.firstName = 'Ferdinand';
await new Promise(requestAnimationFrame);
expect(entries).toEqual(
arr([
obj({
name: 'lwc-render',
}),
rerenderEntry('x-child', 'firstName'),
])
);
});
it('Logs two mutations on two instances of same component', async () => {
const elm2 = createElement('x-child', { is: Child });
document.body.appendChild(elm2);
await new Promise(requestAnimationFrame);
expect(entries).toEqual(
arr([
obj({
name: 'lwc-render',
}),
])
);
entries = []; // reset
elm.firstName = 'Marco';
elm2.lastName = 'Polo';
await new Promise(requestAnimationFrame);
expect(entries).toEqual(
arr([
obj({
name: 'lwc-rerender',
detail: obj({
devtools: obj({
properties: arr([
arr([
'Components',
`<x-child> (\u00D72)`, // x2 with multiplication symbol
]),
arr([
`<x-child> (\u00D72)`, // x2 with multiplication symbol
'firstName, lastName',
]),
]),
}),
}),
}),
])
);
});
it('Logs for deep non-enumerable prop mutation', async () => {
elm.setWackyAccessorDeepValue('yolo');
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'wackyAccessors.foo.bar');
});
it('Logs for deep symbol prop mutation', async () => {
elm.setWackyAccessorSymbol('haha');
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'wackyAccessors[Symbol(yolo)]');
});
it('Logs for doubly deep symbol prop mutation', async () => {
elm.setWackyAccessorDoublyDeepSymbol('wahoo');
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'wackyAccessors[Symbol(whoa)].baz');
});
it('Logs for mutation on deeply-recursive object', async () => {
elm.setOnRecursiveObject('woohoo');
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'recursiveObject.foo');
});
});
describe('parent-child', () => {
let elm;
let child;
beforeEach(async () => {
elm = createElement('x-parent', { is: Parent });
document.body.appendChild(elm);
await new Promise(requestAnimationFrame);
child = elm.shadowRoot.querySelector('x-child');
await new Promise(requestAnimationFrame);
expect(entries).toEqual(
arr(
// synthetic lifecycle considers this one lwc-render event rather than two
lwcRuntimeFlags.DISABLE_NATIVE_CUSTOM_ELEMENT_LIFECYCLE
? [obj({ name: 'lwc-render' })]
: [obj({ name: 'lwc-render' }), obj({ name: 'lwc-render' })]
)
);
entries = []; // reset
});
it('Logs a mutation on the parent only', async () => {
elm.firstName = 'Ferdinand';
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-parent', 'firstName');
});
it('Logs a mutation on the child only', async () => {
child.lastName = 'Magellan';
await new Promise(requestAnimationFrame);
expectRerenderEntry('x-child', 'lastName');
});
it('Logs a mutation on both parent and child', async () => {
elm.firstName = 'Ferdinand';
child.lastName = 'Magellan';
await new Promise(requestAnimationFrame);
expect(entries).toEqual(
arr([
obj({
name: 'lwc-rerender',
detail: obj({
devtools: obj({
properties: arr([
arr(['Components', '<x-child>, <x-parent>']),
arr(['<x-child>', 'lastName']),
arr(['<x-parent>', 'firstName']),
]),
}),
}),
}),
])
);
});
});
});
it('handles case where the getter throws an error', async () => {
const elm = createElement('x-getter-throws', { is: GetterThrows });
document.body.appendChild(elm);
await new Promise(requestAnimationFrame);
expect(elm.shadowRoot.querySelector('div').textContent).toBe('hello');
});