-
Notifications
You must be signed in to change notification settings - Fork 1
/
integration.spec.ts
74 lines (60 loc) · 1.75 KB
/
integration.spec.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
import 'reflect-metadata'
import { Container, inject, injectable } from 'inversify'
import { Log, LogClass, setConfig } from './index'
describe('integration', () => {
// https://github.com/aigoncharov/class-logger/issues/6
describe('inversify', () => {
it('', () => {
const logFn = jest.fn()
setConfig({
include: {
construct: true,
},
log: logFn,
})
const TYPES = {
AI: Symbol.for('AI'),
CPU: Symbol.for('CPU'),
}
@LogClass()
@injectable()
class CPU {
private readonly res = 42
@Log()
public calc() {
return this.res
}
}
@LogClass()
@injectable()
class AI {
constructor(@inject(TYPES.CPU) private readonly _cpu: CPU) {}
@Log()
public takeOverTheWorld() {
return this._cpu.calc() * 2
}
}
const myContainer = new Container()
myContainer.bind(TYPES.CPU).to(CPU)
myContainer.bind(TYPES.AI).to(AI)
const cpu = myContainer.get<CPU>(TYPES.CPU)
expect(cpu).toBeInstanceOf(CPU)
const ai = myContainer.get<AI>(TYPES.AI)
expect(ai).toBeInstanceOf(AI)
const res = ai.takeOverTheWorld()
expect(res).toBe(84)
expect(logFn).toBeCalledTimes(7)
expect(logFn.mock.calls).toEqual([
// Getting CPU from the container explicitly
['CPU.construct. Args: [].'],
// Injecting CPU into AI
['CPU.construct. Args: [].'],
['AI.construct. Args: [CPU {"res":42}].'],
['AI.takeOverTheWorld. Args: [].'],
['CPU.calc. Args: [].'],
['CPU.calc -> done. Args: []. Res: 42.'],
['AI.takeOverTheWorld -> done. Args: []. Res: 84.'],
])
})
})
})