1
- /* globals describe, it, expect */
1
+ /* globals describe, it, expect, expectObservable, hot, cold, lowerCaseO */
2
2
var Rx = require ( '../../dist/cjs/Rx' ) ;
3
3
var Observable = Rx . Observable ;
4
+ var Promise = require ( 'promise' ) ;
4
5
5
- describe ( 'Observable.prototype.withLatestFrom()' , function ( ) {
6
- it ( 'should merge the emitted value with the latest values of the other observables' , function ( done ) {
7
- var a = Observable . of ( 'a' ) ;
8
- var b = Observable . of ( 'b' , 'c' ) ;
9
-
10
- Observable . of ( 'd' ) . delay ( 100 )
11
- . withLatestFrom ( a , b , function ( x , a , b ) { return [ x , a , b ] ; } )
12
- . subscribe ( function ( x ) {
13
- expect ( x ) . toEqual ( [ 'd' , 'a' , 'c' ] ) ;
14
- } , null , done ) ;
6
+ describe ( 'Observable.prototype.withLatestFrom()' , function ( ) {
7
+ it ( 'should merge the value with the latest values from the other observables into arrays' , function ( ) {
8
+ var e1 = hot ( '--a--^---b---c---d--|' ) ;
9
+ var e2 = hot ( '--e--^-f---g---h----|' ) ;
10
+ var e3 = hot ( '--i--^-j---k---l----|' ) ;
11
+ var expected = '----x---y---z--|' ;
12
+ var values = {
13
+ x : [ 'b' , 'f' , 'j' ] ,
14
+ y : [ 'c' , 'g' , 'k' ] ,
15
+ z : [ 'd' , 'h' , 'l' ]
16
+ } ;
17
+ expectObservable ( e1 . withLatestFrom ( e2 , e3 ) ) . toBe ( expected , values ) ;
15
18
} ) ;
16
19
17
- it ( 'should emit nothing if the other observables never emit' , function ( done ) {
18
- var a = Observable . of ( 'a' ) ;
19
- var b = Observable . never ( ) ;
20
-
21
- Observable . of ( 'd' ) . delay ( 100 )
22
- . withLatestFrom ( a , b , function ( x , a , b ) { return [ x , a , b ] ; } )
23
- . subscribe ( function ( x ) {
24
- expect ( 'this was called' ) . toBe ( false ) ;
20
+ it ( 'should merge the value with the latest values from the other observables into arrays and a project argument' , function ( ) {
21
+ var e1 = hot ( '--a--^---b---c---d--|' ) ;
22
+ var e2 = hot ( '--e--^-f---g---h----|' ) ;
23
+ var e3 = hot ( '--i--^-j---k---l----|' ) ;
24
+ var expected = '----x---y---z--|' ;
25
+ var values = {
26
+ x : 'bfj' ,
27
+ y : 'cgk' ,
28
+ z : 'dhl'
29
+ } ;
30
+ var project = function ( a , b , c ) { return a + b + c } ;
31
+ expectObservable ( e1 . withLatestFrom ( e2 , e3 , project ) ) . toBe ( expected , values ) ;
32
+ } ) ;
33
+
34
+ it ( 'should handle empty' , function ( ) {
35
+ var e1 = Observable . empty ( ) ;
36
+ var e2 = hot ( '--e--^-f---g---h----|' ) ;
37
+ var e3 = hot ( '--i--^-j---k---l----|' ) ;
38
+ var expected = '|' ; // empty
39
+ expectObservable ( e1 . withLatestFrom ( e2 , e3 ) ) . toBe ( expected ) ;
40
+ } ) ;
41
+
42
+ it ( 'should handle never' , function ( ) {
43
+ var e1 = Observable . never ( ) ;
44
+ var e2 = hot ( '--e--^-f---g---h----|' ) ;
45
+ var e3 = hot ( '--i--^-j---k---l----|' ) ;
46
+ var expected = '--------------------' ; // never
47
+ expectObservable ( e1 . withLatestFrom ( e2 , e3 ) ) . toBe ( expected ) ;
48
+ } ) ;
49
+
50
+ it ( 'should handle throw' , function ( ) {
51
+ var e1 = Observable . throw ( new Error ( 'sad' ) ) ;
52
+ var e2 = hot ( '--e--^-f---g---h----|' ) ;
53
+ var e3 = hot ( '--i--^-j---k---l----|' ) ;
54
+ var expected = '#' ; // throw
55
+ expectObservable ( e1 . withLatestFrom ( e2 , e3 ) ) . toBe ( expected , null , new Error ( 'sad' ) ) ;
56
+ } ) ;
57
+
58
+ it ( 'should handle error' , function ( ) {
59
+ var e1 = hot ( '--a--^---b---#' , undefined , new Error ( 'boo-hoo' ) ) ;
60
+ var e2 = hot ( '--e--^-f---g---h----|' ) ;
61
+ var e3 = hot ( '--i--^-j---k---l----|' ) ;
62
+ var expected = '----x---#' ; // throw
63
+ var values = {
64
+ x : [ 'b' , 'f' , 'j' ]
65
+ } ;
66
+ expectObservable ( e1 . withLatestFrom ( e2 , e3 ) ) . toBe ( expected , values , new Error ( 'boo-hoo' ) ) ;
67
+ } ) ;
68
+
69
+ it ( 'should handle error with project argument' , function ( ) {
70
+ var e1 = hot ( '--a--^---b---#' , undefined , new Error ( 'boo-hoo' ) ) ;
71
+ var e2 = hot ( '--e--^-f---g---h----|' ) ;
72
+ var e3 = hot ( '--i--^-j---k---l----|' ) ;
73
+ var expected = '----x---#' ; // throw
74
+ var values = {
75
+ x : 'bfj'
76
+ } ;
77
+ var project = function ( a , b , c ) { return a + b + c ; } ;
78
+ expectObservable ( e1 . withLatestFrom ( e2 , e3 , project ) ) . toBe ( expected , values , new Error ( 'boo-hoo' ) ) ;
79
+ } ) ;
80
+
81
+ it ( 'should handle merging with empty' , function ( ) {
82
+ var e1 = hot ( '--a--^---b---c---d--|' ) ;
83
+ var e2 = Observable . empty ( ) ;
84
+ var e3 = hot ( '--i--^-j---k---l----|' ) ;
85
+ var expected = '---------------|' ;
86
+ expectObservable ( e1 . withLatestFrom ( e2 , e3 ) ) . toBe ( expected ) ;
87
+ } ) ;
88
+
89
+ it ( 'should handle merging with never' , function ( ) {
90
+ var e1 = hot ( '--a--^---b---c---d--|' ) ;
91
+ var e2 = Observable . never ( ) ;
92
+ var e3 = hot ( '--i--^-j---k---l----|' ) ;
93
+ var expected = '---------------|' ;
94
+ expectObservable ( e1 . withLatestFrom ( e2 , e3 ) ) . toBe ( expected ) ;
95
+ } ) ;
96
+
97
+ it ( 'should handle promises' , function ( done ) {
98
+ Observable . of ( 1 ) . delay ( 1 ) . withLatestFrom ( Promise . resolve ( 2 ) , Promise . resolve ( 3 ) )
99
+ . subscribe ( function ( x ) {
100
+ expect ( x ) . toEqual ( [ 1 , 2 , 3 ] ) ;
25
101
} , null , done ) ;
26
102
} ) ;
103
+
104
+ it ( 'should handle arrays' , function ( ) {
105
+ Observable . of ( 1 ) . delay ( 1 ) . withLatestFrom ( [ 2 , 3 , 4 ] , [ 4 , 5 , 6 ] )
106
+ . subscribe ( function ( x ) {
107
+ expect ( x ) . toEqual ( [ 1 , 4 , 6 ] ) ;
108
+ } ) ;
109
+ } ) ;
110
+
111
+ it ( 'should handle lowercase-o observables' , function ( ) {
112
+ Observable . of ( 1 ) . delay ( 1 ) . withLatestFrom ( lowerCaseO ( 2 , 3 , 4 ) , lowerCaseO ( 4 , 5 , 6 ) )
113
+ . subscribe ( function ( x ) {
114
+ expect ( x ) . toEqual ( [ 1 , 4 , 6 ] ) ;
115
+ } ) ;
116
+ } ) ;
27
117
} ) ;
0 commit comments