aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client/react/store/reducers.js12
-rw-r--r--src/client/react/store/reducers.test.js32
2 files changed, 5 insertions, 39 deletions
diff --git a/src/client/react/store/reducers.js b/src/client/react/store/reducers.js
index c5c564c..34c6d99 100644
--- a/src/client/react/store/reducers.js
+++ b/src/client/react/store/reducers.js
@@ -20,6 +20,7 @@
import getSearchResults from '../lib/getSearchResults';
import users from '../users';
+import withinRange from '../lib/withinRange';
const DEFAULT_STATE = {
search: {
@@ -91,13 +92,10 @@ function reducer(state = DEFAULT_STATE, action) {
const prevSelectedResult = state.search.selected;
const prevSelectedResultIndex = state.search.results.indexOf(prevSelectedResult);
- let nextSelectedResultIndex = prevSelectedResultIndex + action.relativeChange;
-
- if (nextSelectedResultIndex < 0) {
- nextSelectedResultIndex = state.search.results.length - 1;
- } else if (nextSelectedResultIndex > state.search.results.length - 1) {
- nextSelectedResultIndex = 0;
- }
+ const nextSelectedResultIndex = withinRange(
+ prevSelectedResultIndex + action.relativeChange,
+ state.search.results.length - 1,
+ );
const nextSelectedResult = state.search.results[nextSelectedResultIndex];
diff --git a/src/client/react/store/reducers.test.js b/src/client/react/store/reducers.test.js
index ae4d664..3f6aff1 100644
--- a/src/client/react/store/reducers.test.js
+++ b/src/client/react/store/reducers.test.js
@@ -173,22 +173,6 @@ describe('reducers', () => {
selected: 's/18561',
},
});
-
- expect(reducer({
- ...DEFAULT_STATE,
- search: {
- results: ['s/18561', 's/18562', 's/18563'],
- text: '1856',
- selected: 's/18563',
- },
- }, { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: +1 })).toEqual({
- ...DEFAULT_STATE,
- search: {
- results: ['s/18561', 's/18562', 's/18563'],
- text: '1856',
- selected: 's/18561',
- },
- });
});
it('Properly wraps around when decrementing', () => {
@@ -207,22 +191,6 @@ describe('reducers', () => {
selected: 's/18563',
},
});
-
- expect(reducer({
- ...DEFAULT_STATE,
- search: {
- results: ['s/18561', 's/18562', 's/18563'],
- text: '1856',
- selected: null,
- },
- }, { type: 'SEARCH/CHANGE_SELECTED_RESULT', relativeChange: -1 })).toEqual({
- ...DEFAULT_STATE,
- search: {
- results: ['s/18561', 's/18562', 's/18563'],
- text: '1856',
- selected: 's/18563',
- },
- });
});
});
});