This commit is contained in:
84
tests/unit/GlobalAudioPlayerLanguage.spec.ts
Normal file
84
tests/unit/GlobalAudioPlayerLanguage.spec.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const repositoryMocks = vi.hoisted(() => ({
|
||||
getPlayInfo: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('@/repositories/AudioPlayInfoRepository', () => ({
|
||||
audioPlayInfoRepository: {
|
||||
getPlayInfo: repositoryMocks.getPlayInfo
|
||||
},
|
||||
audioReasonToText: () => '当前语言暂无语音讲解'
|
||||
}))
|
||||
|
||||
import { useGlobalAudioPlayer } from '@/composables/useGlobalAudioPlayer'
|
||||
|
||||
describe('全局讲解播放器语言切换', () => {
|
||||
const player = useGlobalAudioPlayer()
|
||||
|
||||
beforeEach(() => {
|
||||
player.close()
|
||||
repositoryMocks.getPlayInfo.mockReset()
|
||||
vi.stubGlobal('uni', {
|
||||
showToast: vi.fn()
|
||||
})
|
||||
vi.spyOn(window.HTMLMediaElement.prototype, 'play').mockResolvedValue(undefined)
|
||||
vi.spyOn(window.HTMLMediaElement.prototype, 'pause').mockImplementation(() => undefined)
|
||||
vi.spyOn(window.HTMLMediaElement.prototype, 'load').mockImplementation(() => undefined)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
player.close()
|
||||
vi.restoreAllMocks()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('切换到粤语时立即中断旧音频,并使用 yue-HK 播放地址', async () => {
|
||||
repositoryMocks.getPlayInfo.mockResolvedValue({
|
||||
playable: true,
|
||||
targetType: 'STOP',
|
||||
targetId: '1823450596800289',
|
||||
lang: 'yue-HK',
|
||||
audioId: 'cantonese-audio',
|
||||
playUrl: '/museum-assets/audio/cantonese.mp3',
|
||||
hasText: true,
|
||||
fallback: false
|
||||
})
|
||||
|
||||
await player.play({
|
||||
id: 'mandarin-audio',
|
||||
name: '测试讲解',
|
||||
audioUrl: '/museum-assets/audio/mandarin.mp3',
|
||||
language: 'zh-CN'
|
||||
}, {
|
||||
source: {
|
||||
targetType: 'STOP',
|
||||
targetId: '1823450596800289',
|
||||
lang: 'zh-CN'
|
||||
}
|
||||
})
|
||||
|
||||
const pauseSpy = vi.mocked(window.HTMLMediaElement.prototype.pause)
|
||||
pauseSpy.mockClear()
|
||||
const switching = player.switchLanguage('yue-HK')
|
||||
|
||||
expect(pauseSpy).toHaveBeenCalledTimes(1)
|
||||
expect(player.playing.value).toBe(false)
|
||||
expect(player.currentTime.value).toBe(0)
|
||||
expect(repositoryMocks.getPlayInfo).toHaveBeenCalledWith(expect.objectContaining({
|
||||
targetType: 'STOP',
|
||||
targetId: '1823450596800289',
|
||||
lang: 'yue-HK',
|
||||
refresh: true
|
||||
}))
|
||||
|
||||
await expect(switching).resolves.toBe(true)
|
||||
expect(player.currentSource.value?.lang).toBe('yue-HK')
|
||||
expect(player.currentAudio.value).toEqual(expect.objectContaining({
|
||||
language: 'yue-HK',
|
||||
audioUrl: '/museum-assets/audio/cantonese.mp3'
|
||||
}))
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user