Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chrisguttandin/standardized-audio-context-mock
A mocked version of the standardized-audio-context module.
https://github.com/chrisguttandin/standardized-audio-context-mock
testing web-audio
Last synced: 13 days ago
JSON representation
A mocked version of the standardized-audio-context module.
- Host: GitHub
- URL: https://github.com/chrisguttandin/standardized-audio-context-mock
- Owner: chrisguttandin
- License: mit
- Created: 2016-02-13T00:34:11.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-10-18T21:08:10.000Z (20 days ago)
- Last Synced: 2024-10-24T12:25:20.672Z (15 days ago)
- Topics: testing, web-audio
- Language: JavaScript
- Size: 14 MB
- Stars: 20
- Watchers: 3
- Forks: 8
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# standardized-audio-context-mock
**A mocked version of the standardized-audio-context module.**
[![version](https://img.shields.io/npm/v/standardized-audio-context-mock.svg?style=flat-square)](https://www.npmjs.com/package/standardized-audio-context-mock)
This library is meant to test code which is using [`standardized-audio-context`](https://github.com/chrisguttandin/standardized-audio-context) without acutally rendering any audio.
It does depend on [Sinon.JS](https://sinonjs.org) to do the mocking.
## Usage
`standardized-audio-context-mock` is published on
[npm](https://www.npmjs.com/package/standardized-audio-context-mock) and can be installed as usual.```shell
npm install standardized-audio-context-mock
```## Testing
Let's say you have the following code that you want to test:
```typescript
// 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](https://mochajs.org) and [Chai](https://www.chaijs.com) and uses `standardized-audio-context-mock` might look like this:
```js
// 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;
});
});
```