|
| 1 | +import createSlice from './createSlice' |
| 2 | + |
| 3 | +describe('createSlice', () => { |
| 4 | + describe('when slice is empty', () => { |
| 5 | + const { actions, reducer } = createSlice({ |
| 6 | + actions: { |
| 7 | + increment: state => state + 1, |
| 8 | + multiply: (state, action) => state * action.payload |
| 9 | + }, |
| 10 | + initialState: 0 |
| 11 | + }) |
| 12 | + |
| 13 | + it('should create increment action', () => { |
| 14 | + expect(actions.hasOwnProperty('increment')).toBe(true) |
| 15 | + }) |
| 16 | + |
| 17 | + it('should create multiply action', () => { |
| 18 | + expect(actions.hasOwnProperty('multiply')).toBe(true) |
| 19 | + }) |
| 20 | + |
| 21 | + it('should have the correct action for increment', () => { |
| 22 | + expect(actions.increment()).toEqual({ |
| 23 | + type: 'increment', |
| 24 | + payload: undefined |
| 25 | + }) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should have the correct action for multiply', () => { |
| 29 | + expect(actions.multiply(3)).toEqual({ |
| 30 | + type: 'multiply', |
| 31 | + payload: 3 |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + describe('when using reducer', () => { |
| 36 | + it('should return the correct value from reducer with increment', () => { |
| 37 | + expect(reducer(undefined, actions.increment())).toEqual(1) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should return the correct value from reducer with multiply', () => { |
| 41 | + expect(reducer(2, actions.multiply(3))).toEqual(6) |
| 42 | + }) |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + describe('when passing slice', () => { |
| 47 | + const { actions, reducer } = createSlice({ |
| 48 | + actions: { |
| 49 | + increment: state => state + 1 |
| 50 | + }, |
| 51 | + initialState: 0, |
| 52 | + slice: 'cool' |
| 53 | + }) |
| 54 | + |
| 55 | + it('should create increment action', () => { |
| 56 | + expect(actions.hasOwnProperty('increment')).toBe(true) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should have the correct action for increment', () => { |
| 60 | + expect(actions.increment()).toEqual({ |
| 61 | + type: 'cool/increment', |
| 62 | + payload: undefined |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + it('should return the correct value from reducer', () => { |
| 67 | + expect(reducer(undefined, actions.increment())).toEqual(1) |
| 68 | + }) |
| 69 | + }) |
| 70 | +}) |
0 commit comments