1
- /* globals describe, it, expect, hot, cold, expectObservable */
1
+ /* globals describe, it, expect, hot, cold, expectObservable, expectSubscriptions */
2
2
var Rx = require ( '../../dist/cjs/Rx' ) ;
3
3
var Observable = Rx . Observable ;
4
4
5
5
describe ( 'Observable.defer' , function ( ) {
6
6
it ( 'should create an observable from the provided observbale factory' , function ( ) {
7
7
var source = hot ( '--a--b--c--|' ) ;
8
+ var sourceSubs = '^ !' ;
9
+ var expected = '--a--b--c--|' ;
10
+
8
11
var e1 = Observable . defer ( function ( ) {
9
12
return source ;
10
13
} ) ;
11
- var expected = '--a--b--c--|' ;
12
14
13
15
expectObservable ( e1 ) . toBe ( expected ) ;
16
+ expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ;
14
17
} ) ;
15
18
16
19
it ( 'should create an observable from completed' , function ( ) {
17
20
var source = hot ( '|' ) ;
21
+ var sourceSubs = '(^!)' ;
22
+ var expected = '|' ;
23
+
18
24
var e1 = Observable . defer ( function ( ) {
19
25
return source ;
20
26
} ) ;
21
- var expected = '|' ;
22
27
23
28
expectObservable ( e1 ) . toBe ( expected ) ;
29
+ expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ;
24
30
} ) ;
25
31
26
32
it ( 'should create an observable from error' , function ( ) {
27
33
var source = hot ( '#' ) ;
34
+ var sourceSubs = '(^!)' ;
35
+ var expected = '#' ;
36
+
28
37
var e1 = Observable . defer ( function ( ) {
29
38
return source ;
30
39
} ) ;
31
- var expected = '#' ;
32
40
33
41
expectObservable ( e1 ) . toBe ( expected ) ;
42
+ expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ;
34
43
} ) ;
35
44
36
45
it ( 'should create an observable when factory throws' , function ( ) {
@@ -41,4 +50,33 @@ describe('Observable.defer', function () {
41
50
42
51
expectObservable ( e1 ) . toBe ( expected ) ;
43
52
} ) ;
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
+ } ) ;
44
82
} ) ;
0 commit comments