Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/__tests__/handleAction-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handleAction } from '../';
import { handleAction, createAction } from '../';

describe('handleAction()', () => {
const type = 'TYPE';
Expand Down Expand Up @@ -29,6 +29,18 @@ describe('handleAction()', () => {
});
});

it('accepts action function as action type', () => {
const incrementAction = createAction(type);
const reducer = handleAction(incrementAction, (state, action) => ({
counter: state.counter + action.payload
}));

expect(reducer(prevState, incrementAction(7)))
.to.deep.equal({
counter: 10
});
});

it('accepts single function as handler and a default state', () => {
const reducer = handleAction(type, (state, action) => ({
counter: state.counter + action.payload
Expand Down
16 changes: 15 additions & 1 deletion src/__tests__/handleActions-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handleActions } from '../';
import { handleActions, createAction } from '../';

describe('handleActions', () => {
it('create a single handler from a map of multiple action handlers', () => {
Expand Down Expand Up @@ -53,4 +53,18 @@ describe('handleActions', () => {
counter: 10
});
});

it('accepts action function as action type', () => {
const incrementAction = createAction('INCREMENT');
const reducer = handleActions({
[incrementAction]: ({ counter }, { payload: amount }) => ({
counter: counter + amount
})
});

expect(reducer({ counter: 3 }, incrementAction(7)))
.to.deep.equal({
counter: 10
});
});
});
7 changes: 5 additions & 2 deletions src/createAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ export default function createAction(type, actionCreator, metaCreator) {
const finalActionCreator = typeof actionCreator === 'function'
? actionCreator
: identity;

return (...args) => {
const actionHandler = (...args) => {
const action = {
type,
payload: finalActionCreator(...args)
Expand All @@ -24,4 +23,8 @@ export default function createAction(type, actionCreator, metaCreator) {

return action;
};

actionHandler.toString = () => type;

return actionHandler;
}
6 changes: 5 additions & 1 deletion src/handleAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ function isFunction(val) {
}

export default function handleAction(type, reducers, defaultState) {
const typeValue = isFunction(type)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply use String(type).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean const typeValue = String(type) instead of const typeValue = isFunction(type) ? type.toString() : type ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the String() function converts the passed value to a string, calling the toString() method if necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't always convert type to string type because it could be a Symbol. There is a test for that https://github.com/acdlite/redux-actions/blob/master/src/__tests__/handleActions-test.js#L25 . By the way toString() has a slightly better performance https://jsperf.com/string-vs-tostring2

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are excellent reason, sorry for bothering you :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review! :)

? type.toString()
: type;

return (state = defaultState, action) => {
// If action type does not match, return previous state
if (action.type !== type) return state;
if (action.type !== typeValue) return state;

const handlerKey = action.error === true ? 'throw' : 'next';

Expand Down