Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyystop committed Feb 22, 2019
2 parents c7b03d9 + 7108df5 commit 8a46d88
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
24 changes: 23 additions & 1 deletion tests/services/iffelse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ describe('services iffElse', () => {
hookFcnAsyncCalls = 0;
});

describe('runs single hook', () => {
it('when true', () => {
return hooks.iffElse(true, hookFcnSync, hookFcnAsync)(hook)
.then(hook => {
assert.deepEqual(hook, hookAfter);
assert.equal(hookFcnSyncCalls, 1);
assert.equal(hookFcnAsyncCalls, 0);
assert.deepEqual(hook, hookAfter);
});
});

it('when false', () => {
return hooks.iffElse(false, hookFcnSync, hookFcnAsync)(hook)
.then(hook => {
assert.deepEqual(hook, hookAfter);
assert.equal(hookFcnSyncCalls, 0);
assert.equal(hookFcnAsyncCalls, 1);
assert.deepEqual(hook, hookAfter);
});
});
});

describe('runs multiple hooks', () => {
it('when true', () => {
return hooks.iffElse(true, [hookFcnSync, hookFcnAsync, hookFcnCb], null)(hook)
Expand Down Expand Up @@ -154,6 +176,6 @@ describe('services iffElse', () => {

// Helpers

function clone (obj) {
function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ export function validateSchema(schema: object, ajv: AjvOrNewable, options?: Vali
* Execute one array of hooks or another based on a sync or async predicate.
* {@link https://feathers-plus.github.io/v1/feathers-hooks-common/index.html#IffElse}
*/
export function iffElse(predicate: SyncPredicateFn, hooksTrue: Hook | Hook[], hooksFalse: Hook | Hook[]): Hook;
export function iffElse(predicate: PredicateFn, hooksTrue: Hook | Hook[], hooksFalse: Hook | Hook[]): Hook;

export interface IffHook extends Hook {
else(...hooks: Hook[]): Hook;
Expand All @@ -557,7 +557,7 @@ export interface IffHook extends Hook {
* Execute one or another series of hooks depending on a sync or async predicate.
* {@link https://feathers-plus.github.io/v1/feathers-hooks-common/index.html#Iff}
*/
export function iff(predicate: SyncPredicateFn, ...hooks: Hook[]): IffHook;
export function iff(predicate: PredicateFn, ...hooks: Hook[]): IffHook;

/**
* Alias for iff
Expand Down
22 changes: 14 additions & 8 deletions types/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ const fgraphqlOptions2: FGraphQLHookOptions = {
Query: {}
},
options: {
extraAuthProps: ['asdf' ],
extraAuthProps: ['asdf'],
inclAllFields: false,
inclJoinedNames: false,
inclAllFieldsClient: true,
Expand Down Expand Up @@ -306,7 +306,7 @@ makeCallingParams(
// tslint:disable-next-line
class ObjId {
// tslint:disable-next-line
constructor(id?: string | number) {}
constructor(id?: string | number) { }
}

// $ExpectType Hook
Expand Down Expand Up @@ -388,17 +388,17 @@ setNow('createdAt', 'updatedAt');
setSlug('storeId');

// $ExpectType Hook
sequelizeConvert ({
sequelizeConvert({
aBool: 'boolean',
aDate: 'date',
anObject: 'json',
aNumber: 'strToInt'
}, null, {
strToInt: {
js: (sqlValue: string) => +sqlValue,
sql: (jsValue: number) => `${jsValue}`,
}
});
strToInt: {
js: (sqlValue: string) => +sqlValue,
sql: (jsValue: number) => `${jsValue}`,
}
});

// $ExpectType Hook
sifter(ctx => item => true);
Expand Down Expand Up @@ -449,11 +449,17 @@ validateSchema({}, ajv);

// $ExpectType Hook
iffElse(syncTrue, [hook1, hook2], [hook3, hook4]);
// $ExpectType Hook
iffElse(asyncTrue, [hook1, hook2], [hook3, hook4]);

// $ExpectType IffHook
iff(syncTrue, hook1, hook2);
// $ExpectType IffHook
iff(asyncTrue, hook1, hook2);
// $ExpectType Hook
iff(syncTrue, hook1, hook2).else(hook3, hook4);
// $ExpectType Hook
iff(asyncTrue, hook1, hook2).else(hook3, hook4);

// $ExpectType IffHook
when(syncTrue, hook1, hook2);
Expand Down

0 comments on commit 8a46d88

Please sign in to comment.