-
Notifications
You must be signed in to change notification settings - Fork 1
/
di.test.js
110 lines (87 loc) · 2.37 KB
/
di.test.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
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
const test = require('ava');
const sinon = require('sinon');
const {
DependencyInjector,
PlanLintError
} = require('./di');
const spy = () => sinon.spy(() => ({}));
test.beforeEach(t => {
t.context.di = new DependencyInjector();
});
test('totally missing', t => {
const {di} = t.context;
t.throws(() => di.require('a'), PlanLintError);
});
test('missing', t => {
const {di} = t.context;
di.define('a', ['b'], () => {});
t.throws(() => di.require('a'), PlanLintError);
});
test('missing circular', t => {
const {di} = t.context;
di.define('a', ['a'], () => {});
t.throws(() => di.require('a'));
// TODO
// t.throws(() => di.require('a'), PlanLintError);
});
test('ok', t => {
const {di} = t.context;
const af = spy();
di.define('a', af);
const a = di.require('a');
t.truthy(af.calledOnce);
t.truthy(af.lastCall.calledWith());
t.is(a, af.lastCall.returnValue);
});
test('ok 2', t => {
const {di} = t.context;
const af = spy();
const bf = spy();
di.define('a', ['b'], af);
di.define('b', bf);
const a = di.require('a');
t.truthy(bf.calledOnce);
t.truthy(bf.lastCall.calledWith());
t.truthy(af.calledOnce);
t.truthy(af.lastCall.calledWith(bf.lastCall.returnValue));
t.is(a, af.lastCall.returnValue);
});
test('ok circular', t => {
const {di} = t.context;
const af = spy();
const b1f = spy();
const b2f = spy();
di.define('a', ['b'], af);
di.define('b', ['b'], b1f);
di.define('b', b2f);
const a = di.require('a');
t.truthy(b2f.calledOnce);
t.truthy(b2f.lastCall.calledWith());
t.truthy(b1f.calledOnce);
t.truthy(b1f.lastCall.calledWith(b2f.lastCall.returnValue));
t.truthy(af.calledOnce);
t.truthy(af.lastCall.calledWith(b1f.lastCall.returnValue));
t.is(a, af.lastCall.returnValue);
});
test('ok circular greedy', t => {
const {di} = t.context;
const a1f = spy();
const a2f = spy();
const a3f = spy();
di.define('a', ['a'], a1f);
di.define('a', ['a'], a2f);
di.define('a', a3f);
const a = di.require('a');
t.truthy([
a1f.lastCall.returnValue,
a2f.lastCall.returnValue
].includes(a));
t.truthy(a1f.calledOnce);
t.truthy(a1f.lastCall.calledWith(a3f.lastCall.returnValue) ||
a1f.lastCall.calledWith(a2f.lastCall.returnValue));
t.truthy(a2f.calledOnce);
t.truthy(a2f.lastCall.calledWith(a3f.lastCall.returnValue) ||
a2f.lastCall.calledWith(a1f.lastCall.returnValue));
t.truthy(a3f.calledOnce);
t.truthy(a3f.lastCall.calledWith());
});