-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathAudioPlayerNotificationsPlugin.test.ts
More file actions
96 lines (78 loc) · 3.27 KB
/
AudioPlayerNotificationsPlugin.test.ts
File metadata and controls
96 lines (78 loc) · 3.27 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
import { fromPartial } from '@total-typescript/shoehorn';
import type { TFunction } from 'i18next';
import { audioPlayerNotificationsPluginFactory } from '../AudioPlayerNotificationsPlugin';
describe('audioPlayerNotificationsPluginFactory', () => {
const t: TFunction = ((s: string) => s) as TFunction;
const makeNotifier = () => {
const addNotification = vi.fn();
return {
addNotification,
};
};
it('reports mapped error messages for known errCodes', () => {
const { addNotification } = makeNotifier();
const plugin = audioPlayerNotificationsPluginFactory({
addNotification,
t,
});
// simulate failed-to-start
plugin.onError?.({ errCode: 'failed-to-start', player: fromPartial({}) });
expect(addNotification).toHaveBeenCalledTimes(1);
let call = addNotification.mock.calls[0][0];
expect(call.message).toBe('Failed to play the recording');
expect(call.type).toBe('browser:audio:playback:error');
expect(call.emitter).toBe('AudioPlayer');
expect(call.severity).toBe('error');
// simulate not-playable
plugin.onError?.({ errCode: 'not-playable', player: fromPartial({}) });
call = addNotification.mock.calls[1][0];
expect(call.message).toBe(
'Recording format is not supported and cannot be reproduced',
);
// simulate seek-not-supported
plugin.onError?.({ errCode: 'seek-not-supported', player: fromPartial({}) });
call = addNotification.mock.calls[2][0];
expect(call.message).toBe('Cannot seek in the recording');
});
it('falls back to provided Error if no errCode', () => {
const { addNotification } = makeNotifier();
const plugin = audioPlayerNotificationsPluginFactory({
addNotification,
t,
});
plugin.onError?.({ error: new Error('X-Error'), player: fromPartial({}) });
const call = addNotification.mock.calls[0][0];
expect(call.message).toBe('X-Error');
});
it('falls back to generic message if no errCode and no Error', () => {
const { addNotification } = makeNotifier();
const plugin = audioPlayerNotificationsPluginFactory({
addNotification,
t,
});
plugin.onError?.({ player: fromPartial({}) });
const call = addNotification.mock.calls[0][0];
expect(call.message).toBe('Error reproducing the recording');
});
it('debounces seek-not-supported notifications', () => {
const { addNotification } = makeNotifier();
const plugin = audioPlayerNotificationsPluginFactory({
addNotification,
t,
});
const dateNowSpy = vi.spyOn(Date, 'now');
dateNowSpy
.mockReturnValueOnce(1000)
.mockReturnValueOnce(1200)
.mockReturnValueOnce(1800)
.mockReturnValueOnce(2201);
plugin.onError?.({ errCode: 'seek-not-supported', player: fromPartial({}) });
plugin.onError?.({ errCode: 'seek-not-supported', player: fromPartial({}) });
plugin.onError?.({ errCode: 'seek-not-supported', player: fromPartial({}) });
plugin.onError?.({ errCode: 'seek-not-supported', player: fromPartial({}) });
expect(addNotification).toHaveBeenCalledTimes(2);
expect(addNotification.mock.calls[0][0].message).toBe('Cannot seek in the recording');
expect(addNotification.mock.calls[1][0].message).toBe('Cannot seek in the recording');
dateNowSpy.mockRestore();
});
});