fix(web): use FileReader to decode beacon Blob in events test
test-web / test (push) Failing after 31s
test-web / test (push) Failing after 31s
jsdom's Blob doesn't ship .text() (only the polyfill via FileReader or arrayBuffer is reliable). The pagehide test failed with "TypeError: blob.text is not a function" at events.svelte.test.ts:183. Swap blob.text() for new FileReader().readAsText() wrapped in a Promise.
This commit is contained in:
@@ -179,8 +179,14 @@ describe('useEventsDispatcher', () => {
|
||||
|
||||
expect(beacon).toHaveBeenCalled();
|
||||
expect(beacon.mock.calls[0][0]).toBe('/api/events');
|
||||
// jsdom's Blob doesn't ship .text(); FileReader does.
|
||||
const blob = beacon.mock.calls[0][1] as Blob;
|
||||
const text = await blob.text();
|
||||
const text = await new Promise<string>((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onloadend = () => resolve(reader.result as string);
|
||||
reader.onerror = () => reject(reader.error);
|
||||
reader.readAsText(blob);
|
||||
});
|
||||
const payload = JSON.parse(text) as { type: string; duration_played_ms: number };
|
||||
expect(payload.type).toBe('play_ended');
|
||||
expect(payload.duration_played_ms).toBe(73000);
|
||||
|
||||
Reference in New Issue
Block a user