forked from EndangeredMassa/bond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.coffee
246 lines (191 loc) · 6.29 KB
/
test.coffee
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
{ok:expect, equal} = require 'assert'
bond = require './lib/bond'
describe 'bond', ->
math =
PI: Math.PI
abs: Math.abs
add: (a, b) ->
a + b
subtract: (a, b) ->
a - b
ComplexNumber: (@real, @imaginary) ->
this.toString = ->
"#{@real} + #{@imaginary}i"
NumberObject: (number) ->
return {number}
describe '#bond', ->
describe 'when called with 0 args', ->
it 'returns a simple spy', ->
spy = bond()
expect !spy.called
spy()
expect spy.called
it 'returns a spy that can have a return value', ->
spy = bond().return(3)
result = spy()
equal result, 3
it 'returns the bond api when called with 2 args', ->
api = bond(math, 'add')
expect api.to
expect api.return
expect api.through
expect api.restore
describe 'to', ->
describe 'non function values', ->
it 'replaces values', ->
bond(math, 'PI').to 3.14
equal math.PI, 3.14
it 'returns to original value', ->
equal math.PI, 3.141592653589793
describe 'function values', ->
it 'creates a through spy', ->
bond(math, 'subtract').to (x, y) -> math.abs(x - y)
result = math.subtract(5, 10)
equal result, 5
equal math.subtract.called, 1
expect math.subtract.calledWith(5, 10)
it 'returns the original values', ->
result = math.subtract(5, 10)
equal result, -5
it 'explicitly returns objects from constructors', ->
bond(math, 'NumberObject').to -> {number: 7}
result = new math.NumberObject()
equal result.number, 7
it "doesn't return non-objects from constructors", ->
bond(math, 'NumberObject').to ->
@numero = 42
return 'I should not be returned'
result = new math.NumberObject()
equal result.numero, 42
describe 'return', ->
it 'replaces methods', ->
bond(math, 'add').return(888)
result = math.add()
equal result, 888
it 'returns to original value', ->
result = math.add(1, 2)
equal result, 3
describe 'asyncReturn', ->
module =
useNodeCallback: (value1, value2, callback) ->
throw new Error
dontUseNodeCallback: (value1, value2) ->
throw new Error
it 'calls the last argument with the provided arguments', (done) ->
ignoredValue1 = 5
ignoredValue2 = 4
stubValue1 = 3
stubValue2 = 2
bond(module, 'useNodeCallback').asyncReturn(null, stubValue1, stubValue2)
module.useNodeCallback ignoredValue1, ignoredValue2, (error, value1, value2) ->
expect !error
equal value1, stubValue1
equal value2, stubValue2
done()
it 'calls the callback with an error', (done) ->
ignoredValue = 5
stubError = 1
bond(module, 'useNodeCallback').asyncReturn(stubError)
module.useNodeCallback ignoredValue, (error) ->
equal error, stubError
done()
it 'throws an error if the last argument is not a function', ->
ignoredValue = 5
stubValue = 2
error = null
bond(module, 'dontUseNodeCallback').asyncReturn(stubValue)
try
module.useNodeCallback ignoredValue
catch err
error = err
expect error
it 'calls the callback on the next tick', (done) ->
ignoredValue = 5
stubValue = 3
module2 =
work: ->
done()
callbackSpy = bond(module2, 'work').through()
bond(module, 'useNodeCallback').asyncReturn(stubValue)
module.useNodeCallback ignoredValue, callbackSpy
# this will fail if the callback is called immediately
expect !callbackSpy.called
describe 'through', ->
it 'calls original method', ->
bond(math, 'add').through()
result = math.add(1, 2)
equal result, 3
it 'explicitly returns objects from constructors', ->
bond(math, 'NumberObject').through()
result = new math.NumberObject(42)
equal result.number, 42
describe 'restore', ->
it 'restores the original property', ->
original = math.add
bond(math,'add').through()
expect original != math.add
math.add.restore()
expect original == math.add
describe 'spies with `through` and `return`', ->
it 'returns the bond api mixed into the returned spy', ->
for method in ['through', 'return']
bond(math, 'add')[method]()
expect math.add.to
expect math.add.return
expect math.add.through
expect math.add.restore
it 'allows the spy to be replaced with new spies via the mixed-in api', ->
bond(math,'add').return(123)
result = math.add(1, 2)
equal result, 123
math.add.return(321)
result = math.add(1, 2)
equal result, 321
math.add.through()
result = math.add(1, 2)
equal result, 3
math.add.return(123)
result = math.add(1, 2)
equal result, 123
it 'records call count via called', ->
bond(math, 'add').return(777)
equal math.add.called, 0
math.add(1, 2)
equal math.add.called, 1
math.add(1, 2)
equal math.add.called, 2
it 'records called via called', ->
bond(math, 'add').through()
expect !math.add.called
math.add(1, 2)
expect math.add.called
it 'responds to calledWith(args...)', ->
bond(math, 'add').return(666)
expect !math.add.calledWith(11, 22)
math.add(11, 22)
expect math.add.calledWith(11, 22)
it 'exposes argsForCall', ->
bond(math, 'add').return(555)
math.add(111, 222)
math.add(333, 444)
equal math.add.calledArgs[0][0], 111
equal math.add.calledArgs[0][1], 222
equal math.add.calledArgs[1][0], 333
equal math.add.calledArgs[1][1], 444
it 'returns the spy', ->
spy = bond(math, 'add').through()
math.add(1, 2)
expect spy.called
it 'through is constructor safe', ->
bond(math, 'ComplexNumber').through()
result = new math.ComplexNumber(3, 4)
equal result.real, 3
equal result.imaginary, 4
expect math.ComplexNumber.called
it 'return is constructor safe', ->
number =
real: 1
imaginary: 2
bond(math, 'ComplexNumber').return(number)
result = new math.ComplexNumber(3, 4)
equal result, number