|
| 1 | +import {switchReduce} from '../src/utils'; |
| 2 | +import {TypedAction} from '../src/dispatcher'; |
| 3 | +interface TestState { |
| 4 | + num: number; |
| 5 | +} |
| 6 | + |
| 7 | +class AddNumberAction implements TypedAction<number> { |
| 8 | + type: 'ADD_NUMBER'; |
| 9 | + constructor(public payload: number) {} |
| 10 | +} |
| 11 | + |
| 12 | +class SubtractNumberAction implements TypedAction<number> { |
| 13 | + type: 'SUBTRACT_NUMBER'; |
| 14 | + constructor(public payload: number) {} |
| 15 | +} |
| 16 | + |
| 17 | +let testState: TestState; |
| 18 | +describe('switchReduce', () => { |
| 19 | + beforeEach(() => { |
| 20 | + testState = { |
| 21 | + num: 1 |
| 22 | + }; |
| 23 | + }); |
| 24 | + |
| 25 | + it('should return initial state with no cases and no default', () => { |
| 26 | + const runSpy = jasmine.createSpy('spy'); |
| 27 | + const payload = 1; |
| 28 | + |
| 29 | + switchReduce(testState, new AddNumberAction(payload)).reduce(); |
| 30 | + |
| 31 | + expect(runSpy).not.toHaveBeenCalled(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should take default if nothing else specified', () => { |
| 35 | + const newState = switchReduce(testState, new AddNumberAction(1)) |
| 36 | + .reduce(() => ({ |
| 37 | + num: 5 |
| 38 | + })); |
| 39 | + |
| 40 | + expect(newState.num).toBe(5); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should take default if nothing else matches', () => { |
| 44 | + const runSpy = jasmine.createSpy('spy'); |
| 45 | + |
| 46 | + const newState = switchReduce(testState, new AddNumberAction(1)) |
| 47 | + .byClass(SubtractNumberAction, runSpy) |
| 48 | + .byType('NOT_EXISTING', runSpy) |
| 49 | + .reduce(() => ({ |
| 50 | + num: 5 |
| 51 | + })); |
| 52 | + |
| 53 | + expect(newState.num).toBe(5); |
| 54 | + expect(runSpy).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should execute run function only once', () => { |
| 58 | + const runSpy = jasmine.createSpy('spy'); |
| 59 | + const payload = 1; |
| 60 | + |
| 61 | + switchReduce(testState, new AddNumberAction(payload)) |
| 62 | + .byClass(AddNumberAction, runSpy) |
| 63 | + .byClasses([AddNumberAction, SubtractNumberAction], runSpy) |
| 64 | + .byType('ADD_NUMBER', runSpy) |
| 65 | + .byTypes(['ADD_NUMBER', 'SUBTRACT_NUMBER'], runSpy) |
| 66 | + .reduce(runSpy); |
| 67 | + |
| 68 | + expect(runSpy).toHaveBeenCalledWith(payload, jasmine.any(AddNumberAction), jasmine.anything()); |
| 69 | + expect(runSpy.calls.count()).toBe(1); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should execute same byClasses for each action', () => { |
| 73 | + const applySwitchReduce = (state: TestState, action: TypedAction<number>) => |
| 74 | + switchReduce(state, action) |
| 75 | + .byClasses([AddNumberAction, SubtractNumberAction], (payload: number, innerAction: TypedAction<number>) => { |
| 76 | + const addend: number = innerAction instanceof AddNumberAction ? payload : -payload; |
| 77 | + return Object.assign({}, state, { |
| 78 | + num: state.num + addend |
| 79 | + }); |
| 80 | + }) |
| 81 | + .reduce(); |
| 82 | + |
| 83 | + const newState1 = applySwitchReduce(testState, new AddNumberAction(1)); |
| 84 | + const newState2 = applySwitchReduce(newState1, new SubtractNumberAction(2)); |
| 85 | + expect(newState1.num).toBe(2); |
| 86 | + expect(newState2.num).toBe(0); |
| 87 | + }); |
| 88 | +}); |
0 commit comments