Home Manual Reference Source Test Repository

spec-js/operators/timeout-spec.js

"use strict";
var chai_1 = require('chai');
var Rx = require('../../dist/package/Rx');
var Observable = Rx.Observable;
/** @test {timeout} */
describe('Observable.prototype.timeout', function () {
    var defaultTimeoutError = new Rx.TimeoutError();
    asDiagram('timeout(50)')('should timeout after a specified timeout period', function () {
        var e1 = cold('-------a--b--|');
        var e1subs = '^    !        ';
        var expected = '-----#        ';
        var result = e1.timeout(50, rxTestScheduler);
        expectObservable(result).toBe(expected, null, defaultTimeoutError);
        expectSubscriptions(e1.subscriptions).toBe(e1subs);
    });
    it('should emit and error of an instanceof TimeoutError on timeout', function () {
        var e1 = cold('-------a--b--|');
        var result = e1.timeout(50, rxTestScheduler);
        result.subscribe(function () {
            throw new Error('this should not next');
        }, function (err) {
            chai_1.expect(err).to.be.an.instanceof(Rx.TimeoutError);
        }, function () {
            throw new Error('this should not complete');
        });
        rxTestScheduler.flush();
    });
    it('should not timeout if source completes within absolute timeout period', function () {
        var e1 = hot('--a--b--c--d--e--|');
        var e1subs = '^                !';
        var expected = '--a--b--c--d--e--|';
        var timeoutValue = new Date(rxTestScheduler.now() + (expected.length + 2) * 10);
        expectObservable(e1.timeout(timeoutValue, rxTestScheduler)).toBe(expected);
        expectSubscriptions(e1.subscriptions).toBe(e1subs);
    });
    it('should not timeout if source emits within timeout period', function () {
        var e1 = hot('--a--b--c--d--e--|');
        var e1subs = '^                !';
        var expected = '--a--b--c--d--e--|';
        expectObservable(e1.timeout(50, rxTestScheduler)).toBe(expected);
        expectSubscriptions(e1.subscriptions).toBe(e1subs);
    });
    it('should allow unsubscribing explicitly and early', function () {
        var e1 = hot('--a--b--c---d--e--|');
        var unsub = '          !        ';
        var e1subs = '^         !        ';
        var expected = '--a--b--c--        ';
        var result = e1.timeout(50, rxTestScheduler);
        expectObservable(result, unsub).toBe(expected);
        expectSubscriptions(e1.subscriptions).toBe(e1subs);
    });
    it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
        var e1 = hot('--a--b--c---d--e--|');
        var e1subs = '^         !        ';
        var expected = '--a--b--c--        ';
        var unsub = '          !        ';
        var result = e1
            .mergeMap(function (x) { return Observable.of(x); })
            .timeout(50, rxTestScheduler)
            .mergeMap(function (x) { return Observable.of(x); });
        expectObservable(result, unsub).toBe(expected);
        expectSubscriptions(e1.subscriptions).toBe(e1subs);
    });
    it('should timeout after a specified timeout period between emit with default ' +
        'error while source emits', function () {
        var e1 = hot('---a---b---c------d---e---|');
        var e1subs = '^               !          ';
        var expected = '---a---b---c----#          ';
        var values = { a: 'a', b: 'b', c: 'c' };
        var result = e1.timeout(50, rxTestScheduler);
        expectObservable(result).toBe(expected, values, defaultTimeoutError);
        expectSubscriptions(e1.subscriptions).toBe(e1subs);
    });
    it('should timeout at a specified Date', function () {
        var e1 = cold('-');
        var e1subs = '^         !';
        var expected = '----------#';
        var result = e1.timeout(new Date(rxTestScheduler.now() + 100), rxTestScheduler);
        expectObservable(result).toBe(expected, null, defaultTimeoutError);
        expectSubscriptions(e1.subscriptions).toBe(e1subs);
    });
    it('should timeout specified Date with default error while source emits', function () {
        var e1 = hot('--a--b--c--d--e--|');
        var e1subs = '^         !       ';
        var expected = '--a--b--c-#       ';
        var values = { a: 'a', b: 'b', c: 'c' };
        var result = e1.timeout(new Date(rxTestScheduler.now() + 100), rxTestScheduler);
        expectObservable(result).toBe(expected, values, defaultTimeoutError);
        expectSubscriptions(e1.subscriptions).toBe(e1subs);
    });
    it('should unsubscribe from the scheduled timeout action when timeout is unsubscribed early', function () {
        var e1 = hot('--a--b--c---d--e--|');
        var e1subs = '^         !        ';
        var expected = '--a--b--c--        ';
        var unsub = '          !        ';
        var result = e1
            .lift({
            call: function (timeoutSubscriber, source) {
                var action = timeoutSubscriber.action; // get a ref to the action here
                timeoutSubscriber.add(function () {
                    if (!action.closed) {
                        throw new Error('TimeoutSubscriber scheduled action wasn\'t canceled');
                    }
                });
                return source.subscribe(timeoutSubscriber);
            }
        })
            .timeout(50, rxTestScheduler);
        expectObservable(result, unsub).toBe(expected);
        expectSubscriptions(e1.subscriptions).toBe(e1subs);
    });
});
//# sourceMappingURL=timeout-spec.js.map