From 40cc5b9175f08f66c9c6a9d6efad59bf47cea307 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 1 Oct 2017 08:32:55 +0900 Subject: [PATCH] add tests for completion and fix --- src/reducers/completion.js | 11 +++- test/actions/completion.test.js | 27 ++++++++++ test/reducers/completion.test.js | 90 ++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 test/actions/completion.test.js create mode 100644 test/reducers/completion.test.js diff --git a/src/reducers/completion.js b/src/reducers/completion.js index a706988..a8a6444 100644 --- a/src/reducers/completion.js +++ b/src/reducers/completion.js @@ -24,7 +24,10 @@ const nextSelection = (state) => { const prevSelection = (state) => { if (state.groupSelection < 0) { - return [0, 0]; + return [ + state.groups.length - 1, + state.groups[state.groups.length - 1].items.length - 1 + ]; } if (state.groupSelection === 0 && state.itemSelection === 0) { return [-1, -1]; @@ -41,7 +44,9 @@ export default function reducer(state = defaultState, action = {}) { switch (action.type) { case actions.COMPLETION_SET_ITEMS: return Object.assign({}, state, { - groups: action.groups + groups: action.groups, + groupSelection: -1, + itemSelection: -1, }); case actions.COMPLETION_SELECT_NEXT: { let next = nextSelection(state); @@ -57,5 +62,7 @@ export default function reducer(state = defaultState, action = {}) { itemSelection: next[1], }); } + default: + return defaultState; } } diff --git a/test/actions/completion.test.js b/test/actions/completion.test.js new file mode 100644 index 0000000..da88f53 --- /dev/null +++ b/test/actions/completion.test.js @@ -0,0 +1,27 @@ +import { expect } from "chai"; +import actions from '../../src/actions'; +import * as completionActions from '../../src/actions/completion'; + +describe("completion actions", () => { + describe('setItems', () => { + it('create COMPLETION_SET_ITEMS action', () => { + let action = completionActions.setItems([1, 2, 3]); + expect(action.type).to.equal(actions.COMPLETION_SET_ITEMS); + expect(action.groups).to.deep.equal([1, 2, 3]); + }); + }); + + describe('selectNext', () => { + it('create COMPLETION_SELECT_NEXT action', () => { + let action = completionActions.selectNext(); + expect(action.type).to.equal(actions.COMPLETION_SELECT_NEXT); + }); + }); + + describe('selectPrev', () => { + it('create COMPLETION_SELECT_PREV action', () => { + let action = completionActions.selectPrev(); + expect(action.type).to.equal(actions.COMPLETION_SELECT_PREV); + }); + }); +}); diff --git a/test/reducers/completion.test.js b/test/reducers/completion.test.js new file mode 100644 index 0000000..79163bf --- /dev/null +++ b/test/reducers/completion.test.js @@ -0,0 +1,90 @@ +import { expect } from "chai"; +import actions from '../../src/actions'; +import completionReducer from '../../src/reducers/completion'; + +describe("completion reducer", () => { + it ('return the initial state', () => { + let state = completionReducer(undefined, {}); + expect(state).to.have.property('groupSelection', -1); + expect(state).to.have.property('itemSelection', -1); + expect(state).to.have.deep.property('groups', []); + }); + + it ('return next state for COMPLETION_SET_ITEMS', () => { + let state = { + groupSelection: 0, + itemSelection: 0, + groups: [], + } + let action = { + type: actions.COMPLETION_SET_ITEMS, + groups: [{ + name: 'Apple', + items: [1, 2, 3] + }, { + name: 'Banana', + items: [4, 5, 6] + }] + } + state = completionReducer(state, action); + expect(state).to.have.property('groups', action.groups); + expect(state).to.have.property('groupSelection', -1); + expect(state).to.have.property('itemSelection', -1); + }); + + it ('return next state for COMPLETION_SELECT_NEXT', () => { + let action = { type: actions.COMPLETION_SELECT_NEXT }; + let state = { + groupSelection: -1, + itemSelection: -1, + groups: [{ + name: 'Apple', + items: [1, 2] + }, { + name: 'Banana', + items: [3] + }] + }; + + state = completionReducer(state, action); + expect(state).to.have.property('groupSelection', 0); + expect(state).to.have.property('itemSelection', 0); + + state = completionReducer(state, action); + expect(state).to.have.property('groupSelection', 0); + expect(state).to.have.property('itemSelection', 1); + + state = completionReducer(state, action); + state = completionReducer(state, action); + expect(state).to.have.property('groupSelection', -1); + expect(state).to.have.property('itemSelection', -1); + }); + + it ('return next state for COMPLETION_SELECT_PREV', () => { + let action = { type: actions.COMPLETION_SELECT_PREV }; + let state = { + groupSelection: -1, + itemSelection: -1, + groups: [{ + name: 'Apple', + items: [1, 2] + }, { + name: 'Banana', + items: [3] + }] + }; + + state = completionReducer(state, action); + expect(state).to.have.property('groupSelection', 1); + expect(state).to.have.property('itemSelection', 0); + + state = completionReducer(state, action); + expect(state).to.have.property('groupSelection', 0); + expect(state).to.have.property('itemSelection', 1); + + state = completionReducer(state, action); + state = completionReducer(state, action); + expect(state).to.have.property('groupSelection', -1); + expect(state).to.have.property('itemSelection', -1); + }); +});