|
1 | 1 | import { expect } from 'chai'; |
2 | | -import { handleAction, createAction, createActions } from '../'; |
| 2 | +import { handleAction, createAction, createActions, combineActions } from '../'; |
3 | 3 |
|
4 | 4 | describe('handleAction()', () => { |
5 | 5 | const type = 'TYPE'; |
@@ -108,4 +108,91 @@ describe('handleAction()', () => { |
108 | 108 | }); |
109 | 109 | }); |
110 | 110 | }); |
| 111 | + |
| 112 | + describe('with combined actions', () => { |
| 113 | + it('should handle combined actions in reducer form', () => { |
| 114 | + const action1 = createAction('ACTION_1'); |
| 115 | + const reducer = handleAction( |
| 116 | + combineActions(action1, 'ACTION_2', 'ACTION_3'), |
| 117 | + (state, { payload }) => ({ ...state, number: state.number + payload }) |
| 118 | + ); |
| 119 | + |
| 120 | + expect(reducer({ number: 1 }, action1(1))).to.deep.equal({ number: 2 }); |
| 121 | + expect(reducer({ number: 1 }, { type: 'ACTION_2', payload: 2 })).to.deep.equal({ number: 3 }); |
| 122 | + expect(reducer({ number: 1 }, { type: 'ACTION_3', payload: 3 })).to.deep.equal({ number: 4 }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should handle combined actions in next/throw form', () => { |
| 126 | + const action1 = createAction('ACTION_1'); |
| 127 | + const reducer = handleAction(combineActions(action1, 'ACTION_2', 'ACTION_3'), { |
| 128 | + next(state, { payload }) { |
| 129 | + return { ...state, number: state.number + payload }; |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + expect(reducer({ number: 1 }, action1(1))).to.deep.equal({ number: 2 }); |
| 134 | + expect(reducer({ number: 1 }, { type: 'ACTION_2', payload: 2 })).to.deep.equal({ number: 3 }); |
| 135 | + expect(reducer({ number: 1 }, { type: 'ACTION_3', payload: 3 })).to.deep.equal({ number: 4 }); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should handle combined error actions', () => { |
| 139 | + const action1 = createAction('ACTION_1'); |
| 140 | + const reducer = handleAction(combineActions(action1, 'ACTION_2', 'ACTION_3'), { |
| 141 | + next(state, { payload }) { |
| 142 | + return { ...state, number: state.number + payload }; |
| 143 | + }, |
| 144 | + |
| 145 | + throw(state) { |
| 146 | + return { ...state, threw: true }; |
| 147 | + } |
| 148 | + }); |
| 149 | + const error = new Error; |
| 150 | + |
| 151 | + expect(reducer({ number: 0 }, action1(error))) |
| 152 | + .to.deep.equal({ number: 0, threw: true }); |
| 153 | + expect(reducer({ number: 0 }, { type: 'ACTION_2', payload: error, error: true })) |
| 154 | + .to.deep.equal({ number: 0, threw: true }); |
| 155 | + expect(reducer({ number: 0 }, { type: 'ACTION_3', payload: error, error: true })) |
| 156 | + .to.deep.equal({ number: 0, threw: true }); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should return previous state if action is not one of the combined actions', () => { |
| 160 | + const reducer = handleAction( |
| 161 | + combineActions('ACTION_1', 'ACTION_2'), |
| 162 | + (state, { payload }) => ({ ...state, state: state.number + payload }), |
| 163 | + ); |
| 164 | + |
| 165 | + const state = { number: 0 }; |
| 166 | + |
| 167 | + expect(reducer(state, { type: 'ACTION_3', payload: 1 })).to.equal(state); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should use the default state if the initial state is undefined', () => { |
| 171 | + const reducer = handleAction( |
| 172 | + combineActions('INCREMENT', 'DECREMENT'), |
| 173 | + (state, { payload }) => ({ ...state, counter: state.counter + payload }), |
| 174 | + { counter: 10 } |
| 175 | + ); |
| 176 | + |
| 177 | + expect(reducer(undefined, { type: 'INCREMENT', payload: +1 })).to.deep.equal({ counter: 11 }); |
| 178 | + expect(reducer(undefined, { type: 'DECREMENT', payload: -1 })).to.deep.equal({ counter: 9 }); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should handle combined actions with symbols', () => { |
| 182 | + const action1 = createAction('ACTION_1'); |
| 183 | + const action2 = Symbol('ACTION_2'); |
| 184 | + const action3 = createAction(Symbol('ACTION_3')); |
| 185 | + const reducer = handleAction( |
| 186 | + combineActions(action1, action2, action3), |
| 187 | + (state, { payload }) => ({ ...state, number: state.number + payload }) |
| 188 | + ); |
| 189 | + |
| 190 | + expect(reducer({ number: 0 }, action1(1))) |
| 191 | + .to.deep.equal({ number: 1 }); |
| 192 | + expect(reducer({ number: 0 }, { type: action2, payload: 2 })) |
| 193 | + .to.deep.equal({ number: 2 }); |
| 194 | + expect(reducer({ number: 0 }, { type: Symbol('ACTION_3'), payload: 3 })) |
| 195 | + .to.deep.equal({ number: 3 }); |
| 196 | + }); |
| 197 | + }); |
111 | 198 | }); |
0 commit comments