-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
context-view.unit.ts
237 lines (206 loc) · 6.54 KB
/
context-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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// 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 {
Binding,
BindingScope,
BindingTag,
compareBindingsByTag,
Context,
ContextView,
createViewGetter,
filterByTag,
} from '../..';
describe('ContextView', () => {
let app: Context;
let server: ServerContext;
let bindings: Binding<unknown>[];
let taggedAsFoo: ContextView;
beforeEach(givenContextView);
it('tracks bindings', () => {
expect(taggedAsFoo.bindings).to.eql(bindings);
});
it('leverages findByTag for binding tag filter', () => {
expect(taggedAsFoo.bindings).to.eql(bindings);
expect(server.findByTagInvoked).to.be.true();
});
it('sorts matched bindings', () => {
const view = new ContextView(
server,
filterByTag('foo'),
compareBindingsByTag('phase', ['b', 'a']),
);
expect(view.bindings).to.eql([bindings[1], bindings[0]]);
});
it('resolves bindings', async () => {
expect(await taggedAsFoo.resolve()).to.eql(['BAR', 'FOO']);
expect(await taggedAsFoo.values()).to.eql(['BAR', 'FOO']);
});
it('resolves bindings as a getter', async () => {
expect(await taggedAsFoo.asGetter()()).to.eql(['BAR', 'FOO']);
});
it('reports error on singleValue() if multiple values exist', async () => {
return expect(taggedAsFoo.singleValue()).to.be.rejectedWith(
/The ContextView has more than one value\. Use values\(\) to access them\./,
);
});
it('supports singleValue() if only one value exist', async () => {
server.unbind('bar');
expect(await taggedAsFoo.singleValue()).to.eql('FOO');
});
it('reloads bindings after refresh', async () => {
taggedAsFoo.refresh();
const abcBinding = server
.bind('abc')
.to('ABC')
.tag('abc');
const xyzBinding = server
.bind('xyz')
.to('XYZ')
.tag('foo');
expect(taggedAsFoo.bindings).to.containEql(xyzBinding);
// `abc` does not have the matching tag
expect(taggedAsFoo.bindings).to.not.containEql(abcBinding);
expect(await taggedAsFoo.values()).to.eql(['BAR', 'XYZ', 'FOO']);
});
it('reloads bindings if context bindings are added', async () => {
const abcBinding = server
.bind('abc')
.to('ABC')
.tag('abc');
const xyzBinding = server
.bind('xyz')
.to('XYZ')
.tag('foo');
expect(taggedAsFoo.bindings).to.containEql(xyzBinding);
// `abc` does not have the matching tag
expect(taggedAsFoo.bindings).to.not.containEql(abcBinding);
expect(await taggedAsFoo.values()).to.eql(['BAR', 'XYZ', 'FOO']);
});
it('reloads bindings if context bindings are removed', async () => {
server.unbind('bar');
expect(await taggedAsFoo.values()).to.eql(['FOO']);
});
it('reloads bindings if context bindings are rebound', async () => {
server.bind('bar').to('BAR'); // No more tagged with `foo`
expect(await taggedAsFoo.values()).to.eql(['FOO']);
});
it('reloads bindings if parent context bindings are added', async () => {
const xyzBinding = app
.bind('xyz')
.to('XYZ')
.tag('foo');
expect(taggedAsFoo.bindings).to.containEql(xyzBinding);
expect(await taggedAsFoo.values()).to.eql(['BAR', 'FOO', 'XYZ']);
});
it('reloads bindings if parent context bindings are removed', async () => {
app.unbind('foo');
expect(await taggedAsFoo.values()).to.eql(['BAR']);
});
it('stops watching', async () => {
expect(await taggedAsFoo.values()).to.eql(['BAR', 'FOO']);
taggedAsFoo.close();
app.unbind('foo');
expect(await taggedAsFoo.values()).to.eql(['BAR', 'FOO']);
});
describe('EventEmitter', () => {
let events: string[] = [];
beforeEach(setupListeners);
it('emits close', () => {
taggedAsFoo.close();
expect(events).to.eql(['close']);
// 2nd close does not emit `close` as it's closed
taggedAsFoo.close();
expect(events).to.eql(['close']);
});
it('emits refresh', () => {
taggedAsFoo.refresh();
expect(events).to.eql(['refresh']);
});
it('emits resolve', async () => {
await taggedAsFoo.values();
expect(events).to.eql(['resolve']);
// Second call does not resolve as values are cached
await taggedAsFoo.values();
expect(events).to.eql(['resolve']);
});
it('emits refresh & resolve when bindings are changed', async () => {
server
.bind('xyz')
.to('XYZ')
.tag('foo');
await taggedAsFoo.values();
expect(events).to.eql(['refresh', 'resolve']);
});
function setupListeners() {
events = [];
['open', 'close', 'refresh', 'resolve'].forEach(t =>
taggedAsFoo.on(t, () => events.push(t)),
);
}
});
describe('createViewGetter', () => {
it('creates a getter function for the binding filter', async () => {
const getter = createViewGetter(server, filterByTag('foo'));
expect(await getter()).to.eql(['BAR', 'FOO']);
server
.bind('abc')
.to('ABC')
.tag('abc');
server
.bind('xyz')
.to('XYZ')
.tag('foo');
expect(await getter()).to.eql(['BAR', 'XYZ', 'FOO']);
});
it('creates a getter function for the binding filter and comparator', async () => {
const getter = createViewGetter(server, filterByTag('foo'), (a, b) => {
return a.key.localeCompare(b.key);
});
expect(await getter()).to.eql(['BAR', 'FOO']);
server
.bind('abc')
.to('ABC')
.tag('abc');
server
.bind('xyz')
.to('XYZ')
.tag('foo');
expect(await getter()).to.eql(['BAR', 'FOO', 'XYZ']);
});
});
function givenContextView() {
bindings = [];
givenContext();
taggedAsFoo = server.createView(filterByTag('foo'));
}
class ServerContext extends Context {
findByTagInvoked = false;
constructor(parent: Context, name: string) {
super(parent, name);
}
_findByTagIndex(tag: BindingTag) {
this.findByTagInvoked = true;
return super._findByTagIndex(tag);
}
}
function givenContext() {
app = new Context('app');
server = new ServerContext(app, 'server');
bindings.push(
server
.bind('bar')
.toDynamicValue(() => Promise.resolve('BAR'))
.tag('foo', 'bar', {phase: 'a'})
.inScope(BindingScope.SINGLETON),
);
bindings.push(
app
.bind('foo')
.to('FOO')
.tag('foo', 'bar', {phase: 'b'}),
);
}
});