1
- /* globals describe, it, expect */
1
+ /* globals describe, it, expect, expectObservable, hot, cold, rxTestScheduler */
2
2
var Rx = require ( '../../dist/cjs/Rx' ) ;
3
3
var Observable = Rx . Observable ;
4
4
5
- describe ( 'Observable.prototype.timeoutWith' , function ( ) {
6
- it ( 'should timeout after a specified delay then subscribe to the passed observable' , function ( done ) {
7
- var expected = [ 1 , 2 , 3 ] ;
8
- Observable . never ( ) . timeoutWith ( 100 , Observable . of ( 1 , 2 , 3 ) )
9
- . subscribe ( function ( x ) {
10
- expect ( x ) . toBe ( expected . shift ( ) ) ;
11
- } , null , done ) ;
5
+ describe ( 'Observable.prototype.timeoutWith()' , function ( ) {
6
+ it ( 'should timeout after a specified period then subscribe to the passed observable' , function ( ) {
7
+ var e1 = Observable . never ( ) ;
8
+ var e2 = cold ( '--x--y--z--|' ) ;
9
+ var expected = '-------x--y--z--|' ;
10
+
11
+ expectObservable ( e1 . timeoutWith ( 50 , e2 , rxTestScheduler ) ) . toBe ( expected ) ;
12
+ } ) ;
13
+
14
+ it ( 'should timeout at a specified date then subscribe to the passed observable' , function ( done ) {
15
+ var expected = [ 'x' , 'y' , 'z' ] ;
16
+ var e1 = Observable . never ( ) ;
17
+ var e2 = Observable . fromArray ( expected ) ;
18
+
19
+ var res = [ ] ;
20
+ e1 . timeoutWith ( new Date ( Date . now ( ) + 100 ) , e2 )
21
+ . subscribe ( function ( x ) {
22
+ res . push ( x ) ;
23
+ } , function ( x ) {
24
+ throw 'should not be called' ;
25
+ } , function ( ) {
26
+ expect ( res ) . toEqual ( expected ) ;
27
+ done ( ) ;
28
+ } ) ;
12
29
} , 2000 ) ;
13
30
31
+ it ( 'should timeout after a specified period between emit then subscribe to the passed observable when source emits' , function ( ) {
32
+ var e1 = hot ( '---a---b------c---|' ) ;
33
+ var e2 = cold ( '-x-y-|' ) ;
34
+ var expected = '---a---b----x-y-|' ;
35
+
36
+ expectObservable ( e1 . timeoutWith ( 40 , e2 , rxTestScheduler ) ) . toBe ( expected ) ;
37
+ } ) ;
14
38
15
- it ( 'should timeout at a specified date then subscribe to the passed observable' , function ( done ) {
16
- var expected = [ 1 , 2 , 3 ] ;
17
- var date = new Date ( Date . now ( ) + 100 ) ;
18
- Observable . never ( ) . timeoutWith ( date , Observable . of ( 1 , 2 , 3 ) )
19
- . subscribe ( function ( x ) {
20
- expect ( x ) . toBe ( expected . shift ( ) ) ;
21
- } , null , done ) ;
39
+ it ( 'should timeout after a specified period then subscribe to the passed observable when source is empty' , function ( ) {
40
+ var e1 = hot ( '-------------|' ) ;
41
+ var e2 = cold ( '----x----|' ) ;
42
+ var expected = '--------------x----|' ;
43
+
44
+ expectObservable ( e1 . timeoutWith ( 100 , e2 , rxTestScheduler ) ) . toBe ( expected ) ;
45
+ } ) ;
46
+
47
+ it ( 'should timeout after a specified period between emit then never completes if other source does not complete' , function ( ) {
48
+ var e1 = hot ( '--a--b--------c--d--|' ) ;
49
+ var e2 = cold ( '-' ) ;
50
+ var expected = '--a--b----' ;
51
+
52
+ expectObservable ( e1 . timeoutWith ( 40 , e2 , rxTestScheduler ) ) . toBe ( expected ) ;
53
+ } ) ;
54
+
55
+ it ( 'should timeout after a specified period then subscribe to the passed observable when source raises error after timeout' , function ( ) {
56
+ var e1 = hot ( '-------------#' ) ;
57
+ var e2 = cold ( '----x----|' ) ;
58
+ var expected = '--------------x----|' ;
59
+
60
+ expectObservable ( e1 . timeoutWith ( 100 , e2 , rxTestScheduler ) ) . toBe ( expected ) ;
61
+ } ) ;
62
+
63
+ it ( 'should timeout after a specified period between emit then never completes if other source emits but not complete' , function ( ) {
64
+ var e1 = hot ( '-------------|' ) ;
65
+ var e2 = cold ( '----x----' ) ;
66
+ var expected = '--------------x----' ;
67
+
68
+ expectObservable ( e1 . timeoutWith ( 100 , e2 , rxTestScheduler ) ) . toBe ( expected ) ;
69
+ } ) ;
70
+
71
+ it ( 'should not timeout if source completes within timeout period' , function ( ) {
72
+ var e1 = hot ( '-----|' ) ;
73
+ var e2 = cold ( '----x----' ) ;
74
+ var expected = '-----|' ;
75
+
76
+ expectObservable ( e1 . timeoutWith ( 100 , e2 , rxTestScheduler ) ) . toBe ( expected ) ;
77
+ } ) ;
78
+
79
+ it ( 'should not timeout if source raises error within timeout period' , function ( ) {
80
+ var e1 = hot ( '-----#' ) ;
81
+ var e2 = cold ( '----x----|' ) ;
82
+ var expected = '-----#' ;
83
+
84
+ expectObservable ( e1 . timeoutWith ( 100 , e2 , rxTestScheduler ) ) . toBe ( expected ) ;
85
+ } ) ;
86
+
87
+ it ( 'should not timeout if source emits within timeout period' , function ( ) {
88
+ var e1 = hot ( '--a--b--c--d--e--|' ) ;
89
+ var e2 = cold ( '----x----|' ) ;
90
+ var expected = '--a--b--c--d--e--|' ;
91
+
92
+ expectObservable ( e1 . timeoutWith ( 50 , e2 , rxTestScheduler ) ) . toBe ( expected ) ;
93
+ } ) ;
94
+
95
+ it ( 'should timeout after specified Date then subscribe to the passed observable' , function ( done ) {
96
+ var e1 = Observable . interval ( 40 ) . take ( 5 ) ;
97
+ var e2 = Observable . of ( 100 ) ;
98
+
99
+ var res = [ ] ;
100
+ e1 . timeoutWith ( new Date ( Date . now ( ) + 100 ) , e2 )
101
+ . subscribe ( function ( x ) {
102
+ res . push ( x ) ;
103
+ } , function ( x ) {
104
+ throw 'should not be called' ;
105
+ } , function ( ) {
106
+ expect ( res ) . toEqual ( [ 0 , 1 , 100 ] ) ;
107
+ done ( ) ;
108
+ } ) ;
22
109
} , 2000 ) ;
110
+
111
+ it ( 'should not timeout if source completes within specified Date' , function ( ) {
112
+ var e1 = hot ( '--a--b--c--d--e--|' ) ;
113
+ var e2 = cold ( '--x--|' ) ;
114
+ var expected = '--a--b--c--d--e--|' ;
115
+
116
+ var timeoutValue = new Date ( Date . now ( ) + ( expected . length + 2 ) * 10 ) ;
117
+
118
+ expectObservable ( e1 . timeoutWith ( timeoutValue , e2 , rxTestScheduler ) ) . toBe ( expected ) ;
119
+ } ) ;
120
+
121
+ it ( 'should not timeout if source raises error within specified Date' , function ( ) {
122
+ var e1 = hot ( '---a---#' ) ;
123
+ var e2 = cold ( '--x--|' ) ;
124
+ var expected = '---a---#' ;
125
+
126
+ expectObservable ( e1 . timeoutWith ( new Date ( Date . now ( ) + 100 ) , e2 , rxTestScheduler ) ) . toBe ( expected ) ;
127
+ } ) ;
128
+
129
+ it ( 'should timeout specified Date after specified Date then never completes if other source does not complete' , function ( ) {
130
+ var e1 = hot ( '---a---b---c---d---e---|' ) ;
131
+ var e2 = cold ( '-' )
132
+ var expected = '---a---b--' ;
133
+
134
+ expectObservable ( e1 . timeoutWith ( new Date ( Date . now ( ) + 100 ) , e2 , rxTestScheduler ) ) . toBe ( expected ) ;
135
+ } ) ;
23
136
} ) ;
0 commit comments