UNPKG

@hirez_io/observer-spy

Version:

A simple little class that helps making Observable testing a breeze

96 lines 3.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObserverSpy = void 0; var ObserverSpy = /** @class */ (function () { function ObserverSpy(config) { this.onNextValues = []; this.state = { nextWasCalled: false, errorWasCalled: false, completeWasCalled: false, errorValue: undefined, onCompleteCallback: undefined, onErrorCallback: undefined, errorIsExpected: false, }; if (config && config.expectErrors) { this.expectErrors(); } } ObserverSpy.prototype.next = function (value) { this.onNextValues.push(value); this.state.nextWasCalled = true; }; ObserverSpy.prototype.error = function (errorVal) { if (!this.state.errorIsExpected) { throw errorVal; } this.state.errorValue = errorVal; this.state.errorWasCalled = true; if (this.state.onErrorCallback) { this.state.onErrorCallback(); } }; ObserverSpy.prototype.complete = function () { this.state.completeWasCalled = true; if (this.state.onCompleteCallback) { this.state.onCompleteCallback(undefined); } }; ObserverSpy.prototype.onComplete = function (callback) { var _this = this; if (this.state.completeWasCalled) { return callback ? callback(undefined) : Promise.resolve(); } if (callback) { this.state.onCompleteCallback = callback; return; } return new Promise(function (resolve) { _this.state.onCompleteCallback = resolve; }); }; ObserverSpy.prototype.onError = function () { var _this = this; if (this.state.errorWasCalled) { return Promise.resolve(); } return new Promise(function (resolve) { _this.state.onErrorCallback = resolve; }); }; ObserverSpy.prototype.expectErrors = function () { this.state.errorIsExpected = true; return this; }; ObserverSpy.prototype.getValuesLength = function () { return this.onNextValues.length; }; ObserverSpy.prototype.getValues = function () { return this.onNextValues; }; ObserverSpy.prototype.getValueAt = function (index) { return this.onNextValues[index]; }; ObserverSpy.prototype.getFirstValue = function () { return this.onNextValues[0]; }; ObserverSpy.prototype.getLastValue = function () { return this.onNextValues[this.onNextValues.length - 1]; }; ObserverSpy.prototype.receivedNext = function () { return this.state.nextWasCalled; }; ObserverSpy.prototype.getError = function () { return this.state.errorValue; }; ObserverSpy.prototype.receivedError = function () { return this.state.errorWasCalled; }; ObserverSpy.prototype.receivedComplete = function () { return this.state.completeWasCalled; }; return ObserverSpy; }()); exports.ObserverSpy = ObserverSpy; //# sourceMappingURL=observer-spy.js.map