Skip to content

Commit

Permalink
Call onSuggestionsFetchRequested onFocus only if shouldRenderSuggesti…
Browse files Browse the repository at this point in the history
…ons returns true
  • Loading branch information
moroshko committed Aug 28, 2016
1 parent 7608db8 commit 8cee3a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/Autosuggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Autosuggest extends Component {
componentWillReceiveProps(nextProps) {
if (shallowEqualArrays(nextProps.suggestions, this.props.suggestions)) {
if (nextProps.focusFirstSuggestion &&
nextProps.suggestions.length > 0 &&
nextProps.focusedSuggestionIndex === null &&
nextProps.inputProps.value !== this.props.inputProps.value &&
nextProps.valueBeforeUpDown === this.props.valueBeforeUpDown) {
Expand Down Expand Up @@ -286,7 +287,10 @@ class Autosuggest extends Component {
if (!this.justSelectedSuggestion && !this.justClickedOnSuggestionsContainer) {
inputFocused(shouldRenderSuggestions(value));
onFocus && onFocus(event);
onSuggestionsFetchRequested({ value });

if (shouldRenderSuggestions(value)) {
onSuggestionsFetchRequested({ value });
}
}
},
onBlur: event => {
Expand All @@ -304,7 +308,6 @@ class Autosuggest extends Component {
},
onChange: event => {
const { value } = event.target;
const { shouldRenderSuggestions } = this.props;
const shouldRender = shouldRenderSuggestions(value);

this.maybeCallOnChange(event, value, 'type');
Expand Down
7 changes: 7 additions & 0 deletions test/multi-section/AutosuggestApp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ describe('Autosuggest with multiSection={true}', () => {
});

describe('onSuggestionsFetchRequested', () => {
it('should be called once with the right parameters when input gets focus and shouldRenderSuggestions returns true', () => {
onSuggestionsFetchRequested.reset();
focusInput();
expect(onSuggestionsFetchRequested).to.have.been.calledOnce;
expect(onSuggestionsFetchRequested).to.have.been.calledWithExactly({ value: '' });
});

it('should be called once with the right parameters when Escape is pressed and suggestions are hidden and shouldRenderSuggestions returns true for empty value', () => {
focusAndSetInputValue('jr');
onSuggestionsFetchRequested.reset();
Expand Down
27 changes: 13 additions & 14 deletions test/plain-list/AutosuggestApp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,6 @@ describe('Default Autosuggest', () => {
});

describe('onSuggestionsFetchRequested', () => {
it('should be called once with the right parameters when input gets focus', () => {
onSuggestionsFetchRequested.reset();
focusInput();
expect(onSuggestionsFetchRequested).to.have.been.calledOnce;
expect(onSuggestionsFetchRequested).to.have.been.calledWithExactly({ value: '' });
});

it('should be called once with the right parameters when user types', () => {
focusInput();
onSuggestionsFetchRequested.reset();
Expand All @@ -603,6 +596,19 @@ describe('Default Autosuggest', () => {
expect(onSuggestionsFetchRequested).to.have.been.calledWithExactly({ value: 'Javascript' });
});

it('should not be called when input gets focus and shouldRenderSuggestions returns false', () => {
onSuggestionsFetchRequested.reset();
focusInput();
expect(onSuggestionsFetchRequested).not.to.have.been.called;
});

it('should not be called when user types and shouldRenderSuggestions returns false', () => {
focusInput();
onSuggestionsFetchRequested.reset();
setInputValue(' ');
expect(onSuggestionsFetchRequested).not.to.have.been.called;
});

it('should not be called when Down is pressed to highlight the next suggestion', () => {
focusAndSetInputValue('j');
onSuggestionsFetchRequested.reset();
Expand Down Expand Up @@ -670,13 +676,6 @@ describe('Default Autosuggest', () => {
clickEscape();
expect(onSuggestionsFetchRequested).not.to.have.been.called;
});

it('should not be called when shouldRenderSuggestions returns false', () => {
focusInput();
onSuggestionsFetchRequested.reset();
setInputValue(' ');
expect(onSuggestionsFetchRequested).not.to.have.been.called;
});
});

describe('onSuggestionsClearRequested', () => {
Expand Down

0 comments on commit 8cee3a1

Please sign in to comment.