import { afterEach, describe, expect, it, vi } from 'vitest' import { NATIVE_PLAYER_ATTRIBUTE, nativePlayerProps, pauseNativeMediaPlayers, } from './nativeMediaPlayers' describe('pauseNativeMediaPlayers', () => { afterEach(() => { document.body.innerHTML = '' vi.restoreAllMocks() }) const mountPlayer = (marked: boolean): HTMLAudioElement => { const player = document.createElement('audio') if (marked) player.setAttribute(NATIVE_PLAYER_ATTRIBUTE, '') document.body.appendChild(player) return player } // jsdom leaves every `HTMLMediaElement` playback method unimplemented, // so the spy stands in for the real one. const spyOnPause = (player: HTMLMediaElement) => vi.spyOn(player, 'pause').mockImplementation(() => {}) it('pauses enrolled players and leaves everything else alone', () => { const enrolled = spyOnPause(mountPlayer(true)) const foreign = spyOnPause(mountPlayer(false)) pauseNativeMediaPlayers() expect(enrolled).toHaveBeenCalledTimes(1) // A host app's own media is not the toolkit's to stop. expect(foreign).not.toHaveBeenCalled() }) it('skips the excluded player', () => { const keepPlaying = mountPlayer(true) const other = mountPlayer(true) const keepPlayingPause = spyOnPause(keepPlaying) const otherPause = spyOnPause(other) pauseNativeMediaPlayers(keepPlaying) expect(keepPlayingPause).not.toHaveBeenCalled() expect(otherPause).toHaveBeenCalledTimes(1) }) it('marks players with the attribute the query looks for', () => { // The props object and the selector have to agree, or enrolment is // silently a no-op. expect(nativePlayerProps).toEqual({ [NATIVE_PLAYER_ATTRIBUTE]: '' }) }) })