From ed2d61a9f5d2784bdc6de99b8fe0d1886e8bb1ab Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Wed, 15 May 2019 16:06:37 +0200 Subject: [PATCH] fix(types): improve types for voiceSearch (#3778) * fix(types): make isSpeechFinal non-optional boolean * fix(types): make transcript non-optional string * chore(voiceSearch): assign an empty string if no transcript --- .../VoiceSearch/__tests__/VoiceSearch-test.tsx | 2 ++ src/lib/voiceSearchHelper/__tests__/index-test.ts | 4 ++-- src/lib/voiceSearchHelper/index.ts | 15 ++++++++------- .../__snapshots__/voice-search-test.ts.snap | 12 ++++++------ stories/voice-search.stories.ts | 2 +- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/components/VoiceSearch/__tests__/VoiceSearch-test.tsx b/src/components/VoiceSearch/__tests__/VoiceSearch-test.tsx index 72ff198118..583a4513dc 100644 --- a/src/components/VoiceSearch/__tests__/VoiceSearch-test.tsx +++ b/src/components/VoiceSearch/__tests__/VoiceSearch-test.tsx @@ -13,6 +13,8 @@ const defaultProps: VoiceSearchProps = { toggleListening: () => {}, voiceListeningState: { status: 'initial', + isSpeechFinal: false, + transcript: '', }, templates: { buttonText: 'button', diff --git a/src/lib/voiceSearchHelper/__tests__/index-test.ts b/src/lib/voiceSearchHelper/__tests__/index-test.ts index 91d31d9ee3..2ae8a05c03 100644 --- a/src/lib/voiceSearchHelper/__tests__/index-test.ts +++ b/src/lib/voiceSearchHelper/__tests__/index-test.ts @@ -32,9 +32,9 @@ describe('VoiceSearchHelper', () => { const voiceSearchHelper = getVoiceSearchHelper(); expect(voiceSearchHelper.getState()).toEqual({ errorCode: undefined, - isSpeechFinal: undefined, + isSpeechFinal: false, status: 'initial', - transcript: undefined, + transcript: '', }); }); diff --git a/src/lib/voiceSearchHelper/index.ts b/src/lib/voiceSearchHelper/index.ts index 8d71c96b9c..77ddc9765f 100644 --- a/src/lib/voiceSearchHelper/index.ts +++ b/src/lib/voiceSearchHelper/index.ts @@ -13,8 +13,8 @@ export type VoiceSearchHelperParams = { export type VoiceListeningState = { status: string; - transcript?: string; - isSpeechFinal?: boolean; + transcript: string; + isSpeechFinal: boolean; errorCode?: string; }; @@ -37,8 +37,8 @@ export default function voiceSearchHelper({ (window as any).SpeechRecognition; const getDefaultState = (status: string): VoiceListeningState => ({ status, - transcript: undefined, - isSpeechFinal: undefined, + transcript: '', + isSpeechFinal: false, errorCode: undefined, }); let state: VoiceListeningState = getDefaultState(STATUS_INITIAL); @@ -89,9 +89,10 @@ export default function voiceSearchHelper({ setState({ status: STATUS_RECOGNIZING, transcript: - event.results[0] && - event.results[0][0] && - event.results[0][0].transcript, + (event.results[0] && + event.results[0][0] && + event.results[0][0].transcript) || + '', isSpeechFinal: event.results[0] && event.results[0].isFinal, }); if (searchAsYouSpeak && state.transcript) { diff --git a/src/widgets/voice-search/__tests__/__snapshots__/voice-search-test.ts.snap b/src/widgets/voice-search/__tests__/__snapshots__/voice-search-test.ts.snap index 6f49e2af45..da42748063 100644 --- a/src/widgets/voice-search/__tests__/__snapshots__/voice-search-test.ts.snap +++ b/src/widgets/voice-search/__tests__/__snapshots__/voice-search-test.ts.snap @@ -21,9 +21,9 @@ exports[`voiceSearch() Rendering renders during init() 1`] = ` voiceListeningState={ Object { "errorCode": undefined, - "isSpeechFinal": undefined, + "isSpeechFinal": false, "status": "initial", - "transcript": undefined, + "transcript": "", } } /> @@ -50,9 +50,9 @@ exports[`voiceSearch() Rendering renders during render() 1`] = ` voiceListeningState={ Object { "errorCode": undefined, - "isSpeechFinal": undefined, + "isSpeechFinal": false, "status": "initial", - "transcript": undefined, + "transcript": "", } } /> @@ -79,9 +79,9 @@ exports[`voiceSearch() Rendering renders during render() 2`] = ` voiceListeningState={ Object { "errorCode": undefined, - "isSpeechFinal": undefined, + "isSpeechFinal": false, "status": "initial", - "transcript": undefined, + "transcript": "", } } /> diff --git a/stories/voice-search.stories.ts b/stories/voice-search.stories.ts index a8dce262e6..77b77fce95 100644 --- a/stories/voice-search.stories.ts +++ b/stories/voice-search.stories.ts @@ -171,7 +171,7 @@ storiesOf('VoiceSearch', module) status({ isListening, transcript }) { return `
- ${transcript ? transcript : ''} + ${transcript}
`; },