Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Redux support #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"parser": "babel-eslint",

"plugins": [
"babel"
"babel",
"react"
],

"env": {
Expand Down Expand Up @@ -209,6 +210,8 @@
"vars-on-top": 0,
"wrap-iife": 2,
"wrap-regex": 0,
"yoda": [2, "never", {"exceptRange": true}]
"yoda": [2, "never", {"exceptRange": true}],
"react/jsx-uses-react": 1,
"react/jsx-uses-vars": 1
}
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"mocha": "2.x.x",
"chai": "3.x.x",
"eslint": "1.7.x",
"eslint-plugin-react": "3.15.x",
"babel-eslint": "4.x.x",
"eslint-plugin-babel": "2.x.x",
"babel-cli": "6.x.x",
Expand All @@ -36,6 +37,8 @@
"dependencies": {
"babel-runtime": "6.x.x",
"react-komposer": "^1.8.0",
"react-simple-di": "^1.2.0"
"react-redux": "^4.4.5",
"react-simple-di": "^1.2.0",
"redux": "^3.5.1"
}
}
37 changes: 37 additions & 0 deletions src/__tests__/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ describe('App', () => {
});
});

describe('has reducers field', () => {
it('should fail if reducers is not an object whose values are ' +
'reducer functions', () => {
const app = new App({});
const module = {
reducers() {}
};
const errorMatch = /Module's reducers field should/;
expect(app.loadModule.bind(app, module)).to.throw(errorMatch);
});

it('should save reducers', () => {
const app = new App({});
const module = {
reducers: {
foo() {}
}
};

app.loadModule(module);
expect(app._reducers).to.be.deep.equal(module.reducers);
});
});

it('should merge actions with app wide global actions', () => {
const app = new App({});
app.actions = {bb: 10};
Expand Down Expand Up @@ -136,6 +160,19 @@ describe('App', () => {
expect(calledRoutes).to.deep.equal([ 1, 2 ]);
});

it('should create the Redux store from saved reducers', () => {
const app = new App({});
app._reducers = {
foo(state = {}) {
return state;
}
};

app.init();
expect(app.context.ReduxStore.getState()).to.deep.equal({
foo: {}
});
});

it('should mark as initialized', () => {
const app = new App({});
Expand Down
24 changes: 24 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
injectDeps
} from 'react-simple-di';
import { combineReducers, createStore } from 'redux';

export default class App {
constructor(context) {
Expand All @@ -12,6 +13,7 @@ export default class App {
this.context = context;
this.actions = {};
this._routeFns = [];
this._reducers = {};
}

loadModule(module) {
Expand All @@ -36,6 +38,20 @@ export default class App {
this._routeFns.push(module.routes);
}

// Load Redux reducers
if (module.reducers) {
const reducers = module.reducers;

if (typeof reducers !== 'object') {
const message = `Module's reducers field should be a map of reducers.`;
throw new Error(message);
}

for (const key of Object.keys(reducers)) {
this._reducers[key] = reducers[key];
}
}

const actions = module.actions || {};
this.actions = {
...this.actions,
Expand Down Expand Up @@ -64,7 +80,15 @@ export default class App {
routeFn(inject, this.context, this.actions);
}

// Create Redux store in the context
const reducers = this._reducers;
if (Object.keys(reducers).length > 0) {
const combined = combineReducers(reducers);
this.context.ReduxStore = createStore(combined);
}

this._routeFns = [];
this._reducers = {};
this.__initialized = true;
}

Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
} from 'react-komposer';

import App from './app';
import {
default as _withRedux
} from './redux_container';

// export this module's functions
export const createApp = (...args) => (new App(...args));
Expand All @@ -26,3 +29,6 @@ export const composeWithPromise = _composeWithPromise;
export const composeWithObservable = _composeWithObservable;
export const composeAll = _composeAll;
export const disable = _disable;

// export
export const withRedux = _withRedux;
24 changes: 24 additions & 0 deletions src/redux_container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
import { Provider, connect } from 'react-redux';
import { useDeps } from 'react-simple-di';
import { composeAll } from 'react-komposer';

const withRedux = (mapStateToProps, mapDispatchToProps, mergeProps,
options) => {
const provider = Component => {
return props => {
const {context, children, ...nextProps} = props;
const component = React.createElement(Component, nextProps, children);
return React.createElement(Provider,
{store: context().ReduxStore}, component);
};
};

return composeAll(
connect(mapStateToProps, mapDispatchToProps, mergeProps, options),
provider,
useDeps()
);
};

export default withRedux;