-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue with suggestions not being shown in certain circumstances (#…
…223)
- Loading branch information
Showing
10 changed files
with
197 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
test/do-not-focus-input-on-suggestion-click/AutosuggestApp.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { Component } from 'react'; | ||
import sinon from 'sinon'; | ||
import Autosuggest from '../../src/AutosuggestContainer'; | ||
import languages from '../plain-list/languages'; | ||
import { escapeRegexCharacters } from '../../demo/src/components/utils/utils.js'; | ||
|
||
function getMatchingLanguages(value) { | ||
const escapedValue = escapeRegexCharacters(value.trim()); | ||
const regex = new RegExp('^' + escapedValue, 'i'); | ||
|
||
return languages.filter(language => regex.test(language.name)); | ||
} | ||
|
||
let app = null; | ||
|
||
export const getSuggestionValue = sinon.spy(suggestion => { | ||
return suggestion.name; | ||
}); | ||
|
||
export const renderSuggestion = sinon.spy(suggestion => { | ||
return ( | ||
<span>{suggestion.name}</span> | ||
); | ||
}); | ||
|
||
export const onChange = sinon.spy((event, { newValue }) => { | ||
app.setState({ | ||
value: newValue | ||
}); | ||
}); | ||
|
||
export const onBlur = sinon.spy(); | ||
|
||
export const onSuggestionsUpdateRequested = sinon.spy(({ value }) => { | ||
app.setState({ | ||
suggestions: getMatchingLanguages(value) | ||
}); | ||
}); | ||
|
||
export const onSuggestionSelected = sinon.spy(); | ||
|
||
export default class AutosuggestApp extends Component { | ||
constructor() { | ||
super(); | ||
|
||
app = this; | ||
|
||
this.state = { | ||
value: '', | ||
suggestions: getMatchingLanguages('') | ||
}; | ||
} | ||
|
||
render() { | ||
const { value, suggestions } = this.state; | ||
const inputProps = { | ||
value, | ||
onChange, | ||
onBlur | ||
}; | ||
|
||
return ( | ||
<Autosuggest | ||
suggestions={suggestions} | ||
onSuggestionsUpdateRequested={onSuggestionsUpdateRequested} | ||
getSuggestionValue={getSuggestionValue} | ||
renderSuggestion={renderSuggestion} | ||
inputProps={inputProps} | ||
onSuggestionSelected={onSuggestionSelected} | ||
focusInputOnSuggestionClick={false} /> | ||
); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
test/do-not-focus-input-on-suggestion-click/AutosuggestApp.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from 'react'; | ||
import TestUtils from 'react-addons-test-utils'; | ||
import { expect } from 'chai'; | ||
import { | ||
init, | ||
syntheticEventMatcher, | ||
clickSuggestion, | ||
focusAndSetInputValue, | ||
isInputFocused | ||
} from '../helpers'; | ||
import AutosuggestApp, { | ||
onBlur, | ||
onSuggestionsUpdateRequested | ||
} from './AutosuggestApp'; | ||
|
||
describe('Autosuggest with focusInputOnSuggestionClick={false}', () => { | ||
beforeEach(() => { | ||
init(TestUtils.renderIntoDocument(<AutosuggestApp />)); | ||
}); | ||
|
||
describe('when suggestion is clicked', () => { | ||
beforeEach(() => { | ||
onBlur.reset(); | ||
focusAndSetInputValue('p'); | ||
onSuggestionsUpdateRequested.reset(); | ||
clickSuggestion(1); | ||
}); | ||
|
||
it('should not focus on input', () => { | ||
expect(isInputFocused()).to.equal(false); | ||
}); | ||
|
||
it('should call onBlur once with the right parameters', () => { | ||
expect(onBlur).to.have.been.calledOnce; | ||
expect(onBlur).to.have.been.calledWithExactly(syntheticEventMatcher, { | ||
focusedSuggestion: { name: 'PHP', year: 1995 } | ||
}); | ||
}); | ||
|
||
it('should call onSuggestionsUpdateRequested once with the right parameters', () => { | ||
expect(onSuggestionsUpdateRequested).to.have.been.calledOnce; | ||
expect(onSuggestionsUpdateRequested).to.have.been.calledWithExactly({ value: 'PHP', reason: 'click' }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.