Skip to content

Commit

Permalink
Fix issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
arunoda committed Jan 17, 2016
1 parent 6bc11fe commit 38a60fd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
33 changes: 33 additions & 0 deletions lib/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,39 @@ describe('Depedancy Injection', () => {
expect(el.html()).to.match(/arunoda-susiripala/);
});

it('should let use inject actions multiple times', () => {
const context = {name: 'arunoda'};
const actions = {
default: {
getFullName({name}, surname) {
return `${name}-${surname}`;
}
}
};
const Layout = ({children}) => children;

// Injecting again to make sure action binding works fine.
injectDeps(context, actions)(Layout)

const LayoutWithDeps = injectDeps(context, actions)(Layout);

const Comp = ({getName}) => (<p>{getName('susiripala')}</p>);
const mapper = (c, a) => ({
getName: a.default.getFullName
});
const CompWithDeps = useDeps(mapper)(Comp);

const el = shallow((
<div>
<LayoutWithDeps>
<CompWithDeps />
</LayoutWithDeps>
</div>
));

expect(el.html()).to.match(/arunoda-susiripala/);
});

it('should use a default mapper if no mapper is provided', () => {
const context = {name: 'arunoda'};
const actions = {
Expand Down
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ export function injectDeps(context, _actions) {
for (let key in _actions) {
if (_actions.hasOwnProperty(key)) {
const actionMap = _actions[key];
const newActionMap = {};
for (let actionName in actionMap) {
if (actionMap.hasOwnProperty(actionName)) {
actionMap[actionName] = actionMap[actionName].bind(null, context);
newActionMap[actionName] = actionMap[actionName].bind(null, context);
}
}
actions[key] = actionMap;
actions[key] = newActionMap;
}
}

Expand Down

0 comments on commit 38a60fd

Please sign in to comment.