Skip to content

Commit

Permalink
handling duplicate actions, just for the hell of it
Browse files Browse the repository at this point in the history
  • Loading branch information
okwolf committed Mar 22, 2017
1 parent e465620 commit 7b2549f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "erux",
"version": "0.0.7-alpha.2",
"version": "0.0.7-alpha.3",
"description": "Reduced Redux for great justice.",
"author": "Wolfgang Wedemeyer <wolf@okwolf.com>",
"license": "MIT",
Expand Down
11 changes: 7 additions & 4 deletions src/actionMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ let actionMappings = {};

export const addActionMapping = ({ type, path, reducer }) =>
// eslint-disable-next-line fp/no-mutation
actionMappings[type] = {
path,
reducer
};
actionMappings[type] = [
...(actionMappings[type] || []),
{
path,
reducer
}
];
export const getActionMapping = type => actionMappings[type];
21 changes: 13 additions & 8 deletions src/makeReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import { getActionMapping } from './actionMapping';
const mapping = action => getActionMapping(action.type);
export default reducer =>
(state, action) =>
mapping(action)
? updateStatePathWithReducer({
state,
path: mapping(action).path,
reducer: mapping(action).reducer,
action
})
: reducer(state, action);
reducer(
(mapping(action) || []).reduce(
(currentState, { path, reducer }) =>
updateStatePathWithReducer({
state: currentState,
path,
reducer,
action
}),
state
),
action
);
29 changes: 29 additions & 0 deletions test/actionsWithPathAndReducers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,35 @@ describe('actionsWithPathAndReducers', () => {
});
});
});

describe('that have duplicate inc reducer for a different path', () => {
it('should create the INC action when called and update two parts of the state', () => {
const withDifferentPathAndDupReducers = actionsWithPathAndReducers({
path: 'different.path',
reducers: {
inc: reducers.inc
}
});
const { inc } = withDifferentPathAndDupReducers;
const action = inc();
expect(action).to.deep.equal({
type: 'INC'
});
const stateAfter = reducer(undefined, action);
expect(stateAfter).to.deep.equal({
different: {
path: {
counter: 1
}
},
state: {
path: {
counter: 1
}
}
});
});
});
});
});
});
Expand Down

0 comments on commit 7b2549f

Please sign in to comment.