Skip to content

Commit 7da6ece

Browse files
authored
Merge pull request #668 from feathersjs-ecosystem/if-else-test
iff.else working
2 parents c5da0ca + 0147f67 commit 7da6ece

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/hooks/iff-else.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { PredicateFn } from '../types';
1010
export function iffElse (
1111
predicate: boolean | PredicateFn,
1212
trueHooks: Hook | Hook[] | undefined,
13-
falseHooks: Hook | Hook[] | undefined
13+
falseHooks?: Hook | Hook[] | undefined
1414
): Hook {
1515
// fnArgs is [context] for service & permission hooks, [data, connection, context] for event filters
1616
return function (this: any, ctx: HookContext) {

src/hooks/iff.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ export function iff (
1111
...hooks: Hook[]
1212
): IffHook {
1313
const iffWithoutElse = function (context: HookContext) {
14-
return iffElse(predicate, hooks.slice(), undefined)(context);
14+
return iffElse(predicate, hooks.slice())(context);
1515
}
16-
iffWithoutElse.else = (...falseHooks: any[]) => iffElse(true, falseHooks.slice(), []);
16+
17+
iffWithoutElse.else = (...falseHooks: any[]) => (context: HookContext) => iffElse(predicate, hooks.slice(), falseHooks.slice())(context);
1718

1819
return iffWithoutElse;
1920
}

test/hooks/iff-else.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,25 @@ describe('services iff - runs .else()', () => {
347347
});
348348
});
349349

350+
it('using iff(true, ...).else(...)', () => {
351+
return iff(true,
352+
hookFcnSync,
353+
hookFcnSync,
354+
hookFcnSync
355+
)
356+
.else(
357+
hookFcnSync
358+
)(hook)
359+
// @ts-ignore
360+
.then((hook: any) => {
361+
assert.equal(hookFcnSyncCalls, 3);
362+
assert.equal(hookFcnAsyncCalls, 0);
363+
assert.equal(hookFcnCalls, 0);
364+
365+
assert.deepEqual(hook, hookAfter);
366+
});
367+
});
368+
350369
it('using if(false).else(...)', () => {
351370
return iff(false,
352371
hookFcnSync

0 commit comments

Comments
 (0)