|
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | + |
| 3 | +import { createTrigger, renderHookServer } from '@/tests'; |
| 4 | + |
| 5 | +import type { SpriteMap } from './useAudio'; |
| 6 | + |
| 7 | +import { useAudio } from './useAudio'; |
| 8 | + |
| 9 | +const trigger = createTrigger<string, (event: MessageEvent) => void>(); |
| 10 | +const mockAudioPlay = vi.fn(); |
| 11 | +const mockAudioPause = vi.fn(); |
| 12 | +const mockAudioRemove = vi.fn(); |
| 13 | +const mockAudioRemoveEventListener = vi.fn(); |
| 14 | +const mockAudioAddEventListener = vi.fn(); |
| 15 | + |
| 16 | +class MockAudio { |
| 17 | + play = mockAudioPlay; |
| 18 | + pause = mockAudioPause; |
| 19 | + remove = mockAudioRemove; |
| 20 | + addEventListener = (type: string, callback: (event: MessageEvent) => void) => { |
| 21 | + mockAudioAddEventListener(type, callback); |
| 22 | + trigger.add(type, callback); |
| 23 | + }; |
| 24 | + removeEventListener = (type: string, callback: (event: MessageEvent) => void) => { |
| 25 | + mockAudioRemoveEventListener(type, callback); |
| 26 | + if (trigger.get(type) === callback) trigger.delete(type); |
| 27 | + }; |
| 28 | + volume = 0; |
| 29 | + playbackRate = 0; |
| 30 | + currentTime = 0; |
| 31 | +} |
| 32 | + |
| 33 | +afterEach(vi.clearAllMocks); |
| 34 | + |
| 35 | +globalThis.Audio = MockAudio as unknown as typeof Audio; |
| 36 | + |
| 37 | +it('Should use audio', () => { |
| 38 | + const { result } = renderHook(() => useAudio('audio.mp3')); |
| 39 | + |
| 40 | + expect(result.current.playing).toBeFalsy(); |
| 41 | + expect(result.current.volume).toBe(1); |
| 42 | + expect(result.current.playbackRate).toBe(1); |
| 43 | + expect(result.current.play).toBeTypeOf('function'); |
| 44 | + expect(result.current.pause).toBeTypeOf('function'); |
| 45 | + expect(result.current.stop).toBeTypeOf('function'); |
| 46 | + expect(result.current.setVolume).toBeTypeOf('function'); |
| 47 | + expect(result.current.changePlaybackRate).toBeTypeOf('function'); |
| 48 | +}); |
| 49 | + |
| 50 | +it('Should use audio on server side', () => { |
| 51 | + const { result } = renderHookServer(() => useAudio('audio.mp3')); |
| 52 | + |
| 53 | + expect(result.current.playing).toBeFalsy(); |
| 54 | + expect(result.current.volume).toBe(1); |
| 55 | + expect(result.current.playbackRate).toBe(1); |
| 56 | + expect(result.current.play).toBeTypeOf('function'); |
| 57 | + expect(result.current.pause).toBeTypeOf('function'); |
| 58 | + expect(result.current.stop).toBeTypeOf('function'); |
| 59 | + expect(result.current.setVolume).toBeTypeOf('function'); |
| 60 | + expect(result.current.changePlaybackRate).toBeTypeOf('function'); |
| 61 | +}); |
| 62 | + |
| 63 | +it('Should initialize with custom options', () => { |
| 64 | + const options = { |
| 65 | + volume: 0.5, |
| 66 | + playbackRate: 1.5, |
| 67 | + interrupt: true |
| 68 | + }; |
| 69 | + |
| 70 | + const { result } = renderHook(() => useAudio('audio.mp3', options)); |
| 71 | + |
| 72 | + expect(result.current.volume).toBe(0.5); |
| 73 | + expect(result.current.playbackRate).toBe(1.5); |
| 74 | +}); |
| 75 | + |
| 76 | +it('Should play immediately', () => { |
| 77 | + const { result } = renderHook(() => useAudio('audio.mp3', { immediately: true })); |
| 78 | + |
| 79 | + expect(result.current.playing).toBeTruthy(); |
| 80 | + |
| 81 | + expect(mockAudioPlay).toHaveBeenCalledTimes(1); |
| 82 | +}); |
| 83 | + |
| 84 | +it('Should play audio', async () => { |
| 85 | + const { result } = renderHook(() => useAudio('audio.mp3')); |
| 86 | + |
| 87 | + await act(result.current.play); |
| 88 | + |
| 89 | + expect(mockAudioPlay).toHaveBeenCalledTimes(1); |
| 90 | +}); |
| 91 | + |
| 92 | +it('Should pause audio', async () => { |
| 93 | + const { result } = renderHook(() => |
| 94 | + useAudio('audio.mp3', { |
| 95 | + immediately: true |
| 96 | + }) |
| 97 | + ); |
| 98 | + |
| 99 | + expect(result.current.playing).toBeTruthy(); |
| 100 | + |
| 101 | + await act(result.current.pause); |
| 102 | + |
| 103 | + expect(result.current.playing).toBeFalsy(); |
| 104 | + expect(mockAudioPause).toHaveBeenCalledTimes(1); |
| 105 | +}); |
| 106 | + |
| 107 | +it('Should stop audio', async () => { |
| 108 | + const { result } = renderHook(() => |
| 109 | + useAudio('audio.mp3', { |
| 110 | + immediately: true |
| 111 | + }) |
| 112 | + ); |
| 113 | + |
| 114 | + expect(result.current.playing).toBeTruthy(); |
| 115 | + |
| 116 | + await act(result.current.stop); |
| 117 | + |
| 118 | + expect(result.current.playing).toBeFalsy(); |
| 119 | + expect(mockAudioPause).toHaveBeenCalledTimes(1); |
| 120 | +}); |
| 121 | + |
| 122 | +it('Should set volume', async () => { |
| 123 | + const { result } = renderHook(() => useAudio('audio.mp3')); |
| 124 | + |
| 125 | + act(() => result.current.setVolume(0.7)); |
| 126 | + |
| 127 | + expect(result.current.volume).toBe(0.7); |
| 128 | +}); |
| 129 | + |
| 130 | +it('Should clamp volume between 0 and 1', () => { |
| 131 | + const { result } = renderHook(() => useAudio('audio.mp3')); |
| 132 | + |
| 133 | + act(() => result.current.setVolume(1.5)); |
| 134 | + expect(result.current.volume).toBe(1); |
| 135 | + |
| 136 | + act(() => result.current.setVolume(-0.5)); |
| 137 | + expect(result.current.volume).toBe(0); |
| 138 | +}); |
| 139 | + |
| 140 | +it('Should change playback rate', () => { |
| 141 | + const { result } = renderHook(() => useAudio('audio.mp3')); |
| 142 | + |
| 143 | + act(() => result.current.changePlaybackRate(1.25)); |
| 144 | + |
| 145 | + expect(result.current.playbackRate).toBe(1.25); |
| 146 | +}); |
| 147 | + |
| 148 | +it('Should clamp playback rate between 0.5 and 2', () => { |
| 149 | + const { result } = renderHook(() => useAudio('audio.mp3')); |
| 150 | + |
| 151 | + act(() => result.current.changePlaybackRate(3)); |
| 152 | + expect(result.current.playbackRate).toBe(2); |
| 153 | + |
| 154 | + act(() => result.current.changePlaybackRate(0.25)); |
| 155 | + expect(result.current.playbackRate).toBe(0.5); |
| 156 | +}); |
| 157 | + |
| 158 | +it('Should handle sprite playback', async () => { |
| 159 | + const sprite = { |
| 160 | + intro: [0, 5], |
| 161 | + loop: [5, 15] |
| 162 | + } as SpriteMap; |
| 163 | + |
| 164 | + const { result } = renderHook(() => useAudio('audio.mp3', { sprite })); |
| 165 | + |
| 166 | + await act(async () => { |
| 167 | + await result.current.play('intro'); |
| 168 | + }); |
| 169 | + |
| 170 | + expect(mockAudioPlay).toHaveBeenCalledTimes(1); |
| 171 | +}); |
| 172 | + |
| 173 | +it('Should recreate audio element when src changes', () => { |
| 174 | + const { rerender } = renderHook((src) => useAudio(src, { immediately: true }), { |
| 175 | + initialProps: 'audio.mp3' |
| 176 | + }); |
| 177 | + |
| 178 | + expect(mockAudioPlay).toHaveBeenCalledTimes(1); |
| 179 | + |
| 180 | + rerender('new-audio.mp3'); |
| 181 | + |
| 182 | + expect(mockAudioPlay).toHaveBeenCalledTimes(2); |
| 183 | +}); |
| 184 | + |
| 185 | +it('Should clean up audio element on unmount', () => { |
| 186 | + const { unmount } = renderHook(() => useAudio('audio.mp3')); |
| 187 | + |
| 188 | + unmount(); |
| 189 | + |
| 190 | + expect(mockAudioRemoveEventListener).toHaveBeenCalledWith('play', expect.any(Function)); |
| 191 | + expect(mockAudioRemoveEventListener).toHaveBeenCalledWith('pause', expect.any(Function)); |
| 192 | + expect(mockAudioRemoveEventListener).toHaveBeenCalledWith('ended', expect.any(Function)); |
| 193 | + expect(mockAudioRemoveEventListener).toHaveBeenCalledWith('volumechange', expect.any(Function)); |
| 194 | + expect(mockAudioRemoveEventListener).toHaveBeenCalledWith('ratechange', expect.any(Function)); |
| 195 | + expect(mockAudioPause).toHaveBeenCalled(); |
| 196 | + expect(mockAudioRemove).toHaveBeenCalled(); |
| 197 | +}); |
0 commit comments