deno.land / x / hooks@v0.6.5 / test / decorator.test.ts

decorator.test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import * as assert from 'assert';import { hooks, HookContext, NextFunction, middleware } from '../src';
describe('hookDecorator', () => { it('hook decorator on method and classes with inheritance', async () => { const expectedName = 'David NameFromTopLevel NameFromDummyClass';
@hooks([ async (ctx, next) => { ctx.arguments[0] += ' NameFromTopLevel';
await next();
ctx.result += ' ResultFromTopLevel'; } ]) class TopLevel {}
@hooks([async (ctx, next) => { ctx.arguments[0] += ' NameFromDummyClass';
await next();
ctx.result += ' ResultFromDummyClass'; }]) class DummyClass extends TopLevel { @hooks(middleware([ async (ctx: HookContext, next: NextFunction) => { assert.strictEqual(ctx.method, 'sayHi'); assert.deepEqual(ctx.arguments, [expectedName]); assert.deepEqual(ctx.name, expectedName);
await next();
ctx.result += ' ResultFromMethodDecorator'; } ]).params('name')) async sayHi (name: string) { return `Hi ${name}`; }
@hooks() async hookedFn () { return 'Hooks with nothing'; }
@hooks([async (_ctx: HookContext, next: NextFunction) => next()]) async sayWorld () { return 'World'; } }
const instance = new DummyClass();
assert.strictEqual( await instance.sayHi('David'), `Hi ${expectedName} ResultFromMethodDecorator ResultFromDummyClass ResultFromTopLevel` ); });
it('error cases', () => { assert.throws(() => hooks([])({}, 'test', { value: 'not a function' }), { message: `Can not apply hooks. 'test' is not a function` }); });});
hooks

Version Info

Tagged at
3 years ago