Skip to content

Commit

Permalink
Accept new state as the return value of action case
Browse files Browse the repository at this point in the history
  • Loading branch information
cjies committed May 25, 2018
1 parent 20eec14 commit 90d7212
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
11 changes: 8 additions & 3 deletions examples/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import * as duck from '../src/duck';
// -------------------------------------

const UPDATE = duck.defineType('message', 'UPDATE');
const RESET = duck.defineType('message', 'RESET');

// -------------------------------------
// Actions
// -------------------------------------

const updateAction = duck.createAction(UPDATE);
const reset = duck.createAction(RESET);

// Dispatch action
// Dispatch action in anywhere
// dispatch(updateAction('hello world!'));

// -------------------------------------
Expand All @@ -30,8 +32,11 @@ const initState: State = {
};

export default duck.createReducer(initState, {
UPDATE: (state, action) => {
// `state.message` should be 'hello world!'
[UPDATE]: (state, action) => {
// message should turn to 'hello world!'
state.message = action.payload;
},
[RESET]: () => {
return initState;
},
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-duck-immer",
"version": "1.0.1",
"version": "1.0.2",
"description":
"Providing redux helpers to implement ducks-modular-redux proposal and Immer for immutability",
"repository": "git@github.com:cjies/redux-duck-immer.git",
Expand Down
2 changes: 1 addition & 1 deletion src/duck.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function createReducer<S, P: any>(
return (state: S = initState, action: Action<P>): S => {
return produce(state, (draftState: S): void | S => {
if (action && cases[action.type]) {
cases[action.type](draftState, action);
return cases[action.type](draftState, action);
}
});
};
Expand Down
31 changes: 24 additions & 7 deletions src/duck.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,25 @@ test('create action creator', t => {

test('create reducer', t => {
const UPDATE_MESSAGE = duck.defineType('UPDATE_MESSAGE');
const updateMessage = {
type: UPDATE_MESSAGE,
payload: 'hello world',
};
const RESET = duck.defineType('RESET');

function updateMessage(message) {
return {
type: UPDATE_MESSAGE,
payload: message,
};
}
const reset = duck.createAction(RESET);

const initState = {
message: '',
};
const testReducer = duck.createReducer(initState, {
[UPDATE_MESSAGE]: (state, action) => {
state.message = action.payload;
[UPDATE_MESSAGE]: (state, { payload }) => {
state.message = payload;
},
[RESET]: () => {
return initState;
},
});

Expand All @@ -77,7 +85,8 @@ test('create reducer', t => {
'the reducer should be able to return the default state'
);

const testState = testReducer({}, updateMessage);
const testState = testReducer({}, updateMessage('hello world'));
const testState2 = testReducer(testState, reset());

t.deepEqual(
testState,
Expand All @@ -86,6 +95,14 @@ test('create reducer', t => {
},
'the reducer should work with the defined cases'
);

t.deepEqual(
testState2,
{
message: '',
},
'the reducer should work with the defined cases'
);
});

// Reducer state
Expand Down

0 comments on commit 90d7212

Please sign in to comment.