-
Notifications
You must be signed in to change notification settings - Fork 879
/
performance-controller_test.ts
318 lines (278 loc) · 9.19 KB
/
performance-controller_test.ts
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
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import {
ReactiveElement,
PropertyValues,
ReactiveControllerHost,
} from '@lit/reactive-element';
import {
PerformanceController,
PerformanceControllerConfig,
PerformanceValueCallback,
} from '@lit-labs/observers/performance-controller.js';
import {generateElementName, nextFrame} from './test-helpers.js';
import {assert} from '@esm-bundle/chai';
// Note, since tests are not built with production support, detect DEV_MODE
// by checking if warning API is available.
const DEV_MODE = !!ReactiveElement.enableWarning;
if (DEV_MODE) {
ReactiveElement.disableWarning?.('change-in-update');
}
const generateMeasure = async (sync = false) => {
performance.mark('a');
if (!sync) {
await new Promise((resolve) => setTimeout(resolve));
}
performance.mark('b');
performance.measure('measure', 'a', 'b');
};
const observerComplete = async (el?: HTMLElement) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(el as any)?.observer?.flush();
await nextFrame();
await nextFrame();
};
// const canTest = async () => {
// let ok = false;
// if (window.PerformanceObserver) {
// const o = new PerformanceObserver(() => (ok = true));
// o.observe({entryTypes: ['measure']});
// await generateMeasure();
// }
// await observerComplete();
// return ok;
// };
const canTest = () => {
// TODO: disable tests on Sauce Safari until can figure out issues.
// The tests pass on latest Safari locally.
const isSafari =
navigator.userAgent.includes('Safari/') &&
navigator.userAgent.includes('Version/');
return !isSafari && window.PerformanceObserver;
};
(canTest() ? suite : suite.skip)('PerformanceController', () => {
let container: HTMLElement;
interface TestElement extends ReactiveElement {
observer: PerformanceController;
observerValue: unknown;
resetObserverValue: () => void;
changeDuringUpdate?: () => void;
}
const defineTestElement = (
getControllerConfig: (
host: ReactiveControllerHost
) => PerformanceControllerConfig
) => {
class A extends ReactiveElement {
observer: PerformanceController;
observerValue: unknown;
changeDuringUpdate?: () => void;
constructor() {
super();
const config = getControllerConfig(this);
this.observer = new PerformanceController(this, {
callback: () => true,
...config,
});
}
override update(props: PropertyValues) {
super.update(props);
if (this.changeDuringUpdate) {
this.changeDuringUpdate();
}
}
override updated() {
this.observerValue = this.observer.value;
}
resetObserverValue() {
this.observer.value = this.observerValue = undefined;
}
}
customElements.define(generateElementName(), A);
return A;
};
const renderTestElement = async (Ctor: typeof HTMLElement) => {
const el = new Ctor() as TestElement;
container.appendChild(el);
await observerComplete(el);
return el;
};
const getTestElement = async (
getControllerConfig: (
host: ReactiveControllerHost
) => PerformanceControllerConfig
) => {
const ctor = defineTestElement(getControllerConfig);
const el = await renderTestElement(ctor);
return el;
};
setup(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
teardown(() => {
if (container && container.parentNode) {
container.parentNode.removeChild(container);
}
});
test('can observe changes', async () => {
const el = await getTestElement((_host: ReactiveControllerHost) => ({
config: {entryTypes: ['measure']},
}));
// Reports initial change by default
assert.isTrue(el.observerValue);
// Reports attribute change
el.resetObserverValue();
await generateMeasure();
await observerComplete(el);
assert.isTrue(el.observerValue);
// Reports another attribute change
el.resetObserverValue();
el.requestUpdate();
await observerComplete(el);
assert.isUndefined(el.observerValue);
await generateMeasure();
await observerComplete(el);
assert.isTrue(el.observerValue);
});
test('can observe changes during update', async () => {
const el = await getTestElement((_host: ReactiveControllerHost) => ({
config: {entryTypes: ['measure']},
}));
el.resetObserverValue();
el.changeDuringUpdate = () => generateMeasure(true);
el.requestUpdate();
await observerComplete(el);
assert.isTrue(el.observerValue);
});
test('skips initial changes when `skipInitial` is `true`', async () => {
const el = await getTestElement((_host: ReactiveControllerHost) => ({
config: {entryTypes: ['measure']},
skipInitial: true,
}));
// Does not reports initial change when `skipInitial` is set
assert.isUndefined(el.observerValue);
// Reports subsequent change when `skipInitial` is set
el.resetObserverValue();
await generateMeasure();
await observerComplete(el);
assert.isTrue(el.observerValue);
// Reports another change
el.resetObserverValue();
el.requestUpdate();
await nextFrame();
assert.isUndefined(el.observerValue);
await generateMeasure();
await observerComplete(el);
assert.isTrue(el.observerValue);
});
test('observation managed via connection', async () => {
const el = await getTestElement((_host: ReactiveControllerHost) => ({
config: {entryTypes: ['measure']},
skipInitial: true,
}));
assert.isUndefined(el.observerValue);
// Does not report change after element removed.
el.remove();
await generateMeasure();
// Reports no change after element re-connected.
container.appendChild(el);
await observerComplete(el);
assert.isUndefined(el.observerValue);
// Reports change when element is connected
await generateMeasure();
await observerComplete(el);
assert.isTrue(el.observerValue);
});
test('can manage value via `callback`', async () => {
let count = 0;
const el = await getTestElement((_host: ReactiveControllerHost) => ({
config: {entryTypes: ['measure']},
callback: (entries: PerformanceEntryList) =>
`${count++}:${entries
.map((r: PerformanceEntry) => r.duration)
.reduce((a, c) => a + c, 0)}`,
}));
assert.equal(el.observerValue, '0:0');
await generateMeasure();
await observerComplete(el);
assert.match(el.observerValue as string, /1:[\d]/);
await generateMeasure();
await observerComplete(el);
assert.match(el.observerValue as string, /2:[\d]/);
});
test('can observe changes when initialized after host connected', async () => {
class TestFirstUpdated extends ReactiveElement {
observer!: PerformanceController<true>;
observerValue: true | undefined = undefined;
override firstUpdated() {
this.observer = new PerformanceController(this, {
config: {entryTypes: ['measure']},
callback: () => true,
});
}
override updated() {
this.observerValue = this.observer.value;
}
resetObserverValue() {
this.observer.value = this.observerValue = undefined;
}
}
customElements.define(generateElementName(), TestFirstUpdated);
const el = (await renderTestElement(TestFirstUpdated)) as TestFirstUpdated;
// Reports initial change by default
assert.isTrue(el.observerValue);
// Reports measure
el.resetObserverValue();
await generateMeasure();
await observerComplete(el);
assert.isTrue(el.observerValue);
// Reports another measure after noop update
el.resetObserverValue();
el.requestUpdate();
await observerComplete(el);
assert.isUndefined(el.observerValue);
await generateMeasure();
await observerComplete(el);
assert.isTrue(el.observerValue);
});
test('PerformanceController<T> type-checks', async () => {
// This test only checks compile-type behavior. There are no runtime checks.
const el = await getTestElement((_host: ReactiveControllerHost) => ({
config: {entryTypes: ['measure']},
}));
const A = new PerformanceController<number>(el, {
// @ts-expect-error Type 'string' is not assignable to type 'number'
callback: () => '',
config: {entryTypes: ['measure']},
});
if (A) {
// Suppress no-unused-vars warnings
}
const B = new PerformanceController(el, {
callback: () => '',
config: {entryTypes: ['measure']},
});
// @ts-expect-error Type 'number' is not assignable to type 'string'.
B.value = 2;
const C = new PerformanceController(el, {
callback: () => '',
config: {entryTypes: ['measure']},
}) as PerformanceController<string>;
// @ts-expect-error Type 'number' is not assignable to type 'string'.
C.value = 3;
const narrowTypeCb: PerformanceValueCallback<string | null> = () => '';
const D = new PerformanceController(el, {
callback: narrowTypeCb,
config: {entryTypes: ['measure']},
});
D.value = null;
D.value = undefined;
D.value = '';
// @ts-expect-error Type 'number' is not assignable to type 'string'
D.value = 3;
});
});