diff options
author | Noah Loomans <noahloomans@gmail.com> | 2018-02-04 11:36:44 +0100 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2018-02-04 11:36:44 +0100 |
commit | c325916e381d6ac13fcc840b2d4baa87913a3184 (patch) | |
tree | 398b7c274f6d41db28d331d5925d12acf8701ca6 /src/client/react/reducers | |
parent | 77f5b8fbe87ce992c46ee5513eaa25c7a7f129ec (diff) |
Reimplement isExactMatch to compair against the url user.
Diffstat (limited to 'src/client/react/reducers')
-rw-r--r-- | src/client/react/reducers/search.js | 28 | ||||
-rw-r--r-- | src/client/react/reducers/search.test.js | 50 |
2 files changed, 7 insertions, 71 deletions
diff --git a/src/client/react/reducers/search.js b/src/client/react/reducers/search.js index 770cdcb..6293b8a 100644 --- a/src/client/react/reducers/search.js +++ b/src/client/react/reducers/search.js @@ -8,7 +8,6 @@ const DEFAULT_STATE = { results: [], searchText: '', selectedResult: null, - isExactMatch: false, }; function getSearchResults(allUsers, query) { @@ -40,36 +39,19 @@ const search = (state = DEFAULT_STATE, action) => { results: [], searchText: users.byId[user].value, selectedResult: user, - isExactMatch: true, }; } - case 'SEARCH/INPUT_CHANGE': { - const { searchText } = action; - const results = getSearchResults(users.allUsers, action.searchText); - let selectedResult = null; - let isExactMatch = false; - - // Is the typed value exactly the same as the first result? Then show the - // appropriate icon instead of the generic search icon. - if ((results.length === 1) && (action.searchText === users.byId[results[0]].value)) { - [selectedResult] = results; - isExactMatch = true; - } - + case 'SEARCH/INPUT_CHANGE': return { ...state, - results, - searchText, - selectedResult, - isExactMatch, + results: getSearchResults(users.allUsers, action.searchText), + searchText: action.searchText, + selectedResult: null, }; - } case 'SEARCH/CHANGE_SELECTED_RESULT': { - const { results, isExactMatch } = state; - - if (isExactMatch) return state; + const { results } = state; const prevSelectedResult = state.selectedResult; const prevSelectedResultIndex = results.indexOf(prevSelectedResult); diff --git a/src/client/react/reducers/search.test.js b/src/client/react/reducers/search.test.js index 22d32e2..423a5aa 100644 --- a/src/client/react/reducers/search.test.js +++ b/src/client/react/reducers/search.test.js @@ -31,7 +31,6 @@ describe('reducers', () => { results: [], searchText: '18561', selectedResult: 's/18561', - isExactMatch: true, }); }); }); @@ -42,7 +41,6 @@ describe('reducers', () => { results: [], searchText: '', selectedResult: null, - isExactMatch: false, }); }); @@ -51,7 +49,6 @@ describe('reducers', () => { results: [], searchText: ' ', selectedResult: null, - isExactMatch: false, }); }); @@ -65,18 +62,6 @@ describe('reducers', () => { ], searchText: '18', selectedResult: null, - isExactMatch: false, - }); - }); - - it('Selects the first result and sets isExactMatch to true when there is an exact match', () => { - expect(search(undefined, inputChange('18561'))).toEqual({ - results: [ - 's/18561', - ], - searchText: '18561', - selectedResult: 's/18561', - isExactMatch: true, }); }); }); @@ -87,7 +72,6 @@ describe('reducers', () => { results: [], searchText: '', selectedResult: null, - isExactMatch: false, }; const actionPlus = changeSelectedResult(+1); @@ -101,32 +85,11 @@ describe('reducers', () => { expect(nextStateMin).toEqual(prevState); }); - it('Does nothing when there is an exact match', () => { - const prevState = { - results: ['s/18561'], - searchText: '18561', - selectedResult: 's/18561', - isExactMatch: true, - }; - - const actionPlus = changeSelectedResult(+1); - const actionMin = changeSelectedResult(-1); - - deepFreeze([prevState, actionPlus, actionMin]); - - const nextStatePlus = search(prevState, actionPlus); - const nextStateMin = search(prevState, actionMin); - - expect(nextStatePlus).toEqual(prevState); - expect(nextStateMin).toEqual(prevState); - }); - it('Switches to the correct selectedResult when no selected result is selected', () => { const prevState = { results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: null, - isExactMatch: false, }; const actionPlus = changeSelectedResult(+1); @@ -152,7 +115,6 @@ describe('reducers', () => { results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: 's/18562', - isExactMatch: false, }; const actionPlus = changeSelectedResult(+1); @@ -173,55 +135,47 @@ describe('reducers', () => { }); }); - it('Properly wraps arround when incrementing', () => { + it('Properly wraps around when incrementing', () => { expect(search({ results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: 's/18563', - isExactMatch: false, }, changeSelectedResult(+1))).toEqual({ results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: null, - isExactMatch: false, }); expect(search({ results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: null, - isExactMatch: false, }, changeSelectedResult(+1))).toEqual({ results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: 's/18561', - isExactMatch: false, }); }); - it('Properly wraps arround when decrementing', () => { + it('Properly wraps around when decrementing', () => { expect(search({ results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: 's/18561', - isExactMatch: false, }, changeSelectedResult(-1))).toEqual({ results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: null, - isExactMatch: false, }); expect(search({ results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: null, - isExactMatch: false, }, changeSelectedResult(-1))).toEqual({ results: ['s/18561', 's/18562', 's/18563'], searchText: '1856', selectedResult: 's/18563', - isExactMatch: false, }); }); }); |