1
1
import { WebrtcCoreError } from '../errors' ;
2
2
import { createMockedStream } from '../util/test-utils' ;
3
3
import { LocalStream , LocalStreamEventNames , TrackEffect } from './local-stream' ;
4
+ import { StreamEventNames } from './stream' ;
4
5
5
6
/**
6
7
* A dummy LocalStream implementation so we can instantiate it for testing.
@@ -10,20 +11,58 @@ class TestLocalStream extends LocalStream {}
10
11
describe ( 'LocalStream' , ( ) => {
11
12
const mockStream = createMockedStream ( ) ;
12
13
let localStream : LocalStream ;
14
+
13
15
beforeEach ( ( ) => {
14
16
localStream = new TestLocalStream ( mockStream ) ;
15
17
} ) ;
16
18
17
19
describe ( 'setMuted' , ( ) => {
18
- it ( 'should change the input track state based on being muted & unmuted' , ( ) => {
19
- expect . assertions ( 2 ) ;
20
+ let emitSpy : jest . SpyInstance ;
21
+
22
+ beforeEach ( ( ) => {
23
+ localStream = new TestLocalStream ( mockStream ) ;
24
+ emitSpy = jest . spyOn ( localStream [ StreamEventNames . MuteStateChange ] , 'emit' ) ;
25
+ } ) ;
26
+
27
+ it ( 'should change the input track enabled state and fire an event' , ( ) => {
28
+ expect . assertions ( 6 ) ;
29
+
20
30
// Simulate the default state of the track's enabled state.
21
31
mockStream . getTracks ( ) [ 0 ] . enabled = true ;
22
32
23
33
localStream . setMuted ( true ) ;
24
34
expect ( mockStream . getTracks ( ) [ 0 ] . enabled ) . toBe ( false ) ;
35
+ expect ( emitSpy ) . toHaveBeenCalledTimes ( 1 ) ;
36
+ expect ( emitSpy ) . toHaveBeenLastCalledWith ( true ) ;
37
+
25
38
localStream . setMuted ( false ) ;
26
39
expect ( mockStream . getTracks ( ) [ 0 ] . enabled ) . toBe ( true ) ;
40
+ expect ( emitSpy ) . toHaveBeenCalledTimes ( 2 ) ;
41
+ expect ( emitSpy ) . toHaveBeenLastCalledWith ( false ) ;
42
+ } ) ;
43
+
44
+ it ( 'should not fire an event if the same mute state is set twice' , ( ) => {
45
+ expect . assertions ( 1 ) ;
46
+
47
+ // Simulate the default state of the track's enabled state.
48
+ mockStream . getTracks ( ) [ 0 ] . enabled = true ;
49
+
50
+ localStream . setMuted ( false ) ;
51
+ expect ( emitSpy ) . toHaveBeenCalledTimes ( 0 ) ;
52
+ } ) ;
53
+
54
+ it ( 'should not fire an event if the track has been muted by the browser' , ( ) => {
55
+ expect . assertions ( 2 ) ;
56
+
57
+ // Simulate the default state of the track's enabled state.
58
+ mockStream . getTracks ( ) [ 0 ] . enabled = true ;
59
+
60
+ // Simulate the track being muted by the browser.
61
+ Object . defineProperty ( mockStream . getTracks ( ) [ 0 ] , 'muted' , { value : true } ) ;
62
+
63
+ localStream . setMuted ( true ) ;
64
+ expect ( mockStream . getTracks ( ) [ 0 ] . enabled ) . toBe ( false ) ;
65
+ expect ( emitSpy ) . toHaveBeenCalledTimes ( 0 ) ;
27
66
} ) ;
28
67
} ) ;
29
68
0 commit comments