deno.land / x / hooks@v0.6.5 / test / deno_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
import { assertEquals } from 'https://deno.land/std@0.91.0/testing/asserts.ts';
import { hooks, HookContext, NextFunction, middleware } from '../deno/index.ts';
const hello = async (name?: string, _params: any = {}) => { return `Hello ${name}`;};
class DummyClass { async sayHi (name: string) { return `Hi ${name}`; }
async addOne (number: number) { return number + 1; }}
Deno.test('can override context.result after', async () => { const updateResult = async (ctx: HookContext, next: NextFunction) => { await next();
ctx.result += ' You!'; };
const fn = hooks(hello, [ updateResult ]); const res = await fn('There');
assertEquals(res, 'Hello There You!');});
Deno.test('hooking object on class adds to the prototype', async () => { hooks(DummyClass, { sayHi: middleware([ async (ctx: HookContext, next: NextFunction) => { assertEquals(ctx, new (DummyClass.prototype.sayHi as any).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();
assertEquals(await instance.sayHi('David'), 'Hi David?'); assertEquals(await instance.addOne(1), 3);});
hooks

Version Info

Tagged at
3 years ago