Skip to content

Commit

Permalink
test(audio): enhance audio service tests for play/pause functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcatlait committed Sep 27, 2024
1 parent 9b2da01 commit d20fa23
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
41 changes: 35 additions & 6 deletions apps/web/src/app/core/services/audio.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { API_URL } from '@core/tokens'

const mediaPlayerMock: PartiallyMocked<MediaPlayerClass> = {
initialize: vi.fn(),
isPaused: vi.fn(),
on: vi.fn(),
off: vi.fn(),
attachSource: vi.fn(),
Expand Down Expand Up @@ -58,22 +59,50 @@ describe('AudioService', () => {
vi.clearAllMocks()
})

it('should play audio', () => {
it('should not play audio when player is not paused', () => {
// Arrange
mediaPlayerMock.isPaused?.mockImplementation(() => false)

// Act
audioService.play()

// Assert
expect(mediaPlayerMock.play).not.toHaveBeenCalled()
})

it('should play audio when player is paused', () => {
// Arrange
mediaPlayerMock.isPaused?.mockImplementation(() => true)

// Act
audioService.play()

// Assert
expect(mediaPlayerMock.play).toHaveBeenCalled()
})

it('should pause audio', () => {
it('should pause audio when player is not paused', () => {
// Arrange
mediaPlayerMock.isPaused?.mockImplementation(() => false)

// Act
audioService.pause()

// Assert
expect(mediaPlayerMock.pause).toHaveBeenCalled()
})

it('should not pause audio when player is paused', () => {
// Arrange
mediaPlayerMock.isPaused?.mockImplementation(() => true)

// Act
audioService.pause()

// Assert
expect(mediaPlayerMock.pause).not.toHaveBeenCalled()
})

it('should seek to specified time', () => {
// Arrange
const currentTime = 30
Expand Down Expand Up @@ -156,14 +185,14 @@ describe('AudioService', () => {
)
})

it('should dispatch audio play action on loaded manifest', () => {
it('should dispatch audio play action on player ready', () => {
// Arrange
const playSpy = vi.fn()
const injectorSpy = vi.spyOn(injector, 'get').mockReturnValue({ play: playSpy })

// Act
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(audioService as any).onManifestLoaded()
;(audioService as any).onCanPlay()

// Assert
expect(injectorSpy).toHaveBeenCalledWith(AudioState)
Expand Down Expand Up @@ -260,7 +289,7 @@ describe('AudioService', () => {
;(audioService as any).registerEvents()

// Assert
expect(mediaPlayerMock.on).toHaveBeenCalledWith(AudioEvents.MANIFEST_LOADED, expect.any(Function))
expect(mediaPlayerMock.on).toHaveBeenCalledWith(AudioEvents.CAN_PLAY, expect.any(Function))
expect(mediaPlayerMock.on).toHaveBeenCalledWith(AudioEvents.PLAYBACK_TIME_UPDATED, expect.any(Function))
expect(mediaPlayerMock.on).toHaveBeenCalledWith(AudioEvents.PLAYBACK_ENDED, expect.any(Function))
})
Expand All @@ -271,7 +300,7 @@ describe('AudioService', () => {
;(audioService as any).removeEvents()

// Assert
expect(mediaPlayerMock.off).toHaveBeenCalledWith(AudioEvents.MANIFEST_LOADED, expect.any(Function))
expect(mediaPlayerMock.off).toHaveBeenCalledWith(AudioEvents.CAN_PLAY, expect.any(Function))
expect(mediaPlayerMock.off).toHaveBeenCalledWith(AudioEvents.PLAYBACK_TIME_UPDATED, expect.any(Function))
expect(mediaPlayerMock.off).toHaveBeenCalledWith(AudioEvents.PLAYBACK_ENDED, expect.any(Function))
})
Expand Down
5 changes: 4 additions & 1 deletion apps/web/src/app/core/state/playback.state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ describe('PlaybackState', () => {
it('should set current track, queue, load audio, and open visualizer when toggling play with new track', () => {
// Arrange
const trackId = 'track2'
const queue = { source: { name: 'source2' }, tracks: [{ id: 'track1' }, { id: trackId }] } as Queue
const queue = {
source: { name: 'source2', entityId: 'newQueue' },
tracks: [{ id: 'track1' }, { id: trackId }],
} as Queue

// Act
playbackState.togglePlay({ queue, trackId })
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/app/shared/pipes/image-url.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ describe('ImageUrlPipe', () => {
pipe = new ImageUrlPipe()
})

it('should return undefined for empty or undefined input', () => {
it('should return empty string for empty or undefined input', () => {
// Act & Assert
expect(pipe.transform(undefined)).toBeUndefined()
expect(pipe.transform([])).toBeUndefined()
expect(pipe.transform(undefined)).toBe('')
expect(pipe.transform([])).toBe('')
})

it('should return the correct URL for the default size (SMALL)', () => {
Expand Down

0 comments on commit d20fa23

Please sign in to comment.