dev-toolkit
Version:
Universal Development Toolkit for React Veterans
124 lines (101 loc) • 3.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _sinon = _interopRequireDefault(require("sinon"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
// This class will serve as our proxy for creating and restoring each sandbox
// We can either use the sandbox globally or only within a specified `describe`-block
var Sandbox =
/*#__PURE__*/
function () {
function Sandbox() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
global: false
};
_classCallCheck(this, Sandbox);
this.instance = null;
this.useSandbox = false;
this.useGlobalSandbox = options.global;
}
_createClass(Sandbox, [{
key: "use",
value: function use(beforeCallback, afterCallback) {
var _this = this;
beforeCallback(function () {
_this.useSandbox = true;
});
afterCallback(function () {
_this.useSandbox = false;
});
}
}, {
key: "create",
value: function create() {
if (this.useGlobalSandbox || this.useSandbox) {
this.instance = _sinon.default.sandbox.create();
}
}
}, {
key: "restore",
value: function restore() {
if (this.useGlobalSandbox || this.useSandbox) {
this.instance.restore();
}
} // Replicate sinon's `sandbox` methods
// see: http://sinonjs.org/docs/#sinon-sandbox
}, {
key: "spy",
value: function spy() {
var _this$instance;
return this.instance && (_this$instance = this.instance).spy.apply(_this$instance, arguments);
}
}, {
key: "stub",
value: function stub() {
var _this$instance2;
return this.instance && (_this$instance2 = this.instance).stub.apply(_this$instance2, arguments);
}
}, {
key: "mock",
value: function mock() {
var _this$instance3;
return this.instance && (_this$instance3 = this.instance).mock.apply(_this$instance3, arguments);
}
}, {
key: "useFakeTimers",
value: function useFakeTimers() {
var _this$instance4;
return this.instance && (_this$instance4 = this.instance).useFakeTimers.apply(_this$instance4, arguments);
}
}, {
key: "useFakeXMLHttpRequest",
value: function useFakeXMLHttpRequest() {
var _this$instance5;
return this.instance && (_this$instance5 = this.instance).useFakeXMLHttpRequest.apply(_this$instance5, arguments);
}
}, {
key: "useFakeServer",
value: function useFakeServer() {
var _this$instance6;
return this.instance && (_this$instance6 = this.instance).useFakeServer.apply(_this$instance6, arguments);
}
}]);
return Sandbox;
}(); // 1. Instantiate class
var sandbox = new Sandbox({
global: false
}); // 2. Use sandbox's methods in mocha's "Root-Level Hooks"
// see: https://mochajs.org/#root-level-hooks
beforeEach(function () {
sandbox.create();
});
afterEach(function () {
sandbox.restore();
}); // 3. Export class to use as sandbox replacement
var _default = sandbox;
exports.default = _default;