Skip to content

Commit fb6a7ee

Browse files
committed
use createReducer for createSlice and add tests
1 parent 865b171 commit fb6a7ee

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

src/createSlice.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import createNextState from 'immer';
1+
import { createReducer } from './createReducer';
22

3-
const defaultReducer = (state) => state;
43
const getType = (slice, action) =>
54
slice ? `${slice}/${action}` : action;
65

@@ -19,11 +18,7 @@ export default function createSlice({
1918
{},
2019
);
2120

22-
const reducer = (state = initialState, { type, payload }) => {
23-
const actionReducer = reducerMap[type] || defaultReducer;
24-
const produce = (draft) => actionReducer(draft, payload);
25-
return createNextState(state, produce);
26-
};
21+
const reducer = createReducer(initialState, reducerMap);
2722

2823
const actionMap = actionKeys.reduce(
2924
(map, action) => {

src/createSlice.test.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)