I'm writing a test that needs to simulate and provide accessor facilities for validating my Audio implementation is correct. I successfully implemented my setup method like so:
import "regenerator-runtime/runtime";
import { AudioState } from '../src';
export class MockAudio extends Audio {
src: string;
state: AudioState;
duration: number;
playing: boolean;
constructor() {
super();
this.playing = false;
this.duration = NaN;
this.state = AudioState.STOPPED;
this.src = '';
}
fastSeek = (time: number): Promise<void> => {
this.currentTime = time;
return Promise.resolve();
}
play = (): Promise<void> => {
this.playing = true;
this.state = AudioState.PLAYING;
return super.play();
}
pause = (): void => {
this.playing = false;
this.state = AudioState.PAUSED;
return super.pause();
}
}
HTMLMediaElement.prototype.pause = () => Promise.resolve();
HTMLMediaElement.prototype.play = () => Promise.resolve();
window.Audio = MockAudio;
Object.defineProperty(window, 'MediaSource', {
writable: true,
value: jest.fn().mockImplementation((params) => ({
// MediaSource implementation goes here
addEventListener: jest.fn(),
})),
});
As you can see I'm setting the methods up so that they can effect the object properties in a way that simulates the Audio API. I'm wondering how I can do this properly with mockImplementation and defineProperty?