From 7b51f12813ab145304c15f0d39222a2811e6ca4d Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 26 Jul 2016 02:09:54 +0800 Subject: [web] bug fix and add test --- web/src/js/__tests__/ducks/tutils.js | 6 ++++++ web/src/js/__tests__/ducks/ui.js | 36 -------------------------------- web/src/js/__tests__/ducks/ui/header.js | 36 ++++++++++++++++++++++++++++++++ web/src/js/__tests__/ducks/utils/view.js | 6 ++++-- web/src/js/ducks/utils/view.js | 28 +++++++++++++++++++------ 5 files changed, 68 insertions(+), 44 deletions(-) delete mode 100644 web/src/js/__tests__/ducks/ui.js create mode 100644 web/src/js/__tests__/ducks/ui/header.js (limited to 'web') diff --git a/web/src/js/__tests__/ducks/tutils.js b/web/src/js/__tests__/ducks/tutils.js index 90a21b78..6a543434 100644 --- a/web/src/js/__tests__/ducks/tutils.js +++ b/web/src/js/__tests__/ducks/tutils.js @@ -10,3 +10,9 @@ export function createStore(parts) { applyMiddleware(...[thunk]) ) } + +describe('tutils', () => { + it('do nothing', () => { + return + }) +}) \ No newline at end of file diff --git a/web/src/js/__tests__/ducks/ui.js b/web/src/js/__tests__/ducks/ui.js deleted file mode 100644 index d3242815..00000000 --- a/web/src/js/__tests__/ducks/ui.js +++ /dev/null @@ -1,36 +0,0 @@ -jest.unmock('../../ducks/ui') -jest.unmock('../../ducks/flows') - -import reducer, { setActiveMenu } from '../../ducks/ui' -import * as flowActions from '../../ducks/flows' - -describe('ui reducer', () => { - it('should return the initial state', () => { - expect(reducer(undefined, {}).activeMenu).toEqual('Start') - }) - - it('should return the state for view', () => { - expect(reducer(undefined, setActiveMenu('View')).activeMenu).toEqual('View') - }) - - it('should change the state to Start when deselecting a flow and we a currently at the flow tab', () => { - expect(reducer( - { activeMenu: 'Flow', isFlowSelected: true }, - flowActions.select(undefined)).activeMenu - ).toEqual('Start') - }) - - it('should change the state to Flow when we selected a flow and no flow was selected before', () => { - expect(reducer( - { activeMenu: 'Start', isFlowSelected: false }, - flowActions.select(1)).activeMenu - ).toEqual('Flow') - }) - - it('should not change the state to Flow when OPTIONS tab is selected and we selected a flow and a flow as selected before', () => { - expect(reducer( - { activeMenu: 'Options', isFlowSelected: true }, - flowActions.select(1) - ).activeMenu).toEqual('Options') - }) -}) diff --git a/web/src/js/__tests__/ducks/ui/header.js b/web/src/js/__tests__/ducks/ui/header.js new file mode 100644 index 00000000..8968e636 --- /dev/null +++ b/web/src/js/__tests__/ducks/ui/header.js @@ -0,0 +1,36 @@ +jest.unmock('../../../ducks/ui/header') +jest.unmock('../../../ducks/flows') + +import reducer, { setActiveMenu } from '../../../ducks/ui/header' +import * as flowActions from '../../../ducks/flows' + +describe('header reducer', () => { + it('should return the initial state', () => { + expect(reducer(undefined, {}).activeMenu).toEqual('Start') + }) + + it('should return the state for view', () => { + expect(reducer(undefined, setActiveMenu('View')).activeMenu).toEqual('View') + }) + + it('should change the state to Start when deselecting a flow and we a currently at the flow tab', () => { + expect(reducer( + { activeMenu: 'Flow', isFlowSelected: true }, + flowActions.select(undefined)).activeMenu + ).toEqual('Start') + }) + + it('should change the state to Flow when we selected a flow and no flow was selected before', () => { + expect(reducer( + { activeMenu: 'Start', isFlowSelected: false }, + flowActions.select(1)).activeMenu + ).toEqual('Flow') + }) + + it('should not change the state to Flow when OPTIONS tab is selected and we selected a flow and a flow as selected before', () => { + expect(reducer( + { activeMenu: 'Options', isFlowSelected: true }, + flowActions.select(1) + ).activeMenu).toEqual('Options') + }) +}) diff --git a/web/src/js/__tests__/ducks/utils/view.js b/web/src/js/__tests__/ducks/utils/view.js index f0b147da..af3da173 100644 --- a/web/src/js/__tests__/ducks/utils/view.js +++ b/web/src/js/__tests__/ducks/utils/view.js @@ -66,11 +66,13 @@ describe('view reduce', () => { it('should update item', () => { const state = createState([ { id: 1, val: 1 }, - { id: 2, val: 2 } + { id: 2, val: 2 }, + { id: 3, val: 3 } ]) const result = createState([ { id: 1, val: 1 }, - { id: 2, val: 3 } + { id: 2, val: 3 }, + { id: 3, val: 3 } ]) expect(reduce(state, view.update({ id: 2, val: 3 }))).toEqual(result) }) diff --git a/web/src/js/ducks/utils/view.js b/web/src/js/ducks/utils/view.js index 2f1e03fa..fdddc391 100755 --- a/web/src/js/ducks/utils/view.js +++ b/web/src/js/ducks/utils/view.js @@ -54,14 +54,30 @@ export default function reduce(state = defaultState, action) { } case UPDATE: - if (state.indexOf[action.item.id] == null) { - return + let hasOldItem = state.indexOf[action.item.id] !== null && state.indexOf[action.item.id] !== undefined + let hasNewItem = action.filter(action.item) + if (!hasNewItem && !hasOldItem) { + return state } - return { - ...state, - ...sortedUpdate(state, action.item, action.sort), + if (hasNewItem && !hasOldItem) { + return { + ...state, + ...sortedInsert(state, action.item, action.sort) + } } - + if (!hasNewItem && hasOldItem) { + return { + ...state, + ...sortedRemove(state, action.item.id) + } + } + if (hasNewItem && hasOldItem) { + return { + ...state, + ...sortedUpdate(state, action.item, action.sort), + } + } + break; case RECEIVE: { const data = action.list.filter(action.filter).sort(action.sort) -- cgit v1.2.3