Skip to content

Commit

Permalink
what was i thinking?
Browse files Browse the repository at this point in the history
we don't need an enhancer to implement this
just a higher order reducer
  • Loading branch information
okwolf committed Mar 22, 2017
1 parent 22871d7 commit e465620
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 136 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.1",
"version": "0.0.7-alpha.2",
"description": "Reduced Redux for great justice.",
"author": "Wolfgang Wedemeyer <wolf@okwolf.com>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as enhancer } from './enhancer';
export { default as makeReducer } from './makeReducer';
export {
default as actionsWithPathAndReducers
} from './actionsWithPathAndReducers';
Expand Down
7 changes: 1 addition & 6 deletions src/enhancer.js → src/makeReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { updateStatePathWithReducer } from './paths';
import { getActionMapping } from './actionMapping';

const mapping = action => getActionMapping(action.type);

const combinedReducer = reducer =>
export default reducer =>
(state, action) =>
mapping(action)
? updateStatePathWithReducer({
Expand All @@ -13,7 +12,3 @@ const combinedReducer = reducer =>
action
})
: reducer(state, action);

export default createStore =>
(reducer, preloadedState, enhancer) =>
createStore(combinedReducer(reducer), preloadedState, enhancer);
189 changes: 82 additions & 107 deletions test/actionsWithPathAndReducers.spec.js
Original file line number Diff line number Diff line change
@@ -1,133 +1,108 @@
import { assert, expect } from 'chai';
import sinon, { spy } from 'sinon';
import { enhancer, actionsWithPathAndReducers } from '../src';
import createMockStore from './createMockStore';
import { actionsWithPathAndReducers, makeReducer } from '../src';

describe('actionsWithPathAndReducers', () => {
it('should be a function', () => {
assert.isFunction(actionsWithPathAndReducers);
});
describe('when called', () => {
describe('without a store', () => {
describe('without parameters', () => {
it('should throw an error', () => {
assert.throws(() => actionsWithPathAndReducers(), TypeError);
});
});
describe('with a store', () => {
const identityReducer = spy(state => state);
const store = createMockStore(identityReducer, undefined, enhancer);
const { dispatch } = store;
beforeEach(() => {
store.clearState();
dispatch.reset();
});
it('should call existing reducer for unknown actions', () => {
const unknownAction = {};
dispatch(unknownAction);
sinon.assert.alwaysCalledWithExactly(
identityReducer,
undefined,
unknownAction
);
describe('with a path', () => {
const path = 'state.path';
it('should return an empty object', () => {
const withAPathOnly = actionsWithPathAndReducers({
path
});
expect(withAPathOnly).to.deep.equal({});
});
describe('and path', () => {
const path = 'state.path';
describe('without actions', () => {
it('should return an empty object', () => {
const withAPathOnly = actionsWithPathAndReducers({
path
});
expect(withAPathOnly).to.deep.equal({});
});
describe('and reducers', () => {
const reducers = {
inc: ({ counter = 0 }) => ({
counter: counter + 1
}),
incBy: ({ counter = 0 }, { by }) => ({
counter: counter + by
}),
CapitalAction: state => state.toUpperCase()
};
const withAPathAndReducers = actionsWithPathAndReducers({
path,
reducers
});
describe('and reducers', () => {
const reducers = {
inc: ({ counter = 0 }) => ({
counter: counter + 1
}),
incBy: ({ counter = 0 }, { by }) => ({
counter: counter + by
}),
CapitalAction: Function.prototype
};
const withAPathAndReducers = actionsWithPathAndReducers({
path,
reducers
});
it('should return an object of action creators', () => {
assert.isObject(withAPathAndReducers);
const reducer = makeReducer(state => state);
it('should return an object of action creators', () => {
assert.isObject(withAPathAndReducers);
});
it('should call existing reducer for unknown actions', () => {
const unknownAction = {};
const stateAfter = reducer(undefined, unknownAction);
assert.isUndefined(stateAfter);
});
describe('inc property', () => {
const { inc } = withAPathAndReducers;
it('should be a function', () => {
assert.isFunction(inc);
});
describe('inc property', () => {
const { inc } = withAPathAndReducers;
it('should be a function', () => {
assert.isFunction(inc);
it('should create the INC action when called and handle the action in the reducer', () => {
const action = inc();
expect(action).to.deep.equal({
type: 'INC'
});
it('should create the INC action when called and handle the action when dispatched', () => {
const action = inc();
expect(action).to.deep.equal({
type: 'INC'
});
dispatch(action);
sinon.assert.alwaysCalledWithExactly(dispatch, action);
expect(store.getState()).to.deep.equal({
state: {
path: {
counter: 1
}
const stateAfter = reducer(undefined, action);
expect(stateAfter).to.deep.equal({
state: {
path: {
counter: 1
}
});
dispatch(action);
expect(store.getState()).to.deep.equal({
state: {
path: {
counter: 2
}
}
});
}
});
});
describe('incBy property', () => {
const { incBy } = withAPathAndReducers;
it('should be a function', () => {
assert.isFunction(incBy);
});
describe('incBy property', () => {
const { incBy } = withAPathAndReducers;
it('should be a function', () => {
assert.isFunction(incBy);
});
it('should create the INC_BY action when called and handle the action in the reducer', () => {
const action = incBy({ by: 2 });
expect(action).to.deep.equal({
type: 'INC_BY',
by: 2
});
it('should dispatch the INC_BY action when called', () => {
const action = incBy({ by: 2 });
expect(action).to.deep.equal({
type: 'INC_BY',
by: 2
});
dispatch(action);
sinon.assert.alwaysCalledWithExactly(dispatch, action);
expect(store.getState()).to.deep.equal({
state: {
path: {
counter: 2
}
const stateAfter = reducer(undefined, action);
expect(stateAfter).to.deep.equal({
state: {
path: {
counter: 2
}
});
dispatch(incBy({ by: 3 }));
expect(store.getState()).to.deep.equal({
state: {
path: {
counter: 5
}
}
});
}
});
});
describe('CapitalAction property', () => {
const { CapitalAction } = withAPathAndReducers;
it('should be a function', () => {
assert.isFunction(CapitalAction);
});
describe('CapitalAction property', () => {
const { CapitalAction } = withAPathAndReducers;
it('should be a function', () => {
assert.isFunction(CapitalAction);
});
it('should create the CAPITAL_ACTION action when called and handle the action in the reducer', () => {
const action = CapitalAction();
expect(action).to.deep.equal({
type: 'CAPITAL_ACTION'
});
it('should dispatch the CAPITAL_ACTION action when called', () => {
const action = CapitalAction();
expect(action).to.deep.equal({
type: 'CAPITAL_ACTION'
});
dispatch(action);
sinon.assert.alwaysCalledWithExactly(dispatch, action);
const stateBefore = {
state: {
path: 'quiet action'
}
};
const stateAfter = reducer(stateBefore, action);
expect(stateAfter).to.deep.equal({
state: {
path: 'QUIET ACTION'
}
});
});
});
Expand Down
21 changes: 0 additions & 21 deletions test/createMockStore.js

This file was deleted.

0 comments on commit e465620

Please sign in to comment.