Skip to content

Commit e4c7b52

Browse files
staltzkwonoj
authored andcommitted
test(defer): add unsubscription-related tests
Add test cases for Observable.defer, to include subscription assertions and to verify it behaves correctly when the result is unsubscribed explicitly.
1 parent 5ee11e0 commit e4c7b52

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

spec/observables/defer-spec.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
1-
/* globals describe, it, expect, hot, cold, expectObservable */
1+
/* globals describe, it, expect, hot, cold, expectObservable, expectSubscriptions */
22
var Rx = require('../../dist/cjs/Rx');
33
var Observable = Rx.Observable;
44

55
describe('Observable.defer', function () {
66
it('should create an observable from the provided observbale factory', function () {
77
var source = hot('--a--b--c--|');
8+
var sourceSubs = '^ !';
9+
var expected = '--a--b--c--|';
10+
811
var e1 = Observable.defer(function () {
912
return source;
1013
});
11-
var expected = '--a--b--c--|';
1214

1315
expectObservable(e1).toBe(expected);
16+
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
1417
});
1518

1619
it('should create an observable from completed', function () {
1720
var source = hot('|');
21+
var sourceSubs = '(^!)';
22+
var expected = '|';
23+
1824
var e1 = Observable.defer(function () {
1925
return source;
2026
});
21-
var expected = '|';
2227

2328
expectObservable(e1).toBe(expected);
29+
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
2430
});
2531

2632
it('should create an observable from error', function () {
2733
var source = hot('#');
34+
var sourceSubs = '(^!)';
35+
var expected = '#';
36+
2837
var e1 = Observable.defer(function () {
2938
return source;
3039
});
31-
var expected = '#';
3240

3341
expectObservable(e1).toBe(expected);
42+
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
3443
});
3544

3645
it('should create an observable when factory throws', function () {
@@ -41,4 +50,33 @@ describe('Observable.defer', function () {
4150

4251
expectObservable(e1).toBe(expected);
4352
});
53+
54+
it('should allow unsubscribing early and explicitly', function () {
55+
var source = hot('--a--b--c--|');
56+
var sourceSubs = '^ ! ';
57+
var expected = '--a--b- ';
58+
var unsub = ' ! ';
59+
60+
var e1 = Observable.defer(function () {
61+
return source;
62+
});
63+
64+
expectObservable(e1, unsub).toBe(expected);
65+
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
66+
});
67+
68+
it('should not break unsubscription chains when result is unsubscribed explicitly', function () {
69+
var source = hot('--a--b--c--|');
70+
var sourceSubs = '^ ! ';
71+
var expected = '--a--b- ';
72+
var unsub = ' ! ';
73+
74+
var e1 = Observable.defer(function () {
75+
return source.mergeMap(function (x) { return Observable.of(x); });
76+
})
77+
.mergeMap(function (x) { return Observable.of(x); });
78+
79+
expectObservable(e1, unsub).toBe(expected);
80+
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
81+
});
4482
});

0 commit comments

Comments
 (0)