-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
126 lines (102 loc) · 2.67 KB
/
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const waterpump = require('./index')
describe('waterpump', () => {
it('should inject a single module', () => {
const { register } = waterpump()
const iA = () => ({
f() {
return 'This is A.f'
}
})
const A = register('A', iA)
expect(A.f()).toEqual('This is A.f')
})
it('should inject two modules, where A depends on B', () => {
const { register } = waterpump()
const A = register('A', (ports) => ({
f() {
return ports.B.f()
}
}))
const B = register('B', (ports) => ({
f() {
return 'This is B.f'
}
}))
expect(A.f()).toEqual('This is B.f')
})
it('should expode when A depends on B but B was not injected', () => {
const { register } = waterpump()
const A = register('A', (ports) => ({
f() {
return ports.B.f()
}
}))
expect(() => {
A.f()
}).toThrowError(`Cannot read property 'f' of undefined`)
})
it('should inject two modules, where with circular dependency', () => {
const { register } = waterpump()
const A = register('A', (ports) => ({
f() {
return ports.B.f()
},
g() {
return 'This is A.g'
}
}))
register('B', (ports) => ({
f() {
return ports.A.g()
}
}))
expect(A.f()).toEqual('This is A.g')
})
it('should NOT be able to inject the same module twice', () => {
const { register } = waterpump()
register('A', () => ({}))
expect(() => {
register('A', () => ({}))
}).toThrowError('Module A already injected');
})
describe('mocks', () => {
it('should ignore any accessed path', () => {
const { mock: { ignore } } = waterpump()
expect(
ignore
.a
.b({ a: 234 })[123]
.stuff()
.lol
.oki()
.then('nevermind')
.that[0]
.will['work']
.well
.well
)
})
it('should forward values to next callback', () => {
const { mock: { callback } } = waterpump()
const mod = {
f: callback(null, 1, 2, 'joey')
}
mod.f('ignored', 'more_ignored', (err, one, two, joey) => {
expect(err).toBe(null)
expect(one).toBe(1)
expect(two).toBe(2)
expect(joey).toBe('joey')
})
})
it('should forward err to next callback', () => {
const { mock: { callback } } = waterpump()
const errorMessage = `${Math.floor(Math.random() * 1000)}`
const mod = {
f: callback(new Error(errorMessage))
}
mod.f('ignored', 'more_ignored', (err, ...values) => {
expect(err.message).toEqual(errorMessage)
})
})
})
})