Skip to content

Commit

Permalink
🧪 [Abstract] Add volume and mute event tests
Browse files Browse the repository at this point in the history
  • Loading branch information
beefchimi committed Dec 21, 2023
1 parent a40ce7f commit e84967a
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/tests/Abstract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {afterEach, describe, it, expect, vi} from 'vitest';

import {msToSec} from '../utilities';
import {Sound} from '../Sound';
import type {SoundConfig} from '../types';
import type {SoundConfig, SoundEventMap} from '../types';

// This test covers any shared implementation between
// each component. Eventually, I will create a proper
Expand Down Expand Up @@ -69,6 +69,27 @@ describe('Abstract implementation', () => {
// `gain.value` is `0`.
expect(spyGainRamp).toBeCalledWith(0, endTime);
});

it('triggers mute event when set to a unique value', async () => {
const spyMuteChange: SoundEventMap['mute'] = vi.fn((_state) => {});

mockSound.on('mute', spyMuteChange);
expect(spyMuteChange).not.toBeCalled();

mockSound.mute = false;
expect(spyMuteChange).not.toBeCalled();

mockSound.mute = true;
expect(spyMuteChange).toBeCalledWith(true);

mockSound.mute = false;
expect(spyMuteChange).toBeCalledWith(false);

mockSound.off('mute', spyMuteChange);

mockSound.mute = true;
expect(spyMuteChange).not.toHaveBeenLastCalledWith(true);
});
});

describe('volume', () => {
Expand Down Expand Up @@ -188,5 +209,31 @@ describe('Abstract implementation', () => {
// `gain.value` is `newValue`.
expect(spyGainRamp).toBeCalledWith(newValue, endTime);
});

it('triggers volume event when set to a unique value (regardless of mute state)', async () => {
const spyVolumeChange: SoundEventMap['volume'] = vi.fn((_state) => {});

mockSound.on('volume', spyVolumeChange);
expect(spyVolumeChange).not.toBeCalled();

mockSound.volume = 1;
expect(spyVolumeChange).not.toBeCalled();

mockSound.mute = true;
mockSound.volume = 0.8;
expect(spyVolumeChange).toBeCalledWith(0.8);

mockSound.mute = false;
mockSound.volume = 0;
expect(spyVolumeChange).toBeCalledWith(0);

mockSound.volume = 1;
expect(spyVolumeChange).toBeCalledWith(1);

mockSound.off('volume', spyVolumeChange);

mockSound.volume = 0.6;
expect(spyVolumeChange).not.toBeCalledWith(0.6);
});
});
});

0 comments on commit e84967a

Please sign in to comment.