|
| 1 | +/* global define */ |
| 2 | +(function (global, module) { // Browser compatibility |
| 3 | + var simple = module.exports |
| 4 | + var mocks = [] |
| 5 | + var totalCalls = 0 |
| 6 | + |
| 7 | + /** |
| 8 | + * Restore the current simple and create a new one |
| 9 | + * |
| 10 | + * @param {Object} [obj] |
| 11 | + * @param {String} [key] |
| 12 | + * @api public |
| 13 | + */ |
| 14 | + simple.restore = function (obj, key) { |
| 15 | + if (obj && key) { |
| 16 | + mocks.some(function (mock, i) { |
| 17 | + if (mock.obj !== obj || mock.key !== key) return |
| 18 | + |
| 19 | + mock.restore() |
| 20 | + mocks.splice(i, 1) |
| 21 | + return true |
| 22 | + }) |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + mocks.forEach(_restoreMock) |
| 27 | + mocks = [] |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Create a mocked value on an object |
| 32 | + * |
| 33 | + * @param {Object} obj |
| 34 | + * @param {String} key |
| 35 | + * @param {Function|Value} mockValue |
| 36 | + * @return {Function} mock |
| 37 | + * @api public |
| 38 | + */ |
| 39 | + simple.mock = function (obj, key, mockValue) { |
| 40 | + if (!arguments.length) { |
| 41 | + return simple.spyOrStub() |
| 42 | + } else if (arguments.length === 1) { |
| 43 | + return simple.spyOrStub(obj) |
| 44 | + } else if (isFunction(mockValue)) { |
| 45 | + mockValue = simple.spyOrStub(mockValue) |
| 46 | + } else if (arguments.length === 2) { |
| 47 | + mockValue = simple.spyOrStub(obj, key) |
| 48 | + } |
| 49 | + |
| 50 | + var mock = { |
| 51 | + obj: obj, |
| 52 | + key: key, |
| 53 | + mockValue: mockValue, |
| 54 | + oldValue: obj[key], |
| 55 | + oldValueHasKey: (key in obj) |
| 56 | + } |
| 57 | + |
| 58 | + mock.restore = _restoreMock.bind(null, mock) |
| 59 | + mocks.unshift(mock) |
| 60 | + |
| 61 | + obj[key] = mockValue |
| 62 | + return mockValue |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Create a stub function |
| 67 | + * |
| 68 | + * @param {Function|Object} wrappedFn |
| 69 | + * @param {String} [key] |
| 70 | + * @return {Function} spy |
| 71 | + * @api public |
| 72 | + */ |
| 73 | + simple.spyOrStub = simple.stub = simple.spy = function (wrappedFn, key) { |
| 74 | + if (arguments.length === 2) { |
| 75 | + wrappedFn = wrappedFn[key] |
| 76 | + } |
| 77 | + |
| 78 | + var originalFn = wrappedFn |
| 79 | + |
| 80 | + var stubFn = function () { |
| 81 | + var action = (newFn.loop) ? newFn.actions[(newFn.callCount - 1) % newFn.actions.length] : newFn.actions.shift() |
| 82 | + |
| 83 | + if (!action) return |
| 84 | + if (action.throwError) throw action.throwError |
| 85 | + if ('returnValue' in action) return action.returnValue |
| 86 | + if ('fn' in action) return action.fn.apply(action.ctx || this, arguments) |
| 87 | + |
| 88 | + var cb = ('cbArgIndex' in action) ? arguments[action.cbArgIndex] : arguments[arguments.length - 1] |
| 89 | + if (action.cbArgs) return cb.apply(action.ctx || null, action.cbArgs) |
| 90 | + } |
| 91 | + |
| 92 | + var newFn = function () { |
| 93 | + var call = { |
| 94 | + k: totalCalls++, // Keep count of calls to record the absolute order of calls |
| 95 | + args: Array.prototype.slice.call(arguments, 0), |
| 96 | + arg: arguments[0], |
| 97 | + context: this |
| 98 | + } |
| 99 | + newFn.calls.push(call) |
| 100 | + newFn.firstCall = newFn.firstCall || call |
| 101 | + newFn.lastCall = call |
| 102 | + newFn.callCount++ |
| 103 | + newFn.called = true |
| 104 | + |
| 105 | + try { |
| 106 | + call.returned = (wrappedFn || _noop).apply(this, arguments) |
| 107 | + } catch(e) { |
| 108 | + call.threw = e |
| 109 | + throw e |
| 110 | + } |
| 111 | + return call.returned |
| 112 | + } |
| 113 | + |
| 114 | + // Spy |
| 115 | + newFn.reset = function () { |
| 116 | + newFn.calls = [] |
| 117 | + newFn.lastCall = { args: [] } // For dot-safety |
| 118 | + newFn.callCount = 0 |
| 119 | + newFn.called = false |
| 120 | + } |
| 121 | + newFn.reset() |
| 122 | + |
| 123 | + // Stub |
| 124 | + newFn.actions = [] |
| 125 | + newFn.loop = true |
| 126 | + |
| 127 | + newFn.callbackWith = newFn.callback = function () { |
| 128 | + wrappedFn = stubFn |
| 129 | + newFn.actions.push({ cbArgs: arguments }) |
| 130 | + return newFn // Chainable |
| 131 | + } |
| 132 | + |
| 133 | + newFn.callbackArgWith = newFn.callbackAtIndex = function () { |
| 134 | + wrappedFn = stubFn |
| 135 | + newFn.actions.push({ |
| 136 | + cbArgs: Array.prototype.slice.call(arguments, 1), |
| 137 | + cbArgIndex: arguments[0] |
| 138 | + }) |
| 139 | + return newFn // Chainable |
| 140 | + } |
| 141 | + |
| 142 | + newFn.inThisContext = function (obj) { |
| 143 | + var action = newFn.actions[newFn.actions.length - 1] |
| 144 | + action.ctx = obj |
| 145 | + return newFn // Chainable |
| 146 | + } |
| 147 | + |
| 148 | + newFn.withArgs = function () { |
| 149 | + var action = newFn.actions[newFn.actions.length - 1] |
| 150 | + action.cbArgs = arguments |
| 151 | + return newFn // Chainable |
| 152 | + } |
| 153 | + |
| 154 | + newFn.returnWith = function (returnValue) { |
| 155 | + wrappedFn = stubFn |
| 156 | + newFn.actions.push({ returnValue: returnValue }) |
| 157 | + return newFn // Chainable |
| 158 | + } |
| 159 | + |
| 160 | + newFn.throwWith = function (err) { |
| 161 | + wrappedFn = stubFn |
| 162 | + newFn.actions.push({ throwError: err }) |
| 163 | + return newFn // Chainable |
| 164 | + } |
| 165 | + |
| 166 | + newFn.resolveWith = function (value) { |
| 167 | + if (simple.Promise.when) return newFn.callFn(function createResolvedPromise () { return simple.Promise.when(value) }) |
| 168 | + return newFn.callFn(function createResolvedPromise () { return simple.Promise.resolve(value) }) |
| 169 | + } |
| 170 | + |
| 171 | + newFn.rejectWith = function (value) { |
| 172 | + return newFn.callFn(function createRejectedPromise () { return simple.Promise.reject(value) }) |
| 173 | + } |
| 174 | + |
| 175 | + newFn.callFn = function (fn) { |
| 176 | + wrappedFn = stubFn |
| 177 | + newFn.actions.push({ fn: fn }) |
| 178 | + return newFn // Chainable |
| 179 | + } |
| 180 | + |
| 181 | + newFn.withActions = function (actions) { |
| 182 | + wrappedFn = stubFn |
| 183 | + if (actions && actions.length >= 0) { |
| 184 | + Array.prototype.push.apply(newFn.actions, actions) |
| 185 | + } |
| 186 | + return newFn // Chainable |
| 187 | + } |
| 188 | + |
| 189 | + newFn.callOriginal = newFn.callOriginalFn = function () { |
| 190 | + wrappedFn = stubFn |
| 191 | + newFn.actions.push({ fn: originalFn }) |
| 192 | + return newFn // Chainable |
| 193 | + } |
| 194 | + |
| 195 | + newFn.withLoop = function () { |
| 196 | + newFn.loop = true |
| 197 | + return newFn // Chainable |
| 198 | + } |
| 199 | + |
| 200 | + newFn.noLoop = function () { |
| 201 | + newFn.loop = false |
| 202 | + return newFn // Chainable |
| 203 | + } |
| 204 | + return newFn |
| 205 | + } |
| 206 | + |
| 207 | + function _restoreMock (mock) { |
| 208 | + if (!mock.oldValueHasKey) { |
| 209 | + delete mock.obj[mock.key] |
| 210 | + return |
| 211 | + } |
| 212 | + mock.obj[mock.key] = mock.oldValue |
| 213 | + } |
| 214 | + |
| 215 | + function _noop () {} |
| 216 | + |
| 217 | + /** |
| 218 | + * Return whether the passed variable is a function |
| 219 | + * |
| 220 | + * @param {Mixed} value |
| 221 | + * @return {Boolean} |
| 222 | + * @api private |
| 223 | + */ |
| 224 | + function isFunction (value) { |
| 225 | + return Object.prototype.toString.call(value) === funcClass |
| 226 | + } |
| 227 | + var funcClass = '[object Function]' |
| 228 | + |
| 229 | + // Browser compatibility |
| 230 | + if (typeof define === 'function' && define.amd) { |
| 231 | + define(function () { |
| 232 | + return simple |
| 233 | + }) |
| 234 | + } else if (typeof window !== 'undefined') { |
| 235 | + window.simple = simple |
| 236 | + } |
| 237 | + |
| 238 | + // Native Promise support |
| 239 | + if (typeof Promise !== 'undefined') simple.Promise = Promise |
| 240 | +})(this, typeof module !== 'undefined' ? module : {exports: {}}) |
0 commit comments