@@ -14,7 +14,24 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- import { VoiceRecording } from "../../src/audio/VoiceRecording" ;
17
+ import { mocked } from 'jest-mock' ;
18
+ // @ts -ignore
19
+ import Recorder from 'opus-recorder/dist/recorder.min.js' ;
20
+
21
+ import { VoiceRecording , voiceRecorderOptions , higQualityRecorderOptions } from "../../src/audio/VoiceRecording" ;
22
+ import { createAudioContext } from '../..//src/audio/compat' ;
23
+ import MediaDeviceHandler from "../../src/MediaDeviceHandler" ;
24
+
25
+ jest . mock ( 'opus-recorder/dist/recorder.min.js' ) ;
26
+ const RecorderMock = mocked ( Recorder ) ;
27
+
28
+ jest . mock ( '../../src/audio/compat' , ( ) => ( {
29
+ createAudioContext : jest . fn ( ) ,
30
+ } ) ) ;
31
+ const createAudioContextMock = mocked ( createAudioContext ) ;
32
+
33
+ jest . mock ( "../../src/MediaDeviceHandler" ) ;
34
+ const MediaDeviceHandlerMock = mocked ( MediaDeviceHandler ) ;
18
35
19
36
/**
20
37
* The tests here are heavily using access to private props.
@@ -43,6 +60,7 @@ describe("VoiceRecording", () => {
43
60
// @ts -ignore
44
61
recording . observable = {
45
62
update : jest . fn ( ) ,
63
+ close : jest . fn ( ) ,
46
64
} ;
47
65
jest . spyOn ( recording , "stop" ) . mockImplementation ( ) ;
48
66
recorderSecondsSpy = jest . spyOn ( recording , "recorderSeconds" , "get" ) ;
@@ -52,6 +70,56 @@ describe("VoiceRecording", () => {
52
70
jest . resetAllMocks ( ) ;
53
71
} ) ;
54
72
73
+ describe ( "when starting a recording" , ( ) => {
74
+ beforeEach ( ( ) => {
75
+ const mockAudioContext = {
76
+ createMediaStreamSource : jest . fn ( ) . mockReturnValue ( {
77
+ connect : jest . fn ( ) ,
78
+ disconnect : jest . fn ( ) ,
79
+ } ) ,
80
+ createScriptProcessor : jest . fn ( ) . mockReturnValue ( {
81
+ connect : jest . fn ( ) ,
82
+ disconnect : jest . fn ( ) ,
83
+ addEventListener : jest . fn ( ) ,
84
+ removeEventListener : jest . fn ( ) ,
85
+ } ) ,
86
+ destination : { } ,
87
+ close : jest . fn ( ) ,
88
+ } ;
89
+ createAudioContextMock . mockReturnValue ( mockAudioContext as unknown as AudioContext ) ;
90
+ } ) ;
91
+
92
+ afterEach ( async ( ) => {
93
+ await recording . stop ( ) ;
94
+ } ) ;
95
+
96
+ it ( "should record high-quality audio if voice processing is disabled" , async ( ) => {
97
+ MediaDeviceHandlerMock . getAudioNoiseSuppression . mockReturnValue ( false ) ;
98
+ await recording . start ( ) ;
99
+
100
+ expect ( navigator . mediaDevices . getUserMedia ) . toHaveBeenCalledWith ( expect . objectContaining ( {
101
+ audio : expect . objectContaining ( { noiseSuppression : { ideal : false } } ) ,
102
+ } ) ) ;
103
+ expect ( RecorderMock ) . toHaveBeenCalledWith ( expect . objectContaining ( {
104
+ encoderBitRate : higQualityRecorderOptions . bitrate ,
105
+ encoderApplication : higQualityRecorderOptions . encoderApplication ,
106
+ } ) ) ;
107
+ } ) ;
108
+
109
+ it ( "should record normal-quality voice if voice processing is enabled" , async ( ) => {
110
+ MediaDeviceHandlerMock . getAudioNoiseSuppression . mockReturnValue ( true ) ;
111
+ await recording . start ( ) ;
112
+
113
+ expect ( navigator . mediaDevices . getUserMedia ) . toHaveBeenCalledWith ( expect . objectContaining ( {
114
+ audio : expect . objectContaining ( { noiseSuppression : { ideal : true } } ) ,
115
+ } ) ) ;
116
+ expect ( RecorderMock ) . toHaveBeenCalledWith ( expect . objectContaining ( {
117
+ encoderBitRate : voiceRecorderOptions . bitrate ,
118
+ encoderApplication : voiceRecorderOptions . encoderApplication ,
119
+ } ) ) ;
120
+ } ) ;
121
+ } ) ;
122
+
55
123
describe ( "when recording" , ( ) => {
56
124
beforeEach ( ( ) => {
57
125
// @ts -ignore
0 commit comments