Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
markshust committed Aug 10, 2016
1 parent 6ae4eca commit e2ee740
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
31 changes: 31 additions & 0 deletions lib/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,37 @@ describe('Depedancy Injection', () => {
expect(el.html()).to.match(/arunoda-susiripala/);
});

it('should inject namespaced actions and allow to use them', () => {
const context = {name: 'arunoda'};
const actions = {
core: {
default: {
getFullName({ name }, surname) {
return `${name}-${surname}`;
}
}
}
};
const Layout = ({children}) => children;
const LayoutWithDeps = injectDeps(context, actions)(Layout);

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

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

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

it('should let use inject actions multiple times', () => {
const context = {name: 'arunoda'};
const actions = {
Expand Down
33 changes: 25 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,33 @@ const getDisplayName = Component => (

export function injectDeps(context, _actions) {
const 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)) {
newActionMap[actionName] = actionMap[actionName].bind(null, context);
for (let namespace in _actions) {
if (_actions.hasOwnProperty(namespace)) {
actions[namespace] = {};
let namespaceActions = _actions[namespace];
for (let namespaceAction in namespaceActions) {
if (namespaceActions.hasOwnProperty(namespaceAction)) {
actions[namespace][namespaceAction] = {};
let actionFuncs = namespaceActions[namespaceAction];
let actionFuncsExist = false;
for (let actionFunc in actionFuncs) {
if (actionFuncs.hasOwnProperty(actionFunc) &&
typeof actionFuncs[actionFunc] === 'function'
) {
actions[namespace][namespaceAction][actionFunc] =
actionFuncs[actionFunc].bind(null, context);
actionFuncsExist = true;
}
}
if (!actionFuncsExist &&
actions.hasOwnProperty(namespace) &&
typeof actionFuncs === 'function'
) {
actions[namespace][namespaceAction] =
actionFuncs.bind(null, context);
}
}
}
actions[key] = newActionMap;
}
}

Expand Down

0 comments on commit e2ee740

Please sign in to comment.