Skip to content

Commit

Permalink
Merge pull request #668 from feathersjs-ecosystem/if-else-test
Browse files Browse the repository at this point in the history
iff.else working
  • Loading branch information
fratzinger committed May 31, 2022
2 parents c5da0ca + 0147f67 commit 7da6ece
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/hooks/iff-else.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { PredicateFn } from '../types';
export function iffElse (
predicate: boolean | PredicateFn,
trueHooks: Hook | Hook[] | undefined,
falseHooks: Hook | Hook[] | undefined
falseHooks?: Hook | Hook[] | undefined
): Hook {
// fnArgs is [context] for service & permission hooks, [data, connection, context] for event filters
return function (this: any, ctx: HookContext) {
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/iff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ export function iff (
...hooks: Hook[]
): IffHook {
const iffWithoutElse = function (context: HookContext) {
return iffElse(predicate, hooks.slice(), undefined)(context);
return iffElse(predicate, hooks.slice())(context);
}
iffWithoutElse.else = (...falseHooks: any[]) => iffElse(true, falseHooks.slice(), []);

iffWithoutElse.else = (...falseHooks: any[]) => (context: HookContext) => iffElse(predicate, hooks.slice(), falseHooks.slice())(context);

return iffWithoutElse;
}
19 changes: 19 additions & 0 deletions test/hooks/iff-else.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,25 @@ describe('services iff - runs .else()', () => {
});
});

it('using iff(true, ...).else(...)', () => {
return iff(true,
hookFcnSync,
hookFcnSync,
hookFcnSync
)
.else(
hookFcnSync
)(hook)
// @ts-ignore
.then((hook: any) => {
assert.equal(hookFcnSyncCalls, 3);
assert.equal(hookFcnAsyncCalls, 0);
assert.equal(hookFcnCalls, 0);

assert.deepEqual(hook, hookAfter);
});
});

it('using if(false).else(...)', () => {
return iff(false,
hookFcnSync
Expand Down

0 comments on commit 7da6ece

Please sign in to comment.