Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

standardized-audio-context-mock

Package Overview
Dependencies
Maintainers
0
Versions
421
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

standardized-audio-context-mock

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

  • 9.7.14
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20K
increased by42.18%
Maintainers
0
Weekly downloads
 
Created
Source

standardized-audio-context-mock

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

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:

// File `./play.ts`
import { IAudioBuffer, IAudioContext } from 'standardized-audio-context';

export 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:

// File `./play.test.js`
import { AudioBuffer, AudioContext, registrar } from 'standardized-audio-context-mock';
import { play } from './play';

describe('play()', () => {
    let audioBufferMock;
    let audioContextMock;

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

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

    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;
    });
});

Keywords

FAQs

Package last updated on 02 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc