diff --git a/src/hooks/iff-else.ts b/src/hooks/iff-else.ts index 8edf9d90..589e5be9 100755 --- a/src/hooks/iff-else.ts +++ b/src/hooks/iff-else.ts @@ -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) { diff --git a/src/hooks/iff.ts b/src/hooks/iff.ts index 03199b5d..66acb80e 100755 --- a/src/hooks/iff.ts +++ b/src/hooks/iff.ts @@ -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; } diff --git a/test/hooks/iff-else.test.ts b/test/hooks/iff-else.test.ts index 8b213d22..bc58860d 100755 --- a/test/hooks/iff-else.test.ts +++ b/test/hooks/iff-else.test.ts @@ -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