File size: 1,504 Bytes
2b18d49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { WaveformVisualizer } from '../../js/components/waveform-visualizer.js';

function makeCanvas() {
    const canvas = document.createElement('canvas');
    const ctx = {
        fillStyle: '',
        strokeStyle: '',
        lineWidth: 0,
        fillRect: jest.fn(),
        beginPath: jest.fn(),
        moveTo: jest.fn(),
        lineTo: jest.fn(),
        stroke: jest.fn(),
    };
    canvas.getContext = jest.fn(() => ctx);
    return { canvas, ctx };
}

describe('WaveformVisualizer', () => {
    let viz, userCanvas, agentCanvas;

    beforeEach(() => {
        ({ canvas: userCanvas } = makeCanvas());
        ({ canvas: agentCanvas } = makeCanvas());
        viz = new WaveformVisualizer(userCanvas, agentCanvas);
    });

    afterEach(() => {
        viz.stop();
    });

    test('initialises canvas dimensions', () => {
        expect(userCanvas.width).toBe(350);
        expect(userCanvas.height).toBe(60);
        expect(agentCanvas.width).toBe(350);
        expect(agentCanvas.height).toBe(60);
    });

    test('stopAgentSpeaking resets isAgentSpeaking flag', () => {
        viz.startAgentSpeaking();
        viz.stopAgentSpeaking();
        expect(viz.isAgentSpeaking).toBe(false);
    });

    test('stop cancels animation frame', () => {
        const cancelSpy = jest.spyOn(global, 'cancelAnimationFrame');
        viz.start(() => null, () => null);
        viz.stop();
        expect(cancelSpy).toHaveBeenCalled();
        expect(viz.animationId).toBeNull();
    });
});