@@ -6,10 +6,86 @@ var Observable = Rx.Observable;
6
6
describe ( 'Observable' , function ( ) {
7
7
it ( 'should be constructed with a subscriber function' , function ( done ) {
8
8
var source = new Observable ( function ( observer ) {
9
+ expectFullObserver ( observer ) ;
9
10
observer . next ( 1 ) ;
10
11
observer . complete ( ) ;
11
12
} ) ;
12
13
13
14
source . subscribe ( function ( x ) { expect ( x ) . toBe ( 1 ) ; } , null , done ) ;
14
15
} ) ;
15
- } ) ;
16
+
17
+ describe ( 'subscribe' , function ( ) {
18
+ it ( 'should be synchronous' , function ( ) {
19
+ var subscribed = false ;
20
+ var nexted , completed ;
21
+ var source = new Observable ( function ( observer ) {
22
+ subscribed = true ;
23
+ observer . next ( 'wee' ) ;
24
+ expect ( nexted ) . toBe ( 'wee' ) ;
25
+ observer . complete ( ) ;
26
+ expect ( completed ) . toBe ( true ) ;
27
+ } ) ;
28
+ expect ( subscribed ) . toBe ( false ) ;
29
+
30
+ var mutatedByNext = false ;
31
+ var mutatedByComplete = false ;
32
+
33
+ source . subscribe ( function ( x ) {
34
+ nexted = x ;
35
+ mutatedByNext = true ;
36
+ } , null , function ( ) {
37
+ completed = true ;
38
+ mutatedByComplete = true ;
39
+ } ) ;
40
+
41
+ expect ( mutatedByNext ) . toBe ( true ) ;
42
+ expect ( mutatedByComplete ) . toBe ( true ) ;
43
+ } ) ;
44
+
45
+ it ( 'should return a Subscription that calls the unsubscribe function returned by the subscriber' , function ( ) {
46
+ var unsubscribeCalled = false ;
47
+
48
+ var source = new Observable ( function ( ) {
49
+ return function ( ) {
50
+ unsubscribeCalled = true ;
51
+ } ;
52
+ } ) ;
53
+
54
+ var sub = source . subscribe ( function ( ) { } ) ;
55
+ expect ( sub instanceof Rx . Subscription ) . toBe ( true ) ;
56
+ expect ( unsubscribeCalled ) . toBe ( false ) ;
57
+ expect ( typeof sub . unsubscribe ) . toBe ( 'function' ) ;
58
+
59
+ sub . unsubscribe ( ) ;
60
+ expect ( unsubscribeCalled ) . toBe ( true ) ;
61
+ } ) ;
62
+ } ) ;
63
+ } ) ;
64
+
65
+ describe ( 'Observable.create' , function ( ) {
66
+ it ( 'should create an Observable' , function ( ) {
67
+ var result = Observable . create ( function ( ) { } ) ;
68
+ expect ( result instanceof Observable ) . toBe ( true ) ;
69
+ } ) ;
70
+
71
+ it ( 'should provide an observer to the function' , function ( ) {
72
+ var called = false ;
73
+ var result = Observable . create ( function ( observer ) {
74
+ called = true ;
75
+ expectFullObserver ( observer ) ;
76
+ observer . complete ( ) ;
77
+ } ) ;
78
+
79
+ expect ( called ) . toBe ( false ) ;
80
+ result . subscribe ( function ( ) { } ) ;
81
+ expect ( called ) . toBe ( true ) ;
82
+ } ) ;
83
+ } ) ;
84
+
85
+ function expectFullObserver ( val ) {
86
+ expect ( typeof val ) . toBe ( 'object' ) ;
87
+ expect ( typeof val . next ) . toBe ( 'function' ) ;
88
+ expect ( typeof val . error ) . toBe ( 'function' ) ;
89
+ expect ( typeof val . complete ) . toBe ( 'function' ) ;
90
+ expect ( typeof val . isUnsubscribed ) . toBe ( 'boolean' ) ;
91
+ }
0 commit comments