-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.js
84 lines (74 loc) · 2.47 KB
/
index.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
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
const expect = require('chai').expect;
const injector = require('injectdeps');
const loadConfig = require('./index');
const config = require('config');
const defaultDatabase = injector(['app.db.host', 'app.db.port', 'app.db.debug'], function(host, port, debug){
return `${host}:${port}:${debug}`;
});
const differentRootDatabase = injector(['db.host', 'db.port'], function(host, port){
return `${host}:${port}`;
});
const prefixedDatabase = injector(['cfg.db.host', 'cfg.db.port'], function(host, port){
return `${host}:${port}`;
});
const arrayTestDatabase = injector(['cfg.db.seeds', 'cfg.db.port'], function(seeds, port){
return seeds.map(s=>s+':'+port).join(',');;
});
const objectTestDatabase = injector(['app.db'], function(cfg){
return JSON.stringify(cfg);
});
describe('injectdeps-config', () => {
it("should load properties with the default settings", () => {
const container = injector.getContainer();
const configLoader = loadConfig(config, {});
const db = configLoader(container)
.bindName('db').toObject(defaultDatabase)
.newObject('db');
expect(db).to.equal("localhost:1234:true");
});
it("should load properties with a different root", () => {
const settings = {
root: 'app'
};
const db = loadConfig(config, settings)()
.bindName('db').toObject(differentRootDatabase)
.newObject('db');
expect(db).to.equal("localhost:1234");
});
it("should load properties with a custom prefix binding", () => {
const settings = {
root: 'app',
prefix: 'cfg'
};
const db = loadConfig(config, settings)()
.bindName('db').toObject(prefixedDatabase)
.newObject('db');
expect(db).to.equal("localhost:1234");
});
it("should load arrays", () => {
const settings = {
root: 'app',
prefix: 'cfg',
log: true,
typeHints: {
'cfg.db.seeds': 'string'
}
};
const db = loadConfig(config, settings)()
.bindName('db').toObject(arrayTestDatabase)
.newObject('db');
console.log(settings.logs.join('\n'));
expect(db).to.equal("8.8.8.8:1234,8.8.4.4:1234");
});
it("should load entire objects", () => {
const settings = {
objects: true,
log: true
};
const db = loadConfig(config, settings)()
.bindName('db').toObject(objectTestDatabase)
.newObject('db');
console.log(settings.logs.join('\n'));
expect(db).to.equal('{"host":"localhost","port":1234,"seeds":["8.8.8.8","8.8.4.4"],"debug":true}');
});
});