Skip to content

Commit

Permalink
feat(Componentize): action will trigger a setState call
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchentw committed Sep 26, 2015
1 parent 1f2227d commit a66e948
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"jsdom": "^6.5.1",
"mocha": "^2.3.3",
"mocha-jsdom": "^1.0.0",
"react": "^0.13.3",
"react": "^0.14.0-rc1",
"react-addons-test-utils": "^0.14.0-rc1",
"redux": "^3.0.1",
"rimraf": "^2.4.3"
},
Expand Down
77 changes: 69 additions & 8 deletions src/components/__tests__/Componentize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import {
import {
default as jsdom,
} from "mocha-jsdom";
// TODO: React@0.14

import {
default as React,
Children,
PropTypes,
Component,
} from "react/addons";
// TODO: React@0.14
// import ReactDOM from `react-dom`;
// import TestUtils from `react-addons-test-utils`;
} from "react";

import {
default as TestUtils,
} from "react-addons-test-utils";

import {
createStore,
} from "redux";
Expand All @@ -23,8 +25,6 @@ import {
Componentize,
} from "../../index";

const {TestUtils} = React.addons;

function noop () {
}

Expand Down Expand Up @@ -191,7 +191,7 @@ describe(`React`, () => {
dispatch({
type: `CUSTOM_ACTION`,
});
}
},
};
};

Expand Down Expand Up @@ -253,6 +253,67 @@ describe(`React`, () => {
comp.render();
});
});

context(`dispatch an action`, () => {
it(`should update the state and pass in to render`, (done) => {
const mapDispatchToActions = (dispatch) => {
return {
getOlder () {
dispatch({
type: `GET_OLDER`,
age: 1,
});
},
};
};

let initialRender = true;

const render = (props, state, actions) => {
expect(props).toBeA(`object`);
expect(props).toEqual({
name: `Tom Chen`,
});

if (initialRender) {
expect(state).toBeA(`object`);
expect(state).toEqual({
age: 0,
});
initialRender = false;
} else {
expect(state).toBeA(`object`);
expect(state).toEqual({
age: 1,
});
done();
}
return null;
};

const initialState = {
age: 0,
};

const reducer = (state = initialState, action) => {
if (action.type === `GET_OLDER`) {
return {
...state,
age: action.age,
};
}
return state;
};

const ReduxComponent = Componentize(createStore, reducer, noop, mapDispatchToActions)(render);

const comp = TestUtils.renderIntoDocument(
<ReduxComponent name="Tom Chen" />
);

comp.eventActions.getOlder();
});
});
});
});
});
4 changes: 4 additions & 0 deletions src/components/createComponentize.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default function createComponentize (React) {
this.eventActions = mapDispatchToActions(this.store.dispatch);

this.state = this.store.getState();

this.unsubscribeFromStore = this.store.subscribe(() => {
this.setState(this.store.getState());
});
}

componentWillMount () {
Expand Down

0 comments on commit a66e948

Please sign in to comment.