Skip to content

Commit

Permalink
added initialState property to allowed "reducers"
Browse files Browse the repository at this point in the history
  • Loading branch information
okwolf committed Apr 6, 2017
1 parent f123d44 commit 69fd3f7
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 8 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.4",
"version": "0.0.7-alpha.5",
"description": "Reduced Redux for great justice.",
"author": "Wolfgang Wedemeyer <wolf@okwolf.com>",
"license": "MIT",
Expand Down
17 changes: 17 additions & 0 deletions src/actionMapping.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { initialState } from './';
import { updateStatePathWithReducer } from './paths';
import { actionFormat } from './actionsWithPathAndReducers';

// TODO: find a better way to do this!
// eslint-disable-next-line fp/no-let
let actionMappings = {};

// eslint-disable-next-line fp/no-mutation
export const clearActionMappings = () => actionMappings = {};
export const addActionMapping = ({ type, path, reducer }) =>
// eslint-disable-next-line fp/no-mutation
actionMappings[type] = [
Expand All @@ -11,3 +18,13 @@ export const addActionMapping = ({ type, path, reducer }) =>
}
];
export const getActionMapping = type => actionMappings[type];
export const getInitialState = () =>
(actionMappings[actionFormat(initialState)] || []).reduce((
state,
{ path, reducer }
) =>
updateStatePathWithReducer({
state,
path,
reducer: () => reducer
}), {});
2 changes: 1 addition & 1 deletion src/actionsWithPathAndReducers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addActionMapping } from './actionMapping';

const addUnderscoresBetween = (match, offset) => `${offset ? '_' : ''}${match}`;
const actionFormat = name =>
export const actionFormat = name =>
name.replace(/[A-Z]/g, addUnderscoresBetween).toUpperCase();

export default ({ path, reducers = {} }) =>
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export {
default as actionsWithPathAndReducers
} from './actionsWithPathAndReducers';
export { stateAtPath } from './paths';
export const initialState = '@@initialState';
4 changes: 2 additions & 2 deletions src/makeReducer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { updateStatePathWithReducer } from './paths';
import { getActionMapping } from './actionMapping';
import { getActionMapping, getInitialState } from './actionMapping';

const mapping = action => getActionMapping(action.type);
export default reducer =>
Expand All @@ -13,7 +13,7 @@ export default reducer =>
reducer,
action
}),
state
state || getInitialState()
),
action
);
17 changes: 13 additions & 4 deletions test/actionsWithPathAndReducers.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert, expect } from 'chai';
import { actionsWithPathAndReducers, makeReducer } from '../src';
import { actionsWithPathAndReducers, makeReducer, initialState } from '../src';

describe('actionsWithPathAndReducers', () => {
it('should be a function', () => {
Expand All @@ -21,6 +21,9 @@ describe('actionsWithPathAndReducers', () => {
});
describe('and reducers', () => {
const reducers = {
[initialState]: {
counter: 0
},
inc: ({ counter = 0 }) => ({
counter: counter + 1
}),
Expand All @@ -37,10 +40,16 @@ describe('actionsWithPathAndReducers', () => {
it('should return an object of action creators', () => {
assert.isObject(withAPathAndReducers);
});
it('should call existing reducer for unknown actions', () => {
const unknownAction = {};
it('should have default state for unknown action', () => {
const unknownAction = { type: 'UNKNOWN' };
const stateAfter = reducer(undefined, unknownAction);
assert.isUndefined(stateAfter);
expect(stateAfter).to.deep.equal({
state: {
path: {
counter: 0
}
}
});
});
describe('inc property', () => {
const { inc } = withAPathAndReducers;
Expand Down
15 changes: 15 additions & 0 deletions test/makeReducer.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { assert, expect } from 'chai';
import { makeReducer } from '../src';
import { clearActionMappings } from '../src/actionMapping';

describe('makeReducer', () => {
it('should be a function', () => {
assert.isFunction(makeReducer);
});
it('should create a reducer that returns an empty object', () => {
clearActionMappings();
const reducer = makeReducer(state => state);
const stateAfter = reducer(undefined, {});
expect(stateAfter).to.deep.equal({});
});
});

0 comments on commit 69fd3f7

Please sign in to comment.