-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessages.test.ts
More file actions
28 lines (23 loc) · 972 Bytes
/
messages.test.ts
File metadata and controls
28 lines (23 loc) · 972 Bytes
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
import { expect, test, vi } from 'vitest';
import { handleMessage } from './messages';
const handlers = () => ({ onPlayUrl: vi.fn(), onPlayText: vi.fn(), onClose: vi.fn() });
test('routes play_url', () => {
const h = handlers();
handleMessage(JSON.stringify({ type: 'play_url', url: 'u' }), h);
expect(h.onPlayUrl).toHaveBeenCalledWith({ type: 'play_url', url: 'u' });
});
test('routes play_text', () => {
const h = handlers();
handleMessage(JSON.stringify({ type: 'play_text', text: 't', volume: 60 }), h);
expect(h.onPlayText).toHaveBeenCalledWith({ type: 'play_text', text: 't', volume: 60 });
});
test('ignores malformed JSON without throwing', () => {
const h = handlers();
expect(() => handleMessage('not json', h)).not.toThrow();
expect(h.onPlayUrl).not.toHaveBeenCalled();
});
test('ignores unknown type', () => {
const h = handlers();
handleMessage(JSON.stringify({ type: 'wat' }), h);
expect(h.onPlayUrl).not.toHaveBeenCalled();
});