Skip to content

Commit

Permalink
feat(Componentize): add mapDispatchToActions to _4
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchentw committed Sep 26, 2015
1 parent c4f0d8b commit 58a0f1b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
34 changes: 30 additions & 4 deletions src/components/__tests__/Componentize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe(`React`, () => {

describe(`(createStore, reducer)`, () => {
it(`should create redux store inside the constructor of the ReduxComponent`, () => {
const ReduxComponent = Componentize(createStore, () => ({}), noop)();
const ReduxComponent = Componentize(createStore, () => ({}), noop, noop)();
const comp = new ReduxComponent();

expect(comp.store).toBeA(`object`);
Expand All @@ -62,7 +62,7 @@ describe(`React`, () => {

describe(`(_1, _2, mapDispatchToLifecycle)`, () => {
it(`should contain React.Component lifecycle functions`, () => {
const ReduxComponent = Componentize(createStore, () => ({}), noop)();
const ReduxComponent = Componentize(createStore, () => ({}), noop, noop)();
const comp = new ReduxComponent();

expect(comp.componentWillMount).toBeA(`function`);
Expand Down Expand Up @@ -90,7 +90,7 @@ describe(`React`, () => {

const mapDispatchToLifecycle = () => lifecycleCallbacks;

const ReduxComponent = Componentize(createStore, () => ({}), mapDispatchToLifecycle)();
const ReduxComponent = Componentize(createStore, () => ({}), mapDispatchToLifecycle, noop)();
const comp = new ReduxComponent();

Object.keys(spies).forEach(key =>
Expand Down Expand Up @@ -123,7 +123,7 @@ describe(`React`, () => {

const mapDispatchToLifecycle = () => lifecycleCallbacks;

const ReduxComponent = Componentize(createStore, () => ({}), mapDispatchToLifecycle)();
const ReduxComponent = Componentize(createStore, () => ({}), mapDispatchToLifecycle, noop)();
const comp = new ReduxComponent({
name: `Tom Chen`,
});
Expand Down Expand Up @@ -181,5 +181,31 @@ describe(`React`, () => {
});
});
});

describe(`(_1, _2, _3, mapDispatchToActions) with render function`, () => {
context(`(_1, _2, _3, mapDispatchToActions)`, () => {
it(`should pass actions as third arguments of render`, (done) => {
const mapDispatchToActions = (dispatch) => {
return {
customAction () {
dispatch({
type: `CUSTOM_ACTION`,
});
}
};
};

const render = (props, state, actions) => {
expect(actions.customAction).toBeA(`function`);
done();
};

const ReduxComponent = Componentize(createStore, () => ({}), noop, mapDispatchToActions)(render);
const comp = new ReduxComponent();

comp.render();
});
});
});
});
});
6 changes: 4 additions & 2 deletions src/components/createComponentize.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function createComponentize (React) {
componentWillUnmount () {},
};

return function Componentize (createStore, reducer, mapDispatchToLifecycle) {
return function Componentize (createStore, reducer, mapDispatchToLifecycle, mapDispatchToActions) {

return function createComponent (render) {

Expand All @@ -28,6 +28,8 @@ export default function createComponentize (React) {
// TODO: componentWillReceiveProps
...mapDispatchToLifecycle(this.store.dispatch),
};

this.eventActions = mapDispatchToActions(this.store.dispatch);
}

componentWillMount () {
Expand Down Expand Up @@ -55,7 +57,7 @@ export default function createComponentize (React) {
}

render () {
return null;
return render(this.props, this.state, this.eventActions);
}
};
};
Expand Down

0 comments on commit 58a0f1b

Please sign in to comment.