/
context-view.unit.ts
116 lines (100 loc) · 3.35 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
// 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} from '../..';
describe('ContextView', () => {
let app: Context;
let server: Context;
let bindings: Binding<unknown>[];
let contextView: ContextView;
beforeEach(givenContextView);
it('tracks bindings', () => {
expect(contextView.bindings).to.eql(bindings);
});
it('resolves bindings', async () => {
expect(await contextView.resolve()).to.eql(['BAR', 'FOO']);
expect(await contextView.values()).to.eql(['BAR', 'FOO']);
});
it('resolves bindings as a getter', async () => {
expect(await contextView.asGetter()()).to.eql(['BAR', 'FOO']);
});
it('reloads bindings after refresh', async () => {
contextView.refresh();
const abcBinding = server
.bind('abc')
.to('ABC')
.tag('abc');
const xyzBinding = server
.bind('xyz')
.to('XYZ')
.tag('foo');
expect(contextView.bindings).to.containEql(xyzBinding);
// `abc` does not have the matching tag
expect(contextView.bindings).to.not.containEql(abcBinding);
expect(await contextView.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(contextView.bindings).to.containEql(xyzBinding);
// `abc` does not have the matching tag
expect(contextView.bindings).to.not.containEql(abcBinding);
expect(await contextView.values()).to.eql(['BAR', 'XYZ', 'FOO']);
});
it('reloads bindings if context bindings are removed', async () => {
server.unbind('bar');
expect(await contextView.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 contextView.values()).to.eql(['FOO']);
});
it('reloads bindings if parent context bindings are added', async () => {
const xyzBinding = app
.bind('xyz')
.to('XYZ')
.tag('foo');
expect(contextView.bindings).to.containEql(xyzBinding);
expect(await contextView.values()).to.eql(['BAR', 'FOO', 'XYZ']);
});
it('reloads bindings if parent context bindings are removed', async () => {
app.unbind('foo');
expect(await contextView.values()).to.eql(['BAR']);
});
it('stops watching', async () => {
expect(await contextView.values()).to.eql(['BAR', 'FOO']);
contextView.close();
app.unbind('foo');
expect(await contextView.values()).to.eql(['BAR', 'FOO']);
});
function givenContextView() {
bindings = [];
givenContext();
contextView = server.createView(filterByTag('foo'));
}
function givenContext() {
app = new Context('app');
server = new Context(app, 'server');
bindings.push(
server
.bind('bar')
.toDynamicValue(() => Promise.resolve('BAR'))
.tag('foo', 'bar')
.inScope(BindingScope.SINGLETON),
);
bindings.push(
app
.bind('foo')
.to('FOO')
.tag('foo', 'bar'),
);
}
});