-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
240 lines (206 loc) · 6.08 KB
/
index.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
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
/* global define */
(function (global, module) { // Browser compatibility
var simple = module.exports
var mocks = []
var totalCalls = 0
/**
* Restore the current simple and create a new one
*
* @param {Object} [obj]
* @param {String} [key]
* @api public
*/
simple.restore = function (obj, key) {
if (obj && key) {
mocks.some(function (mock, i) {
if (mock.obj !== obj || mock.key !== key) return
mock.restore()
mocks.splice(i, 1)
return true
})
return
}
mocks.forEach(_restoreMock)
mocks = []
}
/**
* Create a mocked value on an object
*
* @param {Object} obj
* @param {String} key
* @param {Function|*} [mockValue]
* @return {Function} mock
* @api public
*/
simple.mock = function (obj, key, mockValue) {
if (!arguments.length) {
return simple.spyOrStub()
} else if (arguments.length === 1) {
return simple.spyOrStub(obj)
} else if (isFunction(mockValue)) {
mockValue = simple.spyOrStub(mockValue)
} else if (arguments.length === 2) {
mockValue = simple.spyOrStub(obj, key)
}
var mock = {
obj: obj,
key: key,
mockValue: mockValue,
oldValue: obj[key],
oldValueHasKey: (key in obj)
}
mock.restore = _restoreMock.bind(null, mock)
mocks.unshift(mock)
obj[key] = mockValue
return mockValue
}
/**
* Create a stub function
*
* @param {Function|Object} wrappedFn
* @param {String} [key]
* @return {Function} spy
* @api public
*/
simple.spyOrStub = simple.stub = simple.spy = function (wrappedFn, key) {
if (arguments.length === 2) {
wrappedFn = wrappedFn[key]
}
var originalFn = wrappedFn
var stubFn = function () {
var action = (newFn.loop) ? newFn.actions[(newFn.callCount - 1) % newFn.actions.length] : newFn.actions.shift()
if (!action) return
if (action.throwError) throw action.throwError
if ('returnValue' in action) return action.returnValue
if ('fn' in action) return action.fn.apply(action.ctx || this, arguments)
var cb = ('cbArgIndex' in action) ? arguments[action.cbArgIndex] : arguments[arguments.length - 1]
if (action.cbArgs) return cb.apply(action.ctx || null, action.cbArgs)
}
var newFn = function () {
var call = {
k: totalCalls++, // Keep count of calls to record the absolute order of calls
args: Array.prototype.slice.call(arguments, 0),
arg: arguments[0],
context: this
}
newFn.calls.push(call)
newFn.firstCall = newFn.firstCall || call
newFn.lastCall = call
newFn.callCount++
newFn.called = true
try {
call.returned = (wrappedFn || _noop).apply(this, arguments)
} catch(e) {
call.threw = e
throw e
}
return call.returned
}
// Spy
newFn.reset = function () {
newFn.calls = []
newFn.lastCall = { args: [] } // For dot-safety
newFn.callCount = 0
newFn.called = false
}
newFn.reset()
// Stub
newFn.actions = []
newFn.loop = true
newFn.callbackWith = newFn.callback = function () {
wrappedFn = stubFn
newFn.actions.push({ cbArgs: arguments })
return newFn // Chainable
}
newFn.callbackArgWith = newFn.callbackAtIndex = function () {
wrappedFn = stubFn
newFn.actions.push({
cbArgs: Array.prototype.slice.call(arguments, 1),
cbArgIndex: arguments[0]
})
return newFn // Chainable
}
newFn.inThisContext = function (obj) {
var action = newFn.actions[newFn.actions.length - 1]
action.ctx = obj
return newFn // Chainable
}
newFn.withArgs = function () {
var action = newFn.actions[newFn.actions.length - 1]
action.cbArgs = arguments
return newFn // Chainable
}
newFn.returnWith = function (returnValue) {
wrappedFn = stubFn
newFn.actions.push({ returnValue: returnValue })
return newFn // Chainable
}
newFn.throwWith = function (err) {
wrappedFn = stubFn
newFn.actions.push({ throwError: err })
return newFn // Chainable
}
newFn.resolveWith = function (value) {
if (simple.Promise.when) return newFn.callFn(function createResolvedPromise () { return simple.Promise.when(value) })
return newFn.callFn(function createResolvedPromise () { return simple.Promise.resolve(value) })
}
newFn.rejectWith = function (value) {
return newFn.callFn(function createRejectedPromise () { return simple.Promise.reject(value) })
}
newFn.callFn = function (fn) {
wrappedFn = stubFn
newFn.actions.push({ fn: fn })
return newFn // Chainable
}
newFn.withActions = function (actions) {
wrappedFn = stubFn
if (actions && actions.length >= 0) {
Array.prototype.push.apply(newFn.actions, actions)
}
return newFn // Chainable
}
newFn.callOriginal = newFn.callOriginalFn = function () {
wrappedFn = stubFn
newFn.actions.push({ fn: originalFn })
return newFn // Chainable
}
newFn.withLoop = function () {
newFn.loop = true
return newFn // Chainable
}
newFn.noLoop = function () {
newFn.loop = false
return newFn // Chainable
}
return newFn
}
function _restoreMock (mock) {
if (!mock.oldValueHasKey) {
delete mock.obj[mock.key]
return
}
mock.obj[mock.key] = mock.oldValue
}
function _noop () {}
/**
* Return whether the passed variable is a function
*
* @param {Mixed} value
* @return {Boolean}
* @api private
*/
function isFunction (value) {
return Object.prototype.toString.call(value) === funcClass
}
var funcClass = '[object Function]'
// Browser compatibility
if (typeof define === 'function' && define.amd) {
define(function () {
return simple
})
} else if (typeof window !== 'undefined') {
window.simple = simple
}
// Native Promise support
if (typeof Promise !== 'undefined') simple.Promise = Promise
})(this, typeof module !== 'undefined' ? module : {exports: {}})