/
context.ts
422 lines (379 loc) · 13 KB
/
context.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {expect} from '@loopback/testlab';
import {Context, Binding, BindingScope, BindingType, isPromise} from '../..';
describe('Context', () => {
let ctx: Context;
beforeEach('given a context', createContext);
describe('bind', () => {
it('adds a binding into the registry', () => {
ctx.bind('foo');
const result = ctx.contains('foo');
expect(result).to.be.true();
});
it('returns a binding', () => {
const binding = ctx.bind('foo');
expect(binding).to.be.instanceOf(Binding);
});
it('rejects a key containing property separator', () => {
const key = 'a' + Binding.PROPERTY_SEPARATOR + 'b';
expect(() => ctx.bind(key)).to.throw(/Binding key .* cannot contain/);
});
});
describe('contains', () => {
it('returns true when the key is the registry', () => {
ctx.bind('foo');
const result = ctx.contains('foo');
expect(result).to.be.true();
});
it('returns false when the key is not in the registry', () => {
const result = ctx.contains('bar');
expect(result).to.be.false();
});
});
describe('find', () => {
it('returns matching binding', () => {
const b1 = ctx.bind('foo');
ctx.bind('bar');
const result = ctx.find('foo');
expect(result).to.be.eql([b1]);
});
it('returns matching binding with *', () => {
const b1 = ctx.bind('foo');
const b2 = ctx.bind('bar');
const b3 = ctx.bind('baz');
let result = ctx.find('*');
expect(result).to.be.eql([b1, b2, b3]);
result = ctx.find('ba*');
expect(result).to.be.eql([b2, b3]);
});
});
describe('findByTag', () => {
it('returns matching binding', () => {
const b1 = ctx.bind('foo').tag('t1');
ctx.bind('bar').tag('t2');
const result = ctx.findByTag('t1');
expect(result).to.be.eql([b1]);
});
it('returns matching binding with *', () => {
const b1 = ctx.bind('foo').tag('t1');
const b2 = ctx.bind('bar').tag('t2');
const result = ctx.findByTag('t*');
expect(result).to.be.eql([b1, b2]);
});
});
describe('getBinding', () => {
it('returns the binding object registered under the given key', () => {
const expected = ctx.bind('foo');
const actual = ctx.getBinding('foo');
expect(actual).to.equal(expected);
});
it('reports an error when binding was not found', () => {
expect(() => ctx.getBinding('unknown-key')).to.throw(/unknown-key/);
});
it('rejects a key containing property separator', () => {
const key = 'a' + Binding.PROPERTY_SEPARATOR + 'b';
expect(() => ctx.getBinding(key)).to.throw(
/Binding key .* cannot contain/,
);
});
});
describe('getSync', () => {
it('returns the value immediately when the binding is sync', () => {
ctx.bind('foo').to('bar');
const result = ctx.getSync('foo');
expect(result).to.equal('bar');
});
it('returns the value with property separator', () => {
const SEP = Binding.PROPERTY_SEPARATOR;
const val = {x: {y: 'Y'}};
ctx.bind('foo').to(val);
const value = ctx.getSync(`foo${SEP}x`);
expect(value).to.eql({y: 'Y'});
});
it('throws a helpful error when the binding is async', () => {
ctx.bind('foo').toDynamicValue(() => Promise.resolve('bar'));
expect(() => ctx.getSync('foo')).to.throw(/foo.*the value is a promise/);
});
it('returns singleton value', () => {
let count = 0;
ctx
.bind('foo')
.toDynamicValue(() => count++)
.inScope(BindingScope.SINGLETON);
let result = ctx.getSync('foo');
expect(result).to.equal(0);
result = ctx.getSync('foo');
expect(result).to.equal(0);
const childCtx = new Context(ctx);
result = childCtx.getSync('foo');
expect(result).to.equal(0);
});
it('returns singleton value triggered by the child context', () => {
let count = 0;
ctx
.bind('foo')
.toDynamicValue(() => count++)
.inScope(BindingScope.SINGLETON);
const childCtx = new Context(ctx);
// Calculate the singleton value at child level 1st
let result = childCtx.getSync('foo');
expect(result).to.equal(0);
// Try twice from the parent ctx
result = ctx.getSync('foo');
expect(result).to.equal(0);
result = ctx.getSync('foo');
expect(result).to.equal(0);
// Try again from the child ctx
result = childCtx.getSync('foo');
expect(result).to.equal(0);
});
it('returns transient value', () => {
let count = 0;
ctx
.bind('foo')
.toDynamicValue(() => count++)
.inScope(BindingScope.TRANSIENT);
let result = ctx.getSync('foo');
expect(result).to.equal(0);
result = ctx.getSync('foo');
expect(result).to.equal(1);
const childCtx = new Context(ctx);
result = childCtx.getSync('foo');
expect(result).to.equal(2);
});
it('returns context value', () => {
let count = 0;
ctx
.bind('foo')
.toDynamicValue(() => count++)
.inScope(BindingScope.CONTEXT);
let result = ctx.getSync('foo');
expect(result).to.equal(0);
result = ctx.getSync('foo');
expect(result).to.equal(0);
});
it('returns context value from a child context', () => {
let count = 0;
ctx
.bind('foo')
.toDynamicValue(() => count++)
.inScope(BindingScope.CONTEXT);
let result = ctx.getSync('foo');
expect(result).to.equal(0);
const childCtx = new Context(ctx);
result = childCtx.getSync('foo');
expect(result).to.equal(1);
result = childCtx.getSync('foo');
expect(result).to.equal(1);
const childCtx2 = new Context(ctx);
result = childCtx2.getSync('foo');
expect(result).to.equal(2);
result = childCtx.getSync('foo');
expect(result).to.equal(1);
});
});
describe('get', () => {
it('returns a promise when the binding is async', async () => {
ctx.bind('foo').to(Promise.resolve('bar'));
const result = await ctx.get('foo');
expect(result).to.equal('bar');
});
it('returns the value with property separator', async () => {
const SEP = Binding.PROPERTY_SEPARATOR;
const val = {x: {y: 'Y'}};
ctx.bind('foo').to(Promise.resolve(val));
const value = await ctx.get(`foo${SEP}x`);
expect(value).to.eql({y: 'Y'});
});
it('returns singleton value', async () => {
let count = 0;
ctx
.bind('foo')
.toDynamicValue(() => Promise.resolve(count++))
.inScope(BindingScope.SINGLETON);
let result = await ctx.get('foo');
expect(result).to.equal(0);
result = await ctx.get('foo');
expect(result).to.equal(0);
const childCtx = new Context(ctx);
result = await childCtx.get('foo');
expect(result).to.equal(0);
});
it('returns context value', async () => {
let count = 0;
ctx
.bind('foo')
.toDynamicValue(() => Promise.resolve(count++))
.inScope(BindingScope.CONTEXT);
let result = await ctx.get('foo');
expect(result).to.equal(0);
result = await ctx.get('foo');
expect(result).to.equal(0);
});
it('returns context value from a child context', async () => {
let count = 0;
ctx
.bind('foo')
.toDynamicValue(() => Promise.resolve(count++))
.inScope(BindingScope.CONTEXT);
let result = await ctx.get('foo');
const childCtx = new Context(ctx);
expect(result).to.equal(0);
result = await childCtx.get('foo');
expect(result).to.equal(1);
result = await childCtx.get('foo');
expect(result).to.equal(1);
const childCtx2 = new Context(ctx);
result = await childCtx2.get('foo');
expect(result).to.equal(2);
result = await childCtx.get('foo');
expect(result).to.equal(1);
});
it('returns transient value', async () => {
let count = 0;
ctx
.bind('foo')
.toDynamicValue(() => Promise.resolve(count++))
.inScope(BindingScope.TRANSIENT);
let result = await ctx.get('foo');
expect(result).to.equal(0);
result = await ctx.get('foo');
expect(result).to.equal(1);
const childCtx = new Context(ctx);
result = await childCtx.get('foo');
expect(result).to.equal(2);
});
});
describe('getValueOrPromise', () => {
it('returns synchronously for constant values', () => {
ctx.bind('key').to('value');
const valueOrPromise = ctx.getValueOrPromise('key');
expect(valueOrPromise).to.equal('value');
});
it('returns promise for async values', async () => {
ctx.bind('key').toDynamicValue(() => Promise.resolve('value'));
const valueOrPromise = ctx.getValueOrPromise('key');
expect(isPromise(valueOrPromise)).to.be.true();
const value = await valueOrPromise;
expect(value).to.equal('value');
});
it('returns nested property (synchronously)', () => {
ctx.bind('key').to({test: 'test-value'});
const value = ctx.getValueOrPromise('key#test');
expect(value).to.equal('test-value');
});
it('returns nested property (asynchronously)', async () => {
ctx.bind('key').to(Promise.resolve({test: 'test-value'}));
const value = await ctx.getValueOrPromise('key#test');
expect(value).to.equal('test-value');
});
it('supports deeply nested property path', () => {
ctx.bind('key').to({x: {y: 'z'}});
const value = ctx.getValueOrPromise('key#x.y');
expect(value).to.equal('z');
});
it('returns undefined when nested property does not exist', () => {
ctx.bind('key').to({test: 'test-value'});
const value = ctx.getValueOrPromise('key#x.y');
expect(value).to.equal(undefined);
});
it('honours TRANSIENT scope when retrieving a nested property', () => {
const state = {count: 0};
ctx
.bind('state')
.toDynamicValue(() => {
state.count++;
return state;
})
.inScope(BindingScope.TRANSIENT);
// verify the initial state & populate the cache
expect(ctx.getSync('state')).to.deepEqual({count: 1});
// retrieve a nested property (expect a new value)
expect(ctx.getSync('state#count')).to.equal(2);
// retrieve the full object again (expect another new value)
expect(ctx.getSync('state')).to.deepEqual({count: 3});
});
it('honours CONTEXT scope when retrieving a nested property', () => {
const state = {count: 0};
ctx
.bind('state')
.toDynamicValue(() => {
state.count++;
return state;
})
.inScope(BindingScope.CONTEXT);
// verify the initial state & populate the cache
expect(ctx.getSync('state')).to.deepEqual({count: 1});
// retrieve a nested property (expect the cached value)
expect(ctx.getSync('state#count')).to.equal(1);
// retrieve the full object again (verify that cache was not modified)
expect(ctx.getSync('state')).to.deepEqual({count: 1});
});
it('honours SINGLETON scope when retrieving a nested property', () => {
const state = {count: 0};
ctx
.bind('state')
.toDynamicValue(() => {
state.count++;
return state;
})
.inScope(BindingScope.SINGLETON);
// verify the initial state & populate the cache
expect(ctx.getSync('state')).to.deepEqual({count: 1});
// retrieve a nested property from a child context
const childContext1 = new Context(ctx);
expect(childContext1.getValueOrPromise('state#count')).to.equal(1);
// retrieve a nested property from another child context
const childContext2 = new Context(ctx);
expect(childContext2.getValueOrPromise('state#count')).to.equal(1);
// retrieve the full object again (verify that cache was not modified)
expect(ctx.getSync('state')).to.deepEqual({count: 1});
});
});
describe('toJSON()', () => {
it('converts to plain JSON object', () => {
ctx
.bind('a')
.to('1')
.lock();
ctx
.bind('b')
.toDynamicValue(() => 2)
.inScope(BindingScope.SINGLETON)
.tag(['X', 'Y']);
ctx
.bind('c')
.to(3)
.tag('Z');
expect(ctx.toJSON()).to.eql({
a: {
key: 'a',
scope: BindingScope.TRANSIENT,
tags: [],
isLocked: true,
type: BindingType.CONSTANT,
},
b: {
key: 'b',
scope: BindingScope.SINGLETON,
tags: ['X', 'Y'],
isLocked: false,
type: BindingType.DYNAMIC_VALUE,
},
c: {
key: 'c',
scope: BindingScope.TRANSIENT,
tags: ['Z'],
isLocked: false,
type: BindingType.CONSTANT,
},
});
});
});
function createContext() {
ctx = new Context();
}
});