This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinjectdeps.spec.js
59 lines (46 loc) · 1.92 KB
/
injectdeps.spec.js
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
const expect = require('chai').expect;
describe('injectdeps', () => {
it('should inject scalar values', () => {
const container = require('../injectdeps').getContainer()
.bindName('text').toScalarValue('abc')
.bindName('num').toScalarValue(123.45)
.bindName('bol').toScalarValue(true);
expect(container.newObject('text')).to.be.a('string')
.that.equals('abc');
expect(container.newObject('num')).to.be.a('number')
.that.equals(123.45);
expect(container.newObject('bol')).to.be.a('boolean')
.that.equals(true);
});
it('should inject basic properties', () => {
const app = require('../injectdeps').getContainer()
.bindName('db').toObject(require('./test/db'))
.bindName('logger').toObject(require('./test/logger'))
.bindName('app').toObject(require('./test/module'))
.bindName('text').toScalarValue('hello world')
.bindName('arr').toPlainObject([1,2,3])
.newObject('app').args;
expect(app['0']).to.be.an('object')
.that.has.property('hello')
.that.equals('hello world');
expect(app['1']).to.be.an('array').with.lengthOf(3);
expect(app['2']).to.be.a('promise');
});
it('should reuse objects bound in singleton scope', (done) => {
const singletonHost = require('../injectdeps')(['container'], function(container){
const obj1 = container.getObject('obj');
const obj2 = container.getObject('obj');
expect(obj1).to.equal(obj2);
done();
});
const container = require('../injectdeps').getContainer()
.bindName('Math').toPlainObject(Math)
.bindName('obj').toObject(require('./test/randomizer'))
.bindName('container').toContainer()
.bindName('singletonHost').toObject(singletonHost);
const obj1 = container.newObject('obj');
const obj2 = container.newObject('obj');
expect(obj1).to.not.equal(obj2);
container.newObject('singletonHost');
});
});