1
- /* globals describe, it, expect */
1
+ /* globals describe, it, expect, rxTestScheduler */
2
2
var Rx = require ( '../../dist/cjs/Rx' ) ;
3
3
var Observable = Rx . Observable ;
4
4
5
5
describe ( 'Observable.bindCallback' , function ( ) {
6
- it ( 'should emit one value from a callback' , function ( done ) {
6
+ describe ( 'when not scheduled' , function ( ) {
7
+ it ( 'should emit one value from a callback' , function ( ) {
8
+ function callback ( datum , cb ) {
9
+ cb ( datum ) ;
10
+ }
11
+ var boundCallback = Observable . bindCallback ( callback ) ;
12
+ var results = [ ] ;
13
+
14
+ boundCallback ( 42 )
15
+ . subscribe ( function ( x ) {
16
+ results . push ( x ) ;
17
+ } , null , function ( ) {
18
+ results . push ( 'done' ) ;
19
+ } ) ;
20
+
21
+ expect ( results ) . toEqual ( [ 42 , 'done' ] ) ;
22
+ } ) ;
23
+
24
+ it ( 'should emit one value chosen by a selector' , function ( ) {
25
+ function callback ( datum , cb ) {
26
+ cb ( datum ) ;
27
+ }
28
+ var boundCallback = Observable . bindCallback ( callback , function ( datum ) { return datum ; } ) ;
29
+ var results = [ ] ;
30
+
31
+ boundCallback ( 42 )
32
+ . subscribe ( function ( x ) {
33
+ results . push ( x ) ;
34
+ } , null , function ( ) {
35
+ results . push ( 'done' ) ;
36
+ } ) ;
37
+
38
+ expect ( results ) . toEqual ( [ 42 , 'done' ] ) ;
39
+ } ) ;
40
+
41
+ it ( 'should emit an error when the selector throws' , function ( ) {
42
+ function callback ( cb ) {
43
+ cb ( 42 ) ;
44
+ }
45
+ var boundCallback = Observable . bindCallback ( callback , function ( err ) { throw new Error ( 'Yikes!' ) ; } ) ;
46
+ var results = [ ] ;
47
+
48
+ boundCallback ( )
49
+ . subscribe ( function ( ) {
50
+ throw 'should not next' ;
51
+ } , function ( err ) {
52
+ results . push ( err ) ;
53
+ } , function ( ) {
54
+ throw 'should not complete' ;
55
+ } ) ;
56
+
57
+ expect ( results ) . toEqual ( [ new Error ( 'Yikes!' ) ] ) ;
58
+ } ) ;
59
+
60
+ it ( 'should not emit, throw or complete if immediately unsubscribed' , function ( done ) {
61
+ var nextSpy = jasmine . createSpy ( 'next' ) ;
62
+ var throwSpy = jasmine . createSpy ( 'throw' ) ;
63
+ var completeSpy = jasmine . createSpy ( 'complete' ) ;
64
+ var timeout ;
65
+ function callback ( datum , cb ) {
66
+ // Need to cb async in order for the unsub to trigger
67
+ timeout = setTimeout ( function ( ) {
68
+ cb ( datum ) ;
69
+ } ) ;
70
+ }
71
+ var subscription = Observable . bindCallback ( callback ) ( 42 )
72
+ . subscribe ( nextSpy , throwSpy , completeSpy ) ;
73
+ subscription . unsubscribe ( ) ;
74
+
75
+ setTimeout ( function ( ) {
76
+ expect ( nextSpy ) . not . toHaveBeenCalled ( ) ;
77
+ expect ( throwSpy ) . not . toHaveBeenCalled ( ) ;
78
+ expect ( completeSpy ) . not . toHaveBeenCalled ( ) ;
79
+
80
+ clearTimeout ( timeout ) ;
81
+ done ( ) ;
82
+ } ) ;
83
+ } ) ;
84
+ } ) ;
85
+
86
+ describe ( 'when scheduled' , function ( ) {
87
+ it ( 'should emit one value from a callback' , function ( ) {
88
+ function callback ( datum , cb ) {
89
+ cb ( datum ) ;
90
+ }
91
+ var boundCallback = Observable . bindCallback ( callback , null , rxTestScheduler ) ;
92
+ var results = [ ] ;
93
+
94
+ boundCallback ( 42 )
95
+ . subscribe ( function ( x ) {
96
+ results . push ( x ) ;
97
+ } , null , function ( ) {
98
+ results . push ( 'done' ) ;
99
+ } ) ;
100
+
101
+ rxTestScheduler . flush ( ) ;
102
+
103
+ expect ( results ) . toEqual ( [ 42 , 'done' ] ) ;
104
+ } ) ;
105
+
106
+ it ( 'should error if callback throws' , function ( ) {
107
+ function callback ( datum , cb ) {
108
+ throw new Error ( 'haha no callback for you' ) ;
109
+ }
110
+ var boundCallback = Observable . bindCallback ( callback , null , rxTestScheduler ) ;
111
+ var results = [ ] ;
112
+
113
+ boundCallback ( 42 )
114
+ . subscribe ( function ( x ) {
115
+ throw 'should not next' ;
116
+ } , function ( err ) {
117
+ results . push ( err ) ;
118
+ } , function ( ) {
119
+ throw 'should not complete' ;
120
+ } ) ;
121
+
122
+ rxTestScheduler . flush ( ) ;
123
+
124
+ expect ( results ) . toEqual ( [ new Error ( 'haha no callback for you' ) ] ) ;
125
+ } ) ;
126
+
127
+ it ( 'should error if selector throws' , function ( ) {
128
+ function callback ( datum , cb ) {
129
+ cb ( datum ) ;
130
+ }
131
+ function selector ( ) {
132
+ throw new Error ( 'what? a selector? I don\'t think so' ) ;
133
+ }
134
+ var boundCallback = Observable . bindCallback ( callback , selector , rxTestScheduler ) ;
135
+ var results = [ ] ;
136
+
137
+ boundCallback ( 42 )
138
+ . subscribe ( function ( x ) {
139
+ throw 'should not next' ;
140
+ } , function ( err ) {
141
+ results . push ( err ) ;
142
+ } , function ( ) {
143
+ throw 'should not complete' ;
144
+ } ) ;
145
+
146
+ rxTestScheduler . flush ( ) ;
147
+
148
+ expect ( results ) . toEqual ( [ new Error ( 'what? a selector? I don\'t think so' ) ] ) ;
149
+ } ) ;
150
+
151
+ it ( 'should use a selector' , function ( ) {
152
+ function callback ( datum , cb ) {
153
+ cb ( datum ) ;
154
+ }
155
+ function selector ( x ) {
156
+ return x + '!!!' ;
157
+ }
158
+ var boundCallback = Observable . bindCallback ( callback , selector , rxTestScheduler ) ;
159
+ var results = [ ] ;
160
+
161
+ boundCallback ( 42 )
162
+ . subscribe ( function ( x ) {
163
+ results . push ( x ) ;
164
+ } , null , function ( ) {
165
+ results . push ( 'done' ) ;
166
+ } ) ;
167
+
168
+ rxTestScheduler . flush ( ) ;
169
+
170
+ expect ( results ) . toEqual ( [ '42!!!' , 'done' ] ) ;
171
+ } ) ;
172
+ } ) ;
173
+
174
+ it ( 'should pass multiple inner arguments as an array' , function ( ) {
7
175
function callback ( datum , cb ) {
8
- cb ( datum ) ;
176
+ cb ( datum , 1 , 2 , 3 ) ;
9
177
}
10
- var boundCallback = Observable . bindCallback ( callback ) ;
178
+ var boundCallback = Observable . bindCallback ( callback , null , rxTestScheduler ) ;
179
+ var results = [ ] ;
11
180
12
181
boundCallback ( 42 )
13
182
. subscribe ( function ( x ) {
14
- expect ( x ) . toBe ( 42 ) ;
15
- } , function ( ) {
16
- done . fail ( 'should not be called' ) ;
17
- } ,
18
- done ) ;
183
+ results . push ( x ) ;
184
+ } , null , function ( ) {
185
+ results . push ( 'done' ) ;
186
+ } ) ;
187
+
188
+ rxTestScheduler . flush ( ) ;
189
+
190
+ expect ( results ) . toEqual ( [ [ 42 , 1 , 2 , 3 ] , 'done' ] ) ;
19
191
} ) ;
20
192
21
- it ( 'should emit one value chosen by a selector' , function ( done ) {
193
+ it ( 'should pass multiple inner arguments to the selector if there is one ' , function ( ) {
22
194
function callback ( datum , cb ) {
23
- cb ( null , datum ) ;
195
+ cb ( datum , 1 , 2 , 3 ) ;
196
+ }
197
+ function selector ( a , b , c , d ) {
198
+ expect ( [ a , b , c , d ] ) . toEqual ( [ 42 , 1 , 2 , 3 ] ) ;
199
+ return a + b + c + d ;
24
200
}
25
- var boundCallback = Observable . bindCallback ( callback , function ( err , datum ) { return datum ; } ) ;
201
+ var boundCallback = Observable . bindCallback ( callback , selector , rxTestScheduler ) ;
202
+ var results = [ ] ;
26
203
27
204
boundCallback ( 42 )
28
205
. subscribe ( function ( x ) {
29
- expect ( x ) . toBe ( 42 ) ;
30
- } , function ( ) {
31
- done . fail ( 'should not be called' ) ;
32
- } ,
33
- done ) ;
34
- } ) ;
206
+ results . push ( x ) ;
207
+ } , null , function ( ) {
208
+ results . push ( 'done' ) ;
209
+ } ) ;
35
210
36
- it ( 'should emit an error when the selector throws' , function ( done ) {
37
- function callback ( cb ) {
38
- cb ( 42 ) ;
39
- }
40
- var boundCallback = Observable . bindCallback ( callback , function ( err ) { throw new Error ( 'Yikes!' ) ; } ) ;
41
-
42
- boundCallback ( )
43
- . subscribe ( function ( ) {
44
- // Considered a failure if we don't go directly to err handler
45
- done . fail ( 'should not be called' ) ;
46
- } ,
47
- function ( err ) {
48
- expect ( err . message ) . toBe ( 'Yikes!' ) ;
49
- done ( ) ;
50
- } ,
51
- function ( ) {
52
- // Considered a failure if we don't go directly to err handler
53
- done . fail ( 'should not be called' ) ;
54
- }
55
- ) ;
211
+ rxTestScheduler . flush ( ) ;
212
+
213
+ expect ( results ) . toEqual ( [ 48 , 'done' ] ) ;
56
214
} ) ;
57
215
58
- it ( 'should not emit, throw or complete if immediately unsubscribed' , function ( done ) {
59
- var nextSpy = jasmine . createSpy ( 'next' ) ;
60
- var throwSpy = jasmine . createSpy ( 'throw' ) ;
61
- var completeSpy = jasmine . createSpy ( 'complete' ) ;
62
- var timeout ;
216
+ it ( 'should cache value for next subscription and not call callbackFunc again' , function ( ) {
217
+ var calls = 0 ;
63
218
function callback ( datum , cb ) {
64
- // Need to cb async in order for the unsub to trigger
65
- timeout = setTimeout ( function ( ) {
66
- cb ( datum ) ;
67
- } ) ;
219
+ calls ++ ;
220
+ cb ( datum ) ;
68
221
}
69
- var subscription = Observable . bindCallback ( callback ) ( 42 )
70
- . subscribe ( nextSpy , throwSpy , completeSpy ) ;
71
- subscription . unsubscribe ( ) ;
222
+ var boundCallback = Observable . bindCallback ( callback , null , rxTestScheduler ) ;
223
+ var results1 = [ ] ;
224
+ var results2 = [ ] ;
72
225
73
- setTimeout ( function ( ) {
74
- expect ( nextSpy ) . not . toHaveBeenCalled ( ) ;
75
- expect ( throwSpy ) . not . toHaveBeenCalled ( ) ;
76
- expect ( completeSpy ) . not . toHaveBeenCalled ( ) ;
226
+ var source = boundCallback ( 42 ) ;
77
227
78
- clearTimeout ( timeout ) ;
79
- done ( ) ;
228
+ source . subscribe ( function ( x ) {
229
+ results1 . push ( x ) ;
230
+ } , null , function ( ) {
231
+ results1 . push ( 'done' ) ;
80
232
} ) ;
233
+
234
+ source . subscribe ( function ( x ) {
235
+ results2 . push ( x ) ;
236
+ } , null , function ( ) {
237
+ results2 . push ( 'done' ) ;
238
+ } ) ;
239
+
240
+ rxTestScheduler . flush ( ) ;
241
+
242
+ expect ( calls ) . toBe ( 1 ) ;
243
+ expect ( results1 ) . toEqual ( [ 42 , 'done' ] ) ;
244
+ expect ( results2 ) . toEqual ( [ 42 , 'done' ] ) ;
81
245
} ) ;
82
246
} ) ;
0 commit comments