UNPKG

1.22 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const spying_1 = require("../spying");
4const expose_spy_functions_1 = require("./expose-spy-functions");
5class RestorableFunctionSpy extends spying_1.FunctionSpy {
6 constructor(target, functionName) {
7 super();
8 this._originalFunction = target[functionName];
9 this.context = target;
10 this._functionName = functionName;
11 this._target = target;
12 target[functionName] = this.call.bind(this);
13 expose_spy_functions_1.exposeSpyFunctions(target[functionName], this);
14 target[functionName].restore = this.restore.bind(this);
15 }
16 restore() {
17 this._target[this._functionName] = this._originalFunction;
18 }
19 andCallThrough() {
20 this.isStubbed = false;
21 }
22 andStub() {
23 this.isStubbed = true;
24 }
25 call(...args) {
26 const returnValue = super.call.apply(this, args);
27 if (!this.isStubbed && !this.hasReturnValue) {
28 return this._originalFunction.apply(this.context, args);
29 }
30 return returnValue;
31 }
32}
33exports.RestorableFunctionSpy = RestorableFunctionSpy;
34//# sourceMappingURL=restorable-function-spy.js.map
\No newline at end of file