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

Ability to module-namespace actions #7

Closed
wants to merge 1 commit into from
Closed
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
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