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

object.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as assert from 'assert';import { hooks, middleware, HookContext, NextFunction } from '../src';
interface HookableObject { test: string; sayHi (name: string): Promise<string>; addOne (number: number): Promise<number>;}
describe('objectHooks', () => { let obj: HookableObject;
beforeEach(() => { obj = { test: 'me',
async sayHi (name: string) { return `Hi ${name}`; },
async addOne (number: number) { return number + 1; } }; });
it('hooks object with hook methods, sets method name', async () => { const hookedObj = hooks(obj, { sayHi: middleware([async (ctx: HookContext, next: NextFunction) => { assert.strictEqual(ctx.method, 'sayHi'); assert.deepEqual(ctx, new (obj.sayHi as any).Context({ arguments: [ 'David' ], method: 'sayHi', self: obj }));
await next();
ctx.result += '?'; }]), addOne: middleware([async (ctx: HookContext, next: NextFunction) => { ctx.arguments[0] += 1;
await next(); }]) });
assert.strictEqual(obj, hookedObj); assert.strictEqual(await hookedObj.sayHi('David'), 'Hi David?'); assert.strictEqual(await hookedObj.addOne(1), 3); });
it('hooks object and allows to customize context for method', async () => { const hookedObj = hooks(obj, { sayHi: middleware([async (ctx: HookContext, next: NextFunction) => { assert.deepStrictEqual(ctx, new (obj.sayHi as any).Context({ arguments: ['David'], method: 'sayHi', name: 'David', self: obj }));
ctx.name = 'Dave';
await next();
ctx.result += '?'; }]).params('name'),
addOne: middleware([async (ctx: HookContext, next: NextFunction) => { ctx.arguments[0] += 1;
await next(); }]) });
assert.strictEqual(obj, hookedObj); assert.strictEqual(await hookedObj.sayHi('David'), 'Hi Dave?'); assert.strictEqual(await hookedObj.addOne(1), 3); });
it('hooking multiple times works properly', async () => { hooks(obj, { sayHi: middleware([async (ctx: HookContext, next: NextFunction) => { await next();
ctx.result += '?'; }]) });
hooks(obj, { sayHi: middleware([async (ctx: HookContext, next: NextFunction) => { await next();
ctx.result += '!'; }]) });
assert.strictEqual(await obj.sayHi('David'), 'Hi David!?'); });
it('throws an error when hooking invalid method', async () => { try { hooks(obj, { test: middleware([async (_ctx, next) => { await next(); }]) }); assert.fail('Should never get here'); } catch (error) { assert.strictEqual(error.message, `Can not apply hooks. 'test' is not a function`); } });
it('works with object level hooks', async () => { hooks(obj, [ async (ctx: HookContext, next: NextFunction) => { await next();
ctx.result += '!'; } ]);
hooks(obj, { sayHi: middleware([async (ctx: HookContext, next: NextFunction) => { await next();
ctx.result += '?'; }]) });
assert.strictEqual(await obj.sayHi('Dave'), 'Hi Dave?!'); });});
hooks

Version Info

Tagged at
3 years ago