1- import createVoiceSearchHelper , {
2- VoiceSearchHelper ,
3- VoiceSearchHelperParams ,
4- } from '..' ;
5-
6- const getVoiceSearchHelper = (
7- opts ?: VoiceSearchHelperParams
8- ) : VoiceSearchHelper =>
9- createVoiceSearchHelper (
10- opts || {
11- searchAsYouSpeak : false ,
12- onQueryChange : ( ) => { } ,
13- onStateChange : ( ) => { } ,
14- }
15- ) ;
1+ import createVoiceSearchHelper from '..' ;
162
173type DummySpeechRecognition = ( ) => void ;
184declare global {
@@ -22,14 +8,35 @@ declare global {
228 }
239}
2410
11+ const start = jest . fn ( ) ;
12+ const stop = jest . fn ( ) ;
13+
14+ const createFakeSpeechRecognition = ( ) : jest . Mock => {
15+ const simulateListener : any = { } ;
16+ const mock = jest . fn ( ) . mockImplementation ( ( ) => ( {
17+ start,
18+ stop,
19+ addEventListener ( eventName : string , callback : ( ) => void ) {
20+ simulateListener [ eventName ] = callback ;
21+ } ,
22+ removeEventListener ( ) { } ,
23+ } ) ) ;
24+ ( mock as any ) . simulateListener = simulateListener ;
25+ return mock ;
26+ } ;
27+
2528describe ( 'VoiceSearchHelper' , ( ) => {
26- beforeEach ( ( ) => {
29+ afterEach ( ( ) => {
2730 delete window . webkitSpeechRecognition ;
2831 delete window . SpeechRecognition ;
2932 } ) ;
3033
3134 it ( 'has initial state correctly' , ( ) => {
32- const voiceSearchHelper = getVoiceSearchHelper ( ) ;
35+ const voiceSearchHelper = createVoiceSearchHelper ( {
36+ searchAsYouSpeak : false ,
37+ onQueryChange : ( ) => { } ,
38+ onStateChange : ( ) => { } ,
39+ } ) ;
3340 expect ( voiceSearchHelper . getState ( ) ) . toEqual ( {
3441 errorCode : undefined ,
3542 isSpeechFinal : false ,
@@ -39,38 +46,49 @@ describe('VoiceSearchHelper', () => {
3946 } ) ;
4047
4148 it ( 'is not supported' , ( ) => {
42- const voiceSearchHelper = getVoiceSearchHelper ( ) ;
49+ const voiceSearchHelper = createVoiceSearchHelper ( {
50+ searchAsYouSpeak : false ,
51+ onQueryChange : ( ) => { } ,
52+ onStateChange : ( ) => { } ,
53+ } ) ;
4354 expect ( voiceSearchHelper . isBrowserSupported ( ) ) . toBe ( false ) ;
4455 } ) ;
4556
4657 it ( 'is not listening' , ( ) => {
47- const voiceSearchHelper = getVoiceSearchHelper ( ) ;
58+ const voiceSearchHelper = createVoiceSearchHelper ( {
59+ searchAsYouSpeak : false ,
60+ onQueryChange : ( ) => { } ,
61+ onStateChange : ( ) => { } ,
62+ } ) ;
4863 expect ( voiceSearchHelper . isListening ( ) ) . toBe ( false ) ;
4964 } ) ;
5065
5166 it ( 'is supported with webkitSpeechRecognition' , ( ) => {
5267 window . webkitSpeechRecognition = ( ) => { } ;
53- const voiceSearchHelper = getVoiceSearchHelper ( ) ;
68+ const voiceSearchHelper = createVoiceSearchHelper ( {
69+ searchAsYouSpeak : false ,
70+ onQueryChange : ( ) => { } ,
71+ onStateChange : ( ) => { } ,
72+ } ) ;
5473 expect ( voiceSearchHelper . isBrowserSupported ( ) ) . toBe ( true ) ;
5574 } ) ;
5675
5776 it ( 'is supported with SpeechRecognition' , ( ) => {
58- window . SpeechRecognition = ( ) => { } ;
59- const voiceSearchHelper = getVoiceSearchHelper ( ) ;
77+ window . SpeechRecognition = createFakeSpeechRecognition ( ) ;
78+ const voiceSearchHelper = createVoiceSearchHelper ( {
79+ searchAsYouSpeak : false ,
80+ onQueryChange : ( ) => { } ,
81+ onStateChange : ( ) => { } ,
82+ } ) ;
6083 expect ( voiceSearchHelper . isBrowserSupported ( ) ) . toBe ( true ) ;
6184 } ) ;
6285
6386 it ( 'works with mock SpeechRecognition (searchAsYouSpeak:false)' , ( ) => {
64- let recognition ;
65- window . SpeechRecognition = jest . fn ( ) . mockImplementation ( ( ) => ( {
66- start ( ) {
67- /* eslint-disable-next-line consistent-this */
68- recognition = this ;
69- } ,
70- } ) ) ;
87+ window . SpeechRecognition = createFakeSpeechRecognition ( ) ;
88+ const { simulateListener } = window . SpeechRecognition as any ;
7189 const onQueryChange = jest . fn ( ) ;
7290 const onStateChange = jest . fn ( ) ;
73- const voiceSearchHelper = getVoiceSearchHelper ( {
91+ const voiceSearchHelper = createVoiceSearchHelper ( {
7492 searchAsYouSpeak : false ,
7593 onQueryChange,
7694 onStateChange,
@@ -79,9 +97,9 @@ describe('VoiceSearchHelper', () => {
7997 voiceSearchHelper . toggleListening ( ) ;
8098 expect ( onStateChange ) . toHaveBeenCalledTimes ( 1 ) ;
8199 expect ( voiceSearchHelper . getState ( ) . status ) . toEqual ( 'askingPermission' ) ;
82- recognition . onstart ( ) ;
100+ simulateListener . start ( ) ;
83101 expect ( voiceSearchHelper . getState ( ) . status ) . toEqual ( 'waiting' ) ;
84- recognition . onresult ( {
102+ simulateListener . result ( {
85103 results : [
86104 ( ( ) => {
87105 const obj = [
@@ -98,22 +116,17 @@ describe('VoiceSearchHelper', () => {
98116 expect ( voiceSearchHelper . getState ( ) . transcript ) . toEqual ( 'Hello World' ) ;
99117 expect ( voiceSearchHelper . getState ( ) . isSpeechFinal ) . toBe ( true ) ;
100118 expect ( onQueryChange ) . toHaveBeenCalledTimes ( 0 ) ;
101- recognition . onend ( ) ;
119+ simulateListener . end ( ) ;
102120 expect ( onQueryChange ) . toHaveBeenCalledWith ( 'Hello World' ) ;
103121 expect ( voiceSearchHelper . getState ( ) . status ) . toEqual ( 'finished' ) ;
104122 } ) ;
105123
106124 it ( 'works with mock SpeechRecognition (searchAsYouSpeak:true)' , ( ) => {
107- let recognition ;
108- window . SpeechRecognition = jest . fn ( ) . mockImplementation ( ( ) => ( {
109- start ( ) {
110- /* eslint-disable-next-line consistent-this */
111- recognition = this ;
112- } ,
113- } ) ) ;
125+ window . SpeechRecognition = createFakeSpeechRecognition ( ) ;
126+ const { simulateListener } = window . SpeechRecognition as any ;
114127 const onQueryChange = jest . fn ( ) ;
115128 const onStateChange = jest . fn ( ) ;
116- const voiceSearchHelper = getVoiceSearchHelper ( {
129+ const voiceSearchHelper = createVoiceSearchHelper ( {
117130 searchAsYouSpeak : true ,
118131 onQueryChange,
119132 onStateChange,
@@ -122,9 +135,9 @@ describe('VoiceSearchHelper', () => {
122135 voiceSearchHelper . toggleListening ( ) ;
123136 expect ( onStateChange ) . toHaveBeenCalledTimes ( 1 ) ;
124137 expect ( voiceSearchHelper . getState ( ) . status ) . toEqual ( 'askingPermission' ) ;
125- recognition . onstart ( ) ;
138+ simulateListener . start ( ) ;
126139 expect ( voiceSearchHelper . getState ( ) . status ) . toEqual ( 'waiting' ) ;
127- recognition . onresult ( {
140+ simulateListener . result ( {
128141 results : [
129142 ( ( ) => {
130143 const obj = [
@@ -141,34 +154,41 @@ describe('VoiceSearchHelper', () => {
141154 expect ( voiceSearchHelper . getState ( ) . transcript ) . toEqual ( 'Hello World' ) ;
142155 expect ( voiceSearchHelper . getState ( ) . isSpeechFinal ) . toBe ( true ) ;
143156 expect ( onQueryChange ) . toHaveBeenCalledWith ( 'Hello World' ) ;
144- recognition . onend ( ) ;
157+ simulateListener . end ( ) ;
145158 expect ( onQueryChange ) . toHaveBeenCalledTimes ( 1 ) ;
146159 expect ( voiceSearchHelper . getState ( ) . status ) . toEqual ( 'finished' ) ;
147160 } ) ;
148161
149162 it ( 'works with onerror' , ( ) => {
150- let recognition ;
151- window . SpeechRecognition = jest . fn ( ) . mockImplementation ( ( ) => ( {
152- start ( ) {
153- /* eslint-disable-next-line consistent-this */
154- recognition = this ;
155- } ,
156- } ) ) ;
163+ window . SpeechRecognition = createFakeSpeechRecognition ( ) ;
164+ const { simulateListener } = window . SpeechRecognition as any ;
157165 const onQueryChange = jest . fn ( ) ;
158166 const onStateChange = jest . fn ( ) ;
159- const voiceSearchHelper = getVoiceSearchHelper ( {
167+ const voiceSearchHelper = createVoiceSearchHelper ( {
160168 searchAsYouSpeak : true ,
161169 onQueryChange,
162170 onStateChange,
163171 } ) ;
164172 voiceSearchHelper . toggleListening ( ) ;
165173 expect ( voiceSearchHelper . getState ( ) . status ) . toEqual ( 'askingPermission' ) ;
166- recognition . onerror ( {
174+ simulateListener . error ( {
167175 error : 'not-allowed' ,
168176 } ) ;
169177 expect ( voiceSearchHelper . getState ( ) . status ) . toEqual ( 'error' ) ;
170178 expect ( voiceSearchHelper . getState ( ) . errorCode ) . toEqual ( 'not-allowed' ) ;
171- recognition . onend ( ) ;
179+ simulateListener . end ( ) ;
172180 expect ( onQueryChange ) . toHaveBeenCalledTimes ( 0 ) ;
173181 } ) ;
182+
183+ it ( 'stops listening on `dispose`' , ( ) => {
184+ window . SpeechRecognition = createFakeSpeechRecognition ( ) ;
185+ const voiceSearchHelper = createVoiceSearchHelper ( {
186+ searchAsYouSpeak : false ,
187+ onQueryChange : ( ) => { } ,
188+ onStateChange : ( ) => { } ,
189+ } ) ;
190+ voiceSearchHelper . toggleListening ( ) ;
191+ voiceSearchHelper . dispose ( ) ;
192+ expect ( stop ) . toHaveBeenCalledTimes ( 1 ) ;
193+ } ) ;
174194} ) ;
0 commit comments