/
inject-view.unit.ts
160 lines (142 loc) · 4.32 KB
/
inject-view.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright IBM Corp. 2018. 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 {
Binding,
BindingScope,
filterByTag,
Context,
ContextView,
Getter,
inject,
} from '../..';
describe('@inject.view', async () => {
let ctx: Context;
beforeEach(() => {
ctx = givenContext();
});
class MyControllerWithGetter {
@inject.view(filterByTag('foo'))
getter: Getter<string[]>;
}
class MyControllerWithValues {
constructor(
@inject.view(filterByTag('foo'))
public values: string[],
) {}
}
class MyControllerWithTracker {
@inject.view(filterByTag('foo'))
view: ContextView<string[]>;
}
it('reports error if the target type (Getter<string[]>) is not ContextView', async () => {
ctx.bind('my-controller').toClass(MyControllerWithGetter);
await expect(
ctx.get<MyControllerWithGetter>('my-controller'),
).to.be.rejectedWith(
'The type of MyControllerWithGetter.prototype.getter (Function) is not ContextView',
);
});
it('reports error if the target type (string[]) is not ContextView', async () => {
ctx.bind('my-controller').toClass(MyControllerWithValues);
await expect(
ctx.get<MyControllerWithValues>('my-controller'),
).to.be.rejectedWith(
'The type of MyControllerWithValues.constructor[0] (Array) is not ContextView',
);
});
it('injects as a view', async () => {
ctx.bind('my-controller').toClass(MyControllerWithTracker);
const inst = await ctx.get<MyControllerWithTracker>('my-controller');
expect(inst.view).to.be.instanceOf(ContextView);
expect(await inst.view.values()).to.eql(['BAR', 'FOO']);
// Add a new binding that matches the filter
ctx
.bind('xyz')
.to('XYZ')
.tag('foo');
// The view picks up the new binding
expect(await inst.view.values()).to.eql(['BAR', 'XYZ', 'FOO']);
});
});
describe('@inject with filter function', async () => {
let ctx: Context;
beforeEach(() => {
ctx = givenContext();
});
class MyControllerWithGetter {
@inject.getter(filterByTag('foo'))
getter: Getter<string[]>;
}
class MyControllerWithValues {
constructor(
@inject(filterByTag('foo'))
public values: string[],
) {}
}
class MyControllerWithView {
@inject(filterByTag('foo'))
view: ContextView<string[]>;
}
class MyControllerWithGetter2 {
@inject(filterByTag('foo'))
getter: Getter<string[]>;
}
it('injects as getter', async () => {
ctx.bind('my-controller').toClass(MyControllerWithGetter);
const inst = await ctx.get<MyControllerWithGetter>('my-controller');
const getter = inst.getter;
expect(getter).to.be.a.Function();
expect(await getter()).to.eql(['BAR', 'FOO']);
// Add a new binding that matches the filter
ctx
.bind('xyz')
.to('XYZ')
.tag('foo');
// The getter picks up the new binding
expect(await getter()).to.eql(['BAR', 'XYZ', 'FOO']);
});
it('injects as values', async () => {
ctx.bind('my-controller').toClass(MyControllerWithValues);
const inst = await ctx.get<MyControllerWithValues>('my-controller');
expect(inst.values).to.eql(['BAR', 'FOO']);
});
it('refuses to inject as a view', async () => {
ctx.bind('my-controller').toClass(MyControllerWithView);
await expect(
ctx.get<MyControllerWithView>('my-controller'),
).to.be.rejectedWith(
'The type of MyControllerWithView.prototype.view' +
' (ContextView) is not Array',
);
});
it('refuses to inject as a getter', async () => {
ctx.bind('my-controller').toClass(MyControllerWithGetter2);
await expect(
ctx.get<MyControllerWithGetter2>('my-controller'),
).to.be.rejectedWith(
'The type of MyControllerWithGetter2.prototype.getter' +
' (Function) is not Array',
);
});
});
function givenContext(bindings: Binding[] = []) {
const parent = new Context('app');
const ctx = new Context(parent, 'server');
bindings.push(
ctx
.bind('bar')
.toDynamicValue(() => Promise.resolve('BAR'))
.tag('foo', 'bar')
.inScope(BindingScope.SINGLETON),
);
bindings.push(
parent
.bind('foo')
.to('FOO')
.tag('foo', 'bar'),
);
return ctx;
}