Repository navigation
Expand file tree
/
Copy pathlottieFailure.spec.ts
More file actions
150 lines (131 loc) · 4.57 KB
/
Copy pathlottieFailure.spec.ts
File metadata and controls
150 lines (131 loc) · 4.57 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
import {test, expect} from '@playwright/test';
test('cleans up a player when tlottie rejects an animation', async({page}) => {
await page.addInitScript(() => {
(window as any).__lottieUnhandledRejections = [];
window.addEventListener('unhandledrejection', (event) => {
(window as any).__lottieUnhandledRejections.push(String(event.reason));
});
});
await page.goto('/');
await page.waitForFunction(
() => typeof (window as any).lottieLoader?.loadAnimationFromURL === 'function',
null,
{timeout: 30_000}
);
const loadResult = await page.evaluate(async() => {
const container = document.createElement('div');
container.id = 'failed-lottie';
container.style.cssText = 'width:64px;height:64px;';
document.body.append(container);
const player = await (window as any).lottieLoader.loadAnimationWorker({
container,
width: 64,
height: 64,
group: 'none',
name: 'failed-lottie',
animationData: new Blob(['{not-json'])
});
try {
await player.loadPromise;
return {rejected: false};
} catch(error) {
return {rejected: true, error: String(error)};
}
});
expect(loadResult.rejected).toBe(true);
expect(loadResult.error).toBeTruthy();
await page.waitForFunction(
() => !(window as any).lottieLoader.getAnimation(document.getElementById('failed-lottie')),
null,
{timeout: 10_000}
);
await page.waitForTimeout(100);
expect(await page.evaluate(() => (window as any).__lottieUnhandledRejections)).toEqual([]);
});
test('frame-driven players render only explicitly requested frames', async({page}) => {
await page.goto('/');
await page.waitForFunction(
() => typeof (window as any).lottieLoader?.loadAnimationFromURL === 'function',
null,
{timeout: 30_000}
);
const result = await page.evaluate(async() => {
const container = document.createElement('div');
container.style.cssText = 'width:64px;height:64px;';
document.body.append(container);
const player = await (window as any).lottieLoader.loadAnimationFromURL({
container,
width: 64,
height: 64,
group: 'none',
name: 'frame-driven-lottie',
autoplay: false,
noOffscreen: true,
skipFirstFrameRendering: true
}, '/assets/tgs/key.json');
const frames: number[] = [];
player.addEventListener('enterFrame', (frameNo: number) => frames.push(frameNo));
await player.loadPromise;
await new Promise((resolve) => setTimeout(resolve, 250));
const framesBeforeRequest = frames.slice();
const renderedFrame = await new Promise<number>((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('frame render timed out')), 5_000);
player.addEventListener('enterFrame', (frameNo: number) => {
clearTimeout(timeout);
resolve(frameNo);
}, {once: true});
player.addEventListener('error', reject, {once: true});
player.requestFrame(1);
});
player.remove();
container.remove();
return {framesBeforeRequest, renderedFrame};
});
expect(result.framesBeforeRequest).toEqual([]);
expect(result.renderedFrame).toBe(1);
});
test('keeps worker preloading silent when WebAssembly SIMD is unavailable', async({page}) => {
await page.addInitScript(() => {
Object.defineProperty(WebAssembly, 'validate', {
configurable: true,
value: () => false
});
(window as any).__lottieUnhandledRejections = [];
window.addEventListener('unhandledrejection', (event) => {
(window as any).__lottieUnhandledRejections.push(String(event.reason));
});
});
await page.goto('/');
await page.waitForFunction(
() => typeof (window as any).lottieLoader?.loadLottieWorkers === 'function',
null,
{timeout: 30_000}
);
const result = await page.evaluate(async() => {
const loader = (window as any).lottieLoader;
await loader.loadLottieWorkers();
const container = document.createElement('div');
container.style.cssText = 'width:64px;height:64px;';
document.body.append(container);
let loadError: string;
try {
await loader.loadAnimationWorker({
container,
width: 64,
height: 64,
animationData: new Blob(['{}'])
});
} catch(error) {
loadError = (error as any)?.type;
} finally {
container.remove();
}
await new Promise((resolve) => setTimeout(resolve, 100));
return {
loadError,
unhandledRejections: (window as any).__lottieUnhandledRejections
};
});
expect(result.loadError).toBe('NO_WASM');
expect(result.unhandledRejections).toEqual([]);
});