-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hooks): Finalize default initializer functionality (#35)
- Loading branch information
Showing
6 changed files
with
197 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { strict as assert } from 'assert'; | ||
import { hooks, middleware, HookContext, NextFunction } from '../src'; | ||
|
||
interface Dummy { | ||
sayHi (name: string): Promise<string>; | ||
addOne (number: number): Promise<number>; | ||
} | ||
|
||
describe('class objectHooks', () => { | ||
let DummyClass: new () => Dummy; | ||
|
||
beforeEach(() => { | ||
DummyClass = class DummyClass implements Dummy { | ||
async sayHi (name: string) { | ||
return `Hi ${name}`; | ||
} | ||
|
||
async addOne (number: number) { | ||
return number + 1; | ||
} | ||
}; | ||
}); | ||
|
||
|
||
it('hooking object on class adds to the prototype', async () => { | ||
hooks(DummyClass, { | ||
sayHi: middleware([async (ctx: HookContext, next: NextFunction) => { | ||
assert.deepStrictEqual(ctx, new DummyClass.prototype.sayHi.Context({ | ||
arguments: ['David'], | ||
method: 'sayHi', | ||
name: 'David', | ||
self: instance | ||
})); | ||
|
||
await next(); | ||
|
||
ctx.result += '?'; | ||
}]).params('name'), | ||
|
||
addOne: middleware([async (ctx: HookContext, next: NextFunction) => { | ||
ctx.arguments[0] += 1; | ||
|
||
await next(); | ||
}]) | ||
}); | ||
|
||
const instance = new DummyClass(); | ||
|
||
assert.strictEqual(await instance.sayHi('David'), 'Hi David?'); | ||
assert.strictEqual(await instance.addOne(1), 3); | ||
}); | ||
|
||
it('works with inheritance', async () => { | ||
hooks(DummyClass, { | ||
sayHi: middleware([async (ctx: HookContext, next: NextFunction) => { | ||
assert.deepStrictEqual(ctx, new (OtherDummy.prototype.sayHi as any).Context({ | ||
arguments: [ 'David' ], | ||
method: 'sayHi', | ||
self: instance | ||
})); | ||
|
||
await next(); | ||
|
||
ctx.result += '?'; | ||
}]) | ||
}); | ||
|
||
class OtherDummy extends DummyClass {} | ||
|
||
hooks(OtherDummy, { | ||
sayHi: middleware([async (ctx: HookContext, next: NextFunction) => { | ||
await next(); | ||
|
||
ctx.result += '!'; | ||
}]) | ||
}); | ||
|
||
const instance = new OtherDummy(); | ||
|
||
assert.strictEqual(await instance.sayHi('David'), 'Hi David?!'); | ||
}); | ||
|
||
it('works with multiple context updaters', async () => { | ||
hooks(DummyClass, { | ||
sayHi: middleware([ | ||
async (ctx, next) => { | ||
assert.equal(ctx.name, 'Dave'); | ||
assert.equal(ctx.gna, 42); | ||
assert.equal(ctx.app, 'ok'); | ||
|
||
ctx.name = 'Changed'; | ||
|
||
await next(); | ||
} | ||
]).params('name') | ||
}); | ||
|
||
class OtherDummy extends DummyClass {} | ||
|
||
hooks(OtherDummy, { | ||
sayHi: middleware([ | ||
async (ctx, next) => { | ||
assert.equal(ctx.name, 'Dave'); | ||
assert.equal(ctx.gna, 42); | ||
assert.equal(ctx.app, 'ok'); | ||
|
||
await next(); | ||
} | ||
]).props({ gna: 42 }) | ||
}); | ||
|
||
const instance = new OtherDummy(); | ||
|
||
hooks(instance, { | ||
sayHi: middleware([ | ||
async (ctx, next) => { | ||
assert.equal(ctx.name, 'Dave'); | ||
assert.equal(ctx.gna, 42); | ||
assert.equal(ctx.app, 'ok'); | ||
|
||
await next(); | ||
} | ||
]).props({ app: 'ok' }) | ||
}); | ||
|
||
assert.equal(await instance.sayHi('Dave'), 'Hi Changed'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.