From 71f43b4551c193bd9a66fcc17425eb5c7f91a801 Mon Sep 17 00:00:00 2001 From: Misha Moroshko Date: Sat, 2 May 2015 22:00:53 +1000 Subject: [PATCH] Cache case insensitive results --- dist/Autosuggest.js | 8 +++++--- package.json | 2 +- src/Autosuggest.js | 10 ++++++---- src/tests/Autosuggest.js | 27 +++++++++++++++++++-------- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/dist/Autosuggest.js b/dist/Autosuggest.js index c0fb54b7..0e7fe81c 100644 --- a/dist/Autosuggest.js +++ b/dist/Autosuggest.js @@ -111,12 +111,14 @@ var Autosuggest = (function (_Component) { value: function showSuggestions(input) { var _this2 = this; + var cacheKey = input.toLowerCase(); + this.lastSuggestionsInputValue = input; if (!this.props.showWhen(input)) { this.setSuggestionsState(null); - } else if (this.cache[input]) { - this.setSuggestionsState(this.cache[input]); + } else if (this.cache[cacheKey]) { + this.setSuggestionsState(this.cache[cacheKey]); } else { this.suggestionsFn(input, function (error, suggestions) { // If input value changed, suggestions are not relevant anymore. @@ -131,7 +133,7 @@ var Autosuggest = (function (_Component) { suggestions = null; } - _this2.cache[input] = suggestions; + _this2.cache[cacheKey] = suggestions; _this2.setSuggestionsState(suggestions); } }); diff --git a/package.json b/package.json index f966c10d..65a0d010 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-autosuggest", - "version": "1.12.0", + "version": "1.12.1", "description": "WAI-ARIA compliant React autosuggest component", "main": "dist/Autosuggest.js", "repository": { diff --git a/src/Autosuggest.js b/src/Autosuggest.js index 8b8cc6de..97c71fa2 100644 --- a/src/Autosuggest.js +++ b/src/Autosuggest.js @@ -83,12 +83,14 @@ export default class Autosuggest extends Component { } showSuggestions(input) { + const cacheKey = input.toLowerCase(); + this.lastSuggestionsInputValue = input; if (!this.props.showWhen(input)) { this.setSuggestionsState(null); - } else if (this.cache[input]) { - this.setSuggestionsState(this.cache[input]); + } else if (this.cache[cacheKey]) { + this.setSuggestionsState(this.cache[cacheKey]); } else { this.suggestionsFn(input, (error, suggestions) => { // If input value changed, suggestions are not relevant anymore. @@ -103,7 +105,7 @@ export default class Autosuggest extends Component { suggestions = null; } - this.cache[input] = suggestions; + this.cache[cacheKey] = suggestions; this.setSuggestionsState(suggestions); } }); @@ -316,7 +318,7 @@ export default class Autosuggest extends Component { valueBeforeUpDown: null }, () => { // This code executes after the component is re-rendered - setTimeout( () => findDOMNode(this.refs.input).focus() ); + setTimeout(() => findDOMNode(this.refs.input).focus()); }); } diff --git a/src/tests/Autosuggest.js b/src/tests/Autosuggest.js index 3b6d1d74..c93394f9 100644 --- a/src/tests/Autosuggest.js +++ b/src/tests/Autosuggest.js @@ -22,6 +22,7 @@ const onSuggestionSelected = jest.genMockFunction(); const onSuggestionFocused = jest.genMockFunction(); const onSuggestionUnfocused = jest.genMockFunction(); const onChange = jest.genMockFunction(); +const getSuburbs = jest.genMockFunction().mockImplementation(getSuburbStrings); function getSuburbStrings(input, callback) { const regex = new RegExp('^' + input, 'i'); @@ -970,17 +971,27 @@ describe('Autosuggest', function() { }); }); - describe('Misc', function() { + describe('Caching', function() { beforeEach(function() { - createAutosuggest(); + createAutosuggest(); + setInputValue('m'); + getSuburbs.mockClear(); }); - it('should reset sectionIterator when getting cached suggestions', function() { - setInputValue('m'); - setInputValue('mz'); - setInputValue('m'); - clickDown(); - expectFocusedSuggestion('Mill Park'); + describe('should not call suggestions function if', function() { + it('suggestions function was called before with the same input', function() { + setInputValue('mi'); + getSuburbs.mockClear(); + setInputValue('m'); + expect(getSuburbs).not.toBeCalled(); + }); + + it('suggestions function was called before with the same case insensitive input', function() { + setInputValue('mi'); + getSuburbs.mockClear(); + setInputValue('M'); + expect(getSuburbs).not.toBeCalled(); + }); }); }); });