Skip to content

Commit

Permalink
test(operators): add tests against breaking unsubscription chain
Browse files Browse the repository at this point in the history
For issue ReactiveX#875.
  • Loading branch information
staltz committed Dec 8, 2015
1 parent fbd89c5 commit f881743
Show file tree
Hide file tree
Showing 46 changed files with 954 additions and 0 deletions.
20 changes: 20 additions & 0 deletions spec/operators/buffer-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ describe('Observable.prototype.buffer()', function () {
expectSubscriptions(a.subscriptions).toBe(subs);
});

it('should not break unsubscription chains when unsubscribed explicitly', function () {
var a = hot('--1--2--^--3--4--5---6----7--8--9---0---|');
var subs = '^ ! ';
var b = hot('--------^--a-------b---cd| ');
var expected = '---a-------b--- ';
var unsub = ' ! ';
var expectedValues = {
a: ['3'],
b: ['4', '5']
};

var result = a
.mergeMap(function (x) { return Observable.of(x); })
.buffer(b)
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected, expectedValues);
expectSubscriptions(a.subscriptions).toBe(subs);
});

it('should work with non-empty and selector error', function () {
// Buffer Boundaries onErrorSource (RxJS 4)
var a = hot('--1--2--^--3-----#', {'3': 3}, new Error('too bad'));
Expand Down
19 changes: 19 additions & 0 deletions spec/operators/bufferCount-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ describe('Observable.prototype.bufferCount', function () {
expectSubscriptions(e1.subscriptions).toBe(subs);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var values = {
v: ['a', 'b', 'c'],
w: ['c', 'd', 'e']
};
var e1 = hot('--a--b--c--d--e--f--g--h--i--|');
var subs = '^ ! ';
var expected = '--------v-----w---- ';
var unsub = ' ! ';

var result = e1
.mergeMap(function (x) { return Observable.of(x); })
.bufferCount(3, 2)
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
});

it('should raise error if source raise error before reaching specified buffer count', function () {
var e1 = hot('--a--b--c--d--#');
var e1subs = '^ !';
Expand Down
22 changes: 22 additions & 0 deletions spec/operators/bufferTime-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ describe('Observable.prototype.bufferTime', function () {
expectSubscriptions(e1.subscriptions).toBe(subs);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var e1 = hot('--1--^2--3---4---5--6--7---8----9------------|');
var subs = '^ ! ';
// -------*------*------*------*------*----- creation interval
// ----------| timespans
// ----------|
// ----------|
var expected = '----------a------ ';
var unsub = ' ! ';
var values = {
a: ['2', '3', '4']
};

var result = e1
.mergeMap(function (x) { return Observable.of(x); })
.bufferTime(100, 70, rxTestScheduler)
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
});

it('should handle empty', function () {
var e1 = cold( '|');
var e1subs = '(^!)';
Expand Down
24 changes: 24 additions & 0 deletions spec/operators/bufferToggle-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ describe('Observable.prototype.bufferToggle', function () {
expectSubscriptions(closings[2].subscriptions).toBe([]);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var e1 = hot('--a--^---b---c---d---e---f---g---h------| ');
var subs = '^ ! ';
var e2 = cold('--x-----------y--------z---| ');
var closings = [
cold( '---------------s--| '),
cold( '----(s|) '),
cold( '---------------(s|)')];
var expected = '-----------------i- ';
var unsub = ' ! ';
var values = {
i: ['b','c','d','e']
};

var i = 0;
var result = e1
.mergeMap(function (x) { return Observable.of(x); })
.bufferToggle(e2, function () { return closings[i++]; })
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
});

it('should propagate error thrown from closingSelector', function () {
var e1 = hot('--a--^---b---c---d---e---f---g---h------| ');
var e2 = cold('--x-----------y--------z---| ');
Expand Down
28 changes: 28 additions & 0 deletions spec/operators/bufferWhen-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,34 @@ describe('Observable.prototype.bufferWhen', function () {
expectSubscriptions(closings[2].subscriptions).toBe([]);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var e1 = hot('--a--^---b---c---d---e---f---g---h------| ');
var subs = '^ ! ';
var closings = [
cold( '---------------(s|) '),
cold( '----------(s|) '),
cold( '-------------(s|)')];
var closeSubs = ['^ ! ',
' ^ ! '];
var expected = '---------------x--- ';
var unsub = ' ! ';
var values = {
x: ['b','c','d']
};

var i = 0;
var result = e1
.mergeMap(function (x) { return Observable.of(x); })
.bufferWhen(function () { return closings[i++]; })
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(subs);
expectSubscriptions(closings[0].subscriptions).toBe(closeSubs[0]);
expectSubscriptions(closings[1].subscriptions).toBe(closeSubs[1]);
expectSubscriptions(closings[2].subscriptions).toBe([]);
});

it('should propagate error thrown from closingSelector', function () {
var e1 = hot('--a--^---b---c---d---e---f---g---h------| ');
var subs = '^ ! ';
Expand Down
19 changes: 19 additions & 0 deletions spec/operators/concat-spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* globals describe, it, expect, expectObservable, expectSubscriptions, hot, cold, rxTestScheduler */
var Rx = require('../../dist/cjs/Rx');
var Observable = Rx.Observable;

describe('Observable.prototype.concat()', function () {
it.asDiagram('concat')('should concatenate two cold observables', function () {
Expand Down Expand Up @@ -168,6 +169,24 @@ describe('Observable.prototype.concat()', function () {
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var e1 = cold('---a-a--a| ');
var e1subs = '^ ! ';
var e2 = cold( '-----b-b--b-|');
var e2subs = ' ^ ! ';
var expected = '---a-a--a-----b-b- ';
var unsub = ' ! ';

var result = e1
.mergeMap(function (x) { return Observable.of(x); })
.concat(e2)
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});

it('should raise error from first source and does not emit from second source', function () {
var e1 = cold('--#');
var e1subs = '^ !';
Expand Down
18 changes: 18 additions & 0 deletions spec/operators/concatAll-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,24 @@ describe('Observable.prototype.concatAll()', function () {
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var e1 = cold('---a-a--a| ');
var e1subs = '^ ! ';
var e2 = cold( '-----b-b--b-|');
var e2subs = ' ^ ! ';
var expected = '---a-a--a-----b-b- ';
var unsub = ' ! ';

var result = Observable.of(e1, e2)
.mergeMap(function (x) { return Observable.of(x); })
.concatAll()
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});

it('should raise error from first source and does not emit from second source', function () {
var e1 = cold('--#');
var e1subs = '^ !';
Expand Down
37 changes: 37 additions & 0 deletions spec/operators/concatMap-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,43 @@ describe('Observable.prototype.concatMap()', function () {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var a = cold( '-# ');
var asubs = [];
var b = cold( '-# ');
var bsubs = [];
var c = cold( '-2--3--4--5----6-| ');
var csubs = ' ^ ! ';
var d = cold( '----2--3| ');
var dsubs = ' ^ ! ';
var e = cold( '-1------2--3-4-5---| ');
var esubs = ' ^ ! ';
var f = cold( '--| ');
var fsubs = [];
var g = cold( '---1-2|');
var gsubs = [];
var e1 = hot('-a-b--^-c-----d------e----------------f-----g| ');
var e1subs = '^ ! ';
var unsub = ' ! ';
var expected = '---2--3--4--5----6-----2--3-1-- ';
var observableLookup = { a: a, b: b, c: c, d: d, e: e, f: f, g: g };

var result = e1
.mergeMap(function (x) { return Observable.of(x); })
.concatMap(function (value) { return observableLookup[value]; })
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(a.subscriptions).toBe(asubs);
expectSubscriptions(b.subscriptions).toBe(bsubs);
expectSubscriptions(c.subscriptions).toBe(csubs);
expectSubscriptions(d.subscriptions).toBe(dsubs);
expectSubscriptions(e.subscriptions).toBe(esubs);
expectSubscriptions(f.subscriptions).toBe(fsubs);
expectSubscriptions(g.subscriptions).toBe(gsubs);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should concatMap many complex, all inners finite, project throws', function () {
var a = cold( '-# ');
var asubs = [];
Expand Down
20 changes: 20 additions & 0 deletions spec/operators/concatMapTo-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@ describe('Observable.prototype.concatMapTo()', function () {
expectSubscriptions(inner.subscriptions).toBe(innersubs);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
var e1 = hot('-a---b---c---d---| ');
var e1subs = '^ !';
var inner = cold('--i-j-k-l-| ', values);
var innersubs = [' ^ ! ',
' ^ !'];
var expected = '---i-j-k-l---i-j-k-';
var unsub = ' !';

var result = e1
.mergeMap(function (x) { return Observable.of(x); })
.concatMapTo(inner)
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(inner.subscriptions).toBe(innersubs);
});

it('should concatMapTo many outer to many inner, inner never completes', function () {
var values = {i: 'foo', j: 'bar', k: 'baz', l: 'qux'};
var e1 = hot('-a---b---c---d---|');
Expand Down
15 changes: 15 additions & 0 deletions spec/operators/delay-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ describe('Observable.prototype.delay()', function () {
expectSubscriptions(e1.subscriptions).toBe(subs);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var e1 = hot('---a---b----');
var e1subs = '^ ! ';
var expected = '------a-- ';
var unsub = ' ! ';

var result = e1
.mergeMap(function (x) { return Observable.of(x); })
.delay(30, rxTestScheduler)
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should not complete when source never completes', function () {
var e1 = Observable.never();
var expected = '-';
Expand Down
15 changes: 15 additions & 0 deletions spec/operators/do-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ describe('Observable.prototype.do()', function () {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var e1 = hot('--1--2--3--#');
var e1subs = '^ ! ';
var expected = '--1--2-- ';
var unsub = ' ! ';

var result = e1
.mergeMap(function (x) { return Observable.of(x); })
.do()
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should mirror multiple values and complete', function () {
var e1 = cold('--1--2--3--|');
var e1subs = '^ !';
Expand Down
15 changes: 15 additions & 0 deletions spec/operators/isEmpty-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,19 @@ describe('Observable.prototype.isEmpty()', function () {
expectObservable(source.isEmpty(), unsub).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var source = cold('-----------a--b--|');
var subs = '^ ! ';
var expected = '------- ';
var unsub = ' ! ';

var result = source
.mergeMap(function (x) { return Observable.of(x); })
.isEmpty()
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});
});
15 changes: 15 additions & 0 deletions spec/operators/last-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ describe('Observable.prototype.last()', function () {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var e1 = hot('--a--b--c--d--|');
var e1subs = '^ ! ';
var expected = '-------- ';
var unsub = ' ! ';

var result = e1
.mergeMap(function (x) { return Observable.of(x); })
.last()
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should return a default value if no element found', function () {
var e1 = cold('|');
var e1subs = '(^!)';
Expand Down
20 changes: 20 additions & 0 deletions spec/operators/materialize-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ describe('Observable.prototype.materialize()', function () {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
var e1 = hot('--a--b--c--|');
var e1subs = '^ ! ';
var expected = '--w--x- ';
var unsub = ' ! ';

var expectedValue = {
w: Notification.createNext('a'),
x: Notification.createNext('b')
};

var result = e1
.mergeMap(function (x) { return Observable.of(x); })
.materialize()
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected, expectedValue);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should materialize stream does not completes', function () {
var e1 = hot('-');
var e1subs = '^';
Expand Down
Loading

0 comments on commit f881743

Please sign in to comment.