fix: test

This commit is contained in:
Xavier Araque 2025-11-07 16:00:12 +01:00
parent d3c2beabd8
commit a82a03feda
3 changed files with 31 additions and 29 deletions

View File

@ -14,10 +14,10 @@ describe('usePreloading', () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks() vi.clearAllMocks()
// Mock Audio constructor // Mock Audio constructor
global.Audio = vi.fn().mockImplementation(() => ({ global.Audio = vi.fn().mockImplementation(function() {
src: '', this.src = ''
addEventListener: vi.fn(), this.addEventListener = vi.fn()
})) })
}) })
afterEach(() => { afterEach(() => {
@ -119,16 +119,14 @@ describe('usePreloading', () => {
it('should handle audio load errors gracefully', () => { it('should handle audio load errors gracefully', () => {
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
const mockAudioInstance = { global.Audio = vi.fn().mockImplementation(function() {
src: '', this.src = ''
addEventListener: vi.fn((event, callback) => { this.addEventListener = vi.fn((event, callback) => {
if (event === 'error') { if (event === 'error') {
callback(new Event('error')) callback(new Event('error'))
} }
}), })
} })
global.Audio = vi.fn().mockImplementation(() => mockAudioInstance)
const { result } = renderHook(() => usePreloading(mockPlayerState)) const { result } = renderHook(() => usePreloading(mockPlayerState))

View File

@ -16,18 +16,18 @@ describe('useReplayGain', () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks() vi.clearAllMocks()
// Mock Web Audio API // Mock Web Audio API
global.AudioContext = vi.fn().mockImplementation(() => ({ global.AudioContext = vi.fn().mockImplementation(function() {
createMediaElementSource: vi.fn(() => ({ this.createMediaElementSource = vi.fn(() => ({
connect: vi.fn(), connect: vi.fn(),
})), }))
createGain: vi.fn(() => ({ this.createGain = vi.fn(() => ({
gain: { gain: {
setValueAtTime: vi.fn(), setValueAtTime: vi.fn(),
}, },
connect: vi.fn(), connect: vi.fn(),
})), }))
currentTime: 0, this.currentTime = 0
})) })
}) })
afterEach(() => { afterEach(() => {
@ -85,7 +85,7 @@ describe('useReplayGain', () => {
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
// Mock AudioContext to throw error // Mock AudioContext to throw error
global.AudioContext = vi.fn().mockImplementation(() => { global.AudioContext = vi.fn().mockImplementation(function() {
throw new Error('Web Audio API not supported') throw new Error('Web Audio API not supported')
}) })
@ -122,15 +122,16 @@ describe('useReplayGain', () => {
throw new Error('Gain application failed') throw new Error('Gain application failed')
}), }),
}, },
connect: vi.fn(),
} }
global.AudioContext = vi.fn().mockImplementation(() => ({ global.AudioContext = vi.fn().mockImplementation(function() {
createMediaElementSource: vi.fn(() => ({ this.createMediaElementSource = vi.fn(() => ({
connect: vi.fn(), connect: vi.fn(),
})), }))
createGain: vi.fn(() => mockGainNode), this.createGain = vi.fn(() => mockGainNode)
currentTime: 0, this.currentTime = 0
})) })
const { result } = renderHook(() => const { result } = renderHook(() =>
useReplayGain(mockAudioInstance, mockPlayerState, mockGainInfo), useReplayGain(mockAudioInstance, mockPlayerState, mockGainInfo),

View File

@ -4,7 +4,10 @@ import { describe, it, beforeEach, vi, expect } from 'vitest'
// Mock subsonic module // Mock subsonic module
vi.mock('../../subsonic', () => ({ vi.mock('../../subsonic', () => ({
default: {}, default: {
scrobble: vi.fn(),
nowPlaying: vi.fn(),
},
scrobble: vi.fn(), scrobble: vi.fn(),
nowPlaying: vi.fn(), nowPlaying: vi.fn(),
})) }))
@ -62,7 +65,7 @@ describe('useScrobbling', () => {
}) })
// Should scrobble since progress > 50% and time > 4 minutes // 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) expect(result.current.scrobbled).toBe(true)
}) })
@ -133,7 +136,7 @@ describe('useScrobbling', () => {
it('should handle scrobbling errors gracefully', () => { it('should handle scrobbling errors gracefully', () => {
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
// const mockSubsonic = subsonic // const mockSubsonic = subsonic
subsonic.scrobble.mockImplementation(() => { subsonic.default.scrobble.mockImplementation(() => {
throw new Error('Scrobbling failed') throw new Error('Scrobbling failed')
}) })
@ -156,7 +159,7 @@ describe('useScrobbling', () => {
'Scrobbling error:', 'Scrobbling error:',
expect.any(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() consoleSpy.mockRestore()
}) })