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(() => {
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))

View File

@ -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),

View File

@ -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()
})