From a82a03fedae99225475bdc5df019cb2b1dbf4e74 Mon Sep 17 00:00:00 2001 From: Xavier Araque Date: Fri, 7 Nov 2025 16:00:12 +0100 Subject: [PATCH] fix: test --- .../audioplayer/hooks/usePreloading.test.js | 20 ++++++------- .../audioplayer/hooks/useReplayGain.test.js | 29 ++++++++++--------- .../audioplayer/hooks/useScrobbling.test.js | 11 ++++--- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/ui/src/audioplayer/hooks/usePreloading.test.js b/ui/src/audioplayer/hooks/usePreloading.test.js index e9a46bb3c..23539ea41 100644 --- a/ui/src/audioplayer/hooks/usePreloading.test.js +++ b/ui/src/audioplayer/hooks/usePreloading.test.js @@ -14,10 +14,10 @@ describe('usePreloading', () => { beforeEach(() => { vi.clearAllMocks() // Mock Audio constructor - global.Audio = vi.fn().mockImplementation(() => ({ - src: '', - addEventListener: vi.fn(), - })) + global.Audio = vi.fn().mockImplementation(function() { + this.src = '' + this.addEventListener = vi.fn() + }) }) afterEach(() => { @@ -119,16 +119,14 @@ describe('usePreloading', () => { it('should handle audio load errors gracefully', () => { const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) - const mockAudioInstance = { - src: '', - addEventListener: vi.fn((event, callback) => { + global.Audio = vi.fn().mockImplementation(function() { + this.src = '' + this.addEventListener = vi.fn((event, callback) => { if (event === 'error') { callback(new Event('error')) } - }), - } - - global.Audio = vi.fn().mockImplementation(() => mockAudioInstance) + }) + }) const { result } = renderHook(() => usePreloading(mockPlayerState)) diff --git a/ui/src/audioplayer/hooks/useReplayGain.test.js b/ui/src/audioplayer/hooks/useReplayGain.test.js index 5edf2a7ca..e2751f6f3 100644 --- a/ui/src/audioplayer/hooks/useReplayGain.test.js +++ b/ui/src/audioplayer/hooks/useReplayGain.test.js @@ -16,18 +16,18 @@ describe('useReplayGain', () => { beforeEach(() => { vi.clearAllMocks() // Mock Web Audio API - global.AudioContext = vi.fn().mockImplementation(() => ({ - createMediaElementSource: vi.fn(() => ({ + global.AudioContext = vi.fn().mockImplementation(function() { + this.createMediaElementSource = vi.fn(() => ({ connect: vi.fn(), - })), - createGain: vi.fn(() => ({ + })) + this.createGain = vi.fn(() => ({ gain: { setValueAtTime: vi.fn(), }, connect: vi.fn(), - })), - currentTime: 0, - })) + })) + this.currentTime = 0 + }) }) afterEach(() => { @@ -85,7 +85,7 @@ describe('useReplayGain', () => { const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) // Mock AudioContext to throw error - global.AudioContext = vi.fn().mockImplementation(() => { + global.AudioContext = vi.fn().mockImplementation(function() { throw new Error('Web Audio API not supported') }) @@ -122,15 +122,16 @@ describe('useReplayGain', () => { throw new Error('Gain application failed') }), }, + connect: vi.fn(), } - global.AudioContext = vi.fn().mockImplementation(() => ({ - createMediaElementSource: vi.fn(() => ({ + global.AudioContext = vi.fn().mockImplementation(function() { + this.createMediaElementSource = vi.fn(() => ({ connect: vi.fn(), - })), - createGain: vi.fn(() => mockGainNode), - currentTime: 0, - })) + })) + this.createGain = vi.fn(() => mockGainNode) + this.currentTime = 0 + }) const { result } = renderHook(() => useReplayGain(mockAudioInstance, mockPlayerState, mockGainInfo), diff --git a/ui/src/audioplayer/hooks/useScrobbling.test.js b/ui/src/audioplayer/hooks/useScrobbling.test.js index 028ce71e5..b91898b0c 100644 --- a/ui/src/audioplayer/hooks/useScrobbling.test.js +++ b/ui/src/audioplayer/hooks/useScrobbling.test.js @@ -4,7 +4,10 @@ import { describe, it, beforeEach, vi, expect } from 'vitest' // Mock subsonic module vi.mock('../../subsonic', () => ({ - default: {}, + default: { + scrobble: vi.fn(), + nowPlaying: vi.fn(), + }, scrobble: vi.fn(), nowPlaying: vi.fn(), })) @@ -62,7 +65,7 @@ describe('useScrobbling', () => { }) // Should scrobble since progress > 50% and time > 4 minutes - expect(subsonic.scrobble).toHaveBeenCalledWith('track1', null) + expect(subsonic.default.scrobble).toHaveBeenCalledWith('track1', null) expect(result.current.scrobbled).toBe(true) }) @@ -133,7 +136,7 @@ describe('useScrobbling', () => { it('should handle scrobbling errors gracefully', () => { const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) // const mockSubsonic = subsonic - subsonic.scrobble.mockImplementation(() => { + subsonic.default.scrobble.mockImplementation(() => { throw new Error('Scrobbling failed') }) @@ -156,7 +159,7 @@ describe('useScrobbling', () => { 'Scrobbling error:', expect.any(Error), ) - expect(result.current.scrobbled).toBe(true) // Still sets to true despite error + expect(result.current.scrobbled).toBe(false) // Should not set to true on error consoleSpy.mockRestore() })