Skip to content

Commit

Permalink
test(windowWhen): add unsubscription-related tests for windowWhen
Browse files Browse the repository at this point in the history
Add test asserting window Subject is disposed when outer Observable is unsubscribed. Add test
verifying unsubscription chains are not broken when outer is unsubscribed. Improve subscription
assertions on some windowWhen tests.
  • Loading branch information
staltz authored and kwonoj committed Dec 9, 2015
1 parent 5168f73 commit 4891957
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions spec/operators/windowWhen-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ describe('Observable.prototype.windowWhen', function () {

it('should emit windows using varying cold closings, outer unsubscribed early', function () {
var e1 = hot('--a--^---b---c---d---e---f---g---h------| ');
var e1subs = '^ ! ';
var unsub = ' ! ';
var e1subs = '^ ! ';
var closings = [
cold( '-----------------s--| '),
cold( '-----(s|) ')];
cold( '---------(s|) ')];
var closeSubs = ['^ ! ',
' ^ ! '];
var expected = 'x----------------y-- ';
' ^ ! '];
var expected = 'x----------------y---- ';
var x = cold( '----b---c---d---e| ');
var y = cold( '--- ');
var y = cold( '---f- ');
var unsub = ' ! ';
var values = { x: x, y: y };

var i = 0;
Expand All @@ -124,6 +124,59 @@ describe('Observable.prototype.windowWhen', function () {
expectSubscriptions(closings[1].subscriptions).toBe(closeSubs[1]);
});

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

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

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

it('should dispose window Subjects if the outer is unsubscribed early', function () {
var virtualTimeScheduler = new Rx.VirtualTimeScheduler();
var source = new Rx.Subject();
var result = source.windowWhen(function () { return Observable.never(); });
var win;
var subscription;

virtualTimeScheduler.schedule(function () {
subscription = result.subscribe(function (w) { win = w; });
}, 0);
virtualTimeScheduler.schedule(function () { source.next('a'); }, 0);
virtualTimeScheduler.schedule(function () { source.next('b'); }, 10);
virtualTimeScheduler.schedule(function () { source.next('c'); }, 20);
virtualTimeScheduler.schedule(function () { subscription.unsubscribe(); }, 30);
virtualTimeScheduler.schedule(function () { source.next('d'); }, 40);
virtualTimeScheduler.schedule(function () { source.next('e'); }, 50);
virtualTimeScheduler.schedule(function () { source.next('f'); }, 60);
virtualTimeScheduler.schedule(function () { source.next('g'); }, 70);
virtualTimeScheduler.schedule(function () {
expect(function () {
win.subscribe();
}).toThrowError('Cannot subscribe to a disposed Subject.');
}, 80);

virtualTimeScheduler.flush();
});

it('should propagate error thrown from closingSelector', function () {
var e1 = hot('--a--^---b---c---d---e---f---g---h------| ');
var e1subs = '^ ! ';
Expand Down

0 comments on commit 4891957

Please sign in to comment.