Skip to content

Commit

Permalink
test: update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryce Tham committed Jan 19, 2024
1 parent 12c4ab5 commit 89e9d95
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/media/local-stream.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WebrtcCoreError } from '../errors';
import { createMockedStream } from '../util/test-utils';
import { LocalStream, LocalStreamEventNames, TrackEffect } from './local-stream';
import { StreamEventNames } from './stream';

/**
* A dummy LocalStream implementation so we can instantiate it for testing.
Expand All @@ -10,20 +11,58 @@ class TestLocalStream extends LocalStream {}
describe('LocalStream', () => {
const mockStream = createMockedStream();
let localStream: LocalStream;

beforeEach(() => {
localStream = new TestLocalStream(mockStream);
});

describe('setMuted', () => {
it('should change the input track state based on being muted & unmuted', () => {
expect.assertions(2);
let emitSpy: jest.SpyInstance;

beforeEach(() => {
localStream = new TestLocalStream(mockStream);
emitSpy = jest.spyOn(localStream[StreamEventNames.MuteStateChange], 'emit');
});

it('should change the input track enabled state and fire an event', () => {
expect.assertions(6);

// Simulate the default state of the track's enabled state.
mockStream.getTracks()[0].enabled = true;

localStream.setMuted(true);
expect(mockStream.getTracks()[0].enabled).toBe(false);
expect(emitSpy).toHaveBeenCalledTimes(1);
expect(emitSpy).toHaveBeenLastCalledWith(true);

localStream.setMuted(false);
expect(mockStream.getTracks()[0].enabled).toBe(true);
expect(emitSpy).toHaveBeenCalledTimes(2);
expect(emitSpy).toHaveBeenLastCalledWith(false);
});

it('should not fire an event if the same mute state is set twice', () => {
expect.assertions(1);

// Simulate the default state of the track's enabled state.
mockStream.getTracks()[0].enabled = true;

localStream.setMuted(false);
expect(emitSpy).toHaveBeenCalledTimes(0);
});

it('should not fire an event if the track has been muted by the browser', () => {
expect.assertions(2);

// Simulate the default state of the track's enabled state.
mockStream.getTracks()[0].enabled = true;

// Simulate the track being muted by the browser.
Object.defineProperty(mockStream.getTracks()[0], 'muted', { value: true });

localStream.setMuted(true);
expect(mockStream.getTracks()[0].enabled).toBe(false);
expect(emitSpy).toHaveBeenCalledTimes(0);
});
});

Expand Down

0 comments on commit 89e9d95

Please sign in to comment.