-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
invocation-context.unit.ts
135 lines (114 loc) · 3.67 KB
/
invocation-context.unit.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
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/context
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {expect} from '@loopback/testlab';
import {Context, inject, InvocationContext} from '../..';
describe('InvocationContext', () => {
let ctx: Context;
let invocationCtxForGreet: InvocationContext;
let invocationCtxForHello: InvocationContext;
let invocationCtxForCheckName: InvocationContext;
let invalidInvocationCtx: InvocationContext;
let invalidInvocationCtxForStaticMethod: InvocationContext;
before(givenContext);
before(givenInvocationContext);
it('has a getter for targetClass', () => {
expect(invocationCtxForGreet.targetClass).to.equal(MyController);
expect(invocationCtxForCheckName.targetClass).to.equal(MyController);
});
it('has a getter for targetName', () => {
expect(invocationCtxForGreet.targetName).to.equal(
'MyController.prototype.greet',
);
expect(invocationCtxForCheckName.targetName).to.equal(
'MyController.checkName',
);
});
it('has a getter for description', () => {
expect(invocationCtxForGreet.description).to.equal(
`InvocationContext(${invocationCtxForGreet.name}): MyController.prototype.greet`,
);
expect(invocationCtxForCheckName.description).to.equal(
`InvocationContext(${invocationCtxForCheckName.name}): MyController.checkName`,
);
});
it('has public access to parent context', () => {
expect(invocationCtxForGreet.parent).to.equal(ctx);
});
it('throws error if method does not exist', () => {
expect(() => invalidInvocationCtx.assertMethodExists()).to.throw(
'Method MyController.prototype.invalid-method not found',
);
expect(() =>
invalidInvocationCtxForStaticMethod.assertMethodExists(),
).to.throw('Method MyController.invalid-static-method not found');
});
it('invokes target method', async () => {
expect(await invocationCtxForGreet.invokeTargetMethod()).to.eql(
'Hello, John',
);
expect(invocationCtxForCheckName.invokeTargetMethod()).to.eql(true);
});
it('invokes target method with injection', async () => {
expect(
await invocationCtxForHello.invokeTargetMethod({
skipParameterInjection: false,
}),
).to.eql('Hello, Jane');
});
it('does not close when an interceptor is in processing', () => {
const result = invocationCtxForGreet.invokeTargetMethod();
expect(invocationCtxForGreet.isBound('abc'));
return result;
});
class MyController {
static checkName(name: string) {
const firstLetter = name.substring(0, 1);
return firstLetter === firstLetter.toUpperCase();
}
async greet(name: string) {
return `Hello, ${name}`;
}
async hello(@inject('name') name: string) {
return `Hello, ${name}`;
}
}
function givenContext() {
ctx = new Context();
ctx.bind('abc').to('xyz');
}
function givenInvocationContext() {
invocationCtxForGreet = new InvocationContext(
ctx,
new MyController(),
'greet',
['John'],
);
invocationCtxForHello = new InvocationContext(
ctx,
new MyController(),
'hello',
[],
);
invocationCtxForHello.bind('name').to('Jane');
invocationCtxForCheckName = new InvocationContext(
ctx,
MyController,
'checkName',
['John'],
);
invalidInvocationCtx = new InvocationContext(
ctx,
new MyController(),
'invalid-method',
['John'],
);
invalidInvocationCtxForStaticMethod = new InvocationContext(
ctx,
MyController,
'invalid-static-method',
['John'],
);
}
});