UNPKG

expect

Version:
90 lines (62 loc) 1.78 kB
'use strict'; exports.__esModule = true; exports.createSpy = createSpy; exports.spyOn = spyOn; exports.isSpy = isSpy; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } var _invariant = require('./invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _isFunction = require('./isFunction'); var _isFunction2 = _interopRequireDefault(_isFunction); function noop() {} function createSpy(fn) { if (fn == null) fn = noop; _invariant2['default'](_isFunction2['default'](fn), 'createSpy needs a function'); var targetFn = undefined, thrownValue = undefined, returnValue = undefined; var spy = function spy() { spy.calls.push({ context: this, arguments: Array.prototype.slice.call(arguments, 0) }); if (targetFn) return targetFn.apply(this, arguments); if (thrownValue) throw thrownValue; return returnValue; }; spy.calls = []; spy.andCall = function (fn) { targetFn = fn; return spy; }; spy.andCallThrough = function () { return spy.andCall(fn); }; spy.andThrow = function (object) { thrownValue = object; return spy; }; spy.andReturn = function (value) { returnValue = value; return spy; }; spy.getLastCall = function () { return spy.calls[spy.calls.length - 1]; }; spy.__isSpy = true; return spy; } function spyOn(object, methodName) { var original = object[methodName]; if (original == null || !original.__isSpy) { var spy = createSpy(original); spy.restore = spy.destroy = function () { object[methodName] = original; }; object[methodName] = spy; } return object[methodName]; } function isSpy(object) { return object && object.__isSpy === true; }