forked from moroshko/react-autosuggest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for shouldKeepSuggestionsOnSelect
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React, { Component } from 'react'; | ||
import Autosuggest from '../../src/Autosuggest'; | ||
import { escapeRegexCharacters } from '../../demo/src/components/utils/utils'; | ||
import languages from '../plain-list/languages'; | ||
import sinon from 'sinon'; | ||
import { addEvent } from '../helpers'; | ||
|
||
const 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 = (suggestion) => suggestion.name; | ||
|
||
export const renderSuggestion = (suggestion) => <span>{suggestion.name}</span>; | ||
|
||
export const onChange = sinon.spy((event, { newValue }) => { | ||
addEvent('onChange'); | ||
|
||
app.setState({ | ||
value: newValue, | ||
}); | ||
}); | ||
|
||
export const onSuggestionsFetchRequested = ({ value }) => { | ||
app.setState({ | ||
suggestions: getMatchingLanguages(value), | ||
}); | ||
}; | ||
|
||
export const onSuggestionsClearRequested = () => { | ||
app.setState({ | ||
suggestions: [], | ||
}); | ||
}; | ||
|
||
export const shouldKeepSuggestionsOnSelect = (suggestion) => { | ||
return suggestion.name.toLowerCase().startsWith('clo'); | ||
}; | ||
|
||
export default class AutosuggestApp extends Component { | ||
constructor() { | ||
super(); | ||
|
||
app = this; | ||
|
||
this.state = { | ||
value: '', | ||
suggestions: [], | ||
}; | ||
} | ||
|
||
render() { | ||
const { value, suggestions } = this.state; | ||
const inputProps = { | ||
value, | ||
onChange, | ||
}; | ||
|
||
return ( | ||
<Autosuggest | ||
suggestions={suggestions.slice()} | ||
onSuggestionsFetchRequested={onSuggestionsFetchRequested} | ||
onSuggestionsClearRequested={onSuggestionsClearRequested} | ||
getSuggestionValue={getSuggestionValue} | ||
renderSuggestion={renderSuggestion} | ||
inputProps={inputProps} | ||
shouldKeepSuggestionsOnSelect={shouldKeepSuggestionsOnSelect} | ||
/> | ||
); | ||
} | ||
} |
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,34 @@ | ||
import { | ||
clickSuggestion, | ||
focusAndSetInputValue, | ||
getSuggestions, | ||
init, | ||
} from '../helpers'; | ||
import TestUtils from 'react-dom/test-utils'; | ||
import AutosuggestApp from './AutosuggestApp'; | ||
import { expect } from 'chai'; | ||
import React from 'react'; | ||
|
||
describe('Autosuggest with custom shouldKeepSuggestionsOnSelect', () => { | ||
beforeEach(() => { | ||
init(TestUtils.renderIntoDocument(<AutosuggestApp />)); | ||
}); | ||
|
||
describe('when keep opened for starts with `clo` suggestions', () => { | ||
it('should keep suggestions on select', () => { | ||
focusAndSetInputValue('clo'); | ||
clickSuggestion(0); | ||
const suggestions = getSuggestions(); | ||
|
||
expect(suggestions.length).to.equal(1); | ||
}); | ||
|
||
it('should not suggestions on select', () => { | ||
focusAndSetInputValue('php'); | ||
clickSuggestion(0); | ||
const suggestions = getSuggestions(); | ||
|
||
expect(suggestions.length).to.equal(0); | ||
}); | ||
}); | ||
}); |