Skip to content

Commit

Permalink
fix(types): improve types for voiceSearch (#3778)
Browse files Browse the repository at this point in the history
* fix(types): make isSpeechFinal non-optional boolean

* fix(types): make transcript non-optional string

* chore(voiceSearch): assign an empty string if no transcript
  • Loading branch information
Eunjae Lee authored May 15, 2019
1 parent 36e3a3d commit ed2d61a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/components/VoiceSearch/__tests__/VoiceSearch-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const defaultProps: VoiceSearchProps = {
toggleListening: () => {},
voiceListeningState: {
status: 'initial',
isSpeechFinal: false,
transcript: '',
},
templates: {
buttonText: 'button',
Expand Down
4 changes: 2 additions & 2 deletions src/lib/voiceSearchHelper/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ describe('VoiceSearchHelper', () => {
const voiceSearchHelper = getVoiceSearchHelper();
expect(voiceSearchHelper.getState()).toEqual({
errorCode: undefined,
isSpeechFinal: undefined,
isSpeechFinal: false,
status: 'initial',
transcript: undefined,
transcript: '',
});
});

Expand Down
15 changes: 8 additions & 7 deletions src/lib/voiceSearchHelper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export type VoiceSearchHelperParams = {

export type VoiceListeningState = {
status: string;
transcript?: string;
isSpeechFinal?: boolean;
transcript: string;
isSpeechFinal: boolean;
errorCode?: string;
};

Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ exports[`voiceSearch() Rendering renders during init() 1`] = `
voiceListeningState={
Object {
"errorCode": undefined,
"isSpeechFinal": undefined,
"isSpeechFinal": false,
"status": "initial",
"transcript": undefined,
"transcript": "",
}
}
/>
Expand All @@ -50,9 +50,9 @@ exports[`voiceSearch() Rendering renders during render() 1`] = `
voiceListeningState={
Object {
"errorCode": undefined,
"isSpeechFinal": undefined,
"isSpeechFinal": false,
"status": "initial",
"transcript": undefined,
"transcript": "",
}
}
/>
Expand All @@ -79,9 +79,9 @@ exports[`voiceSearch() Rendering renders during render() 2`] = `
voiceListeningState={
Object {
"errorCode": undefined,
"isSpeechFinal": undefined,
"isSpeechFinal": false,
"status": "initial",
"transcript": undefined,
"transcript": "",
}
}
/>
Expand Down
2 changes: 1 addition & 1 deletion stories/voice-search.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ storiesOf('VoiceSearch', module)
status({ isListening, transcript }) {
return `
<div class="layer listening-${isListening}">
<span>${transcript ? transcript : ''}</span>
<span>${transcript}</span>
</div>
`;
},
Expand Down

0 comments on commit ed2d61a

Please sign in to comment.