Skip to content

ThomasSimoens/standardized-audio-context-mock

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

standardized-audio-context-mock

A mocked version of the standardized-audio-context module.

tests dependencies version

This library is meant to test code which is using standardized-audio-context without acutally rendering any audio.

It does depend on Sinon.JS to do the mocking.

Usage

standardized-audio-context-mock is published on npm and can be installed as usual.

npm install standardized-audio-context-mock

Testing

Let's say you have the following code that you want to test:

import { IAudioBuffer, IAudioContext } from 'standardized-audio-context';

const play = (audioBuffer: IAudioBuffer, audioContext: IAudioContext) => {
    const audioBufferSourceNode = audioContext.createBufferSource();

    audioBufferSourceNode.buffer = audioBuffer;
    audioBufferSourceNode.connect(audioContext.destination);

    audioBufferSourceNode.start();
};

A test suite for the play() function which will run with Mocha and Chai and uses standardized-audio-context-mock might look like this:

import { AudioBufferMock, AudioContextMock, registrar } from 'standardized-audio-context-mock';

describe('play()', () => {

    let audioBufferMock;
    let audioContextMock;

    afterEach(() => registrar.reset());

    beforeEach(() => {
        audioBufferMock = new AudioBufferMock({ length: 10, sampleRate: 44100 });
        audioContextMock = new AudioContextMock();
    });

    it('should create a new AudioBufferSourceNode', () => {
        play(audioBufferMock, audioContextMock);

        expect(registrar.getAudioNodes(audioContextMock, 'AudioBufferSourceNode')).to.have.a.lengthOf(1);
    });

    it('should set the buffer property of the AudioBufferSourceNode', () => {
        play(audioBufferMock, audioContextMock);

        const [ audioBufferSourceNodeMock ] = registrar.getAudioNodes(audioContextMock, 'AudioBufferSourceNode');

        expect(audioBufferSourceNodeMock.buffer).to.equal(audioBufferMock);
    });

    it('should connect the AudioBufferSourceNode with to destination', () => {
        play(audioBufferMock, audioContextMock);

        const [ audioBufferSourceNodeMock ] = registrar.getAudioNodes(audioContextMock, 'AudioBufferSourceNode');

        expect(audioBufferSourceNodeMock.connect).to.have.been.calledOnce;
        expect(audioBufferSourceNodeMock.connect).to.have.been.calledWithExactly(audioContextMock.destination);
    });

    it('should start the AudioBufferSourceNode', () => {
        play(audioBufferMock, audioContextMock);

        const [ audioBufferSourceNodeMock ] = registrar.getAudioNodes(audioContextMock, 'AudioBufferSourceNode');

        expect(audioBufferSourceNodeMock.start).to.have.been.calledOnce;
    });

});

About

A mocked version of the standardized-audio-context module.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 51.2%
  • JavaScript 48.8%