1
+ /* globals describe, it, expect, expectObservable, expectSubscriptions, cold, hot */
2
+ var Rx = require ( '../../dist/cjs/Rx' ) ;
3
+ var Observable = Rx . Observable ;
4
+
5
+ describe ( 'Observable.prototype.distinctKey()' , function ( ) {
6
+ it . asDiagram ( 'distinctKey(\'k\')' ) ( 'should distinguish between values' , function ( ) {
7
+ var values = { a : { k : 1 } , b : { k : 2 } , c : { k : 3 } } ;
8
+ var e1 = hot ( '-a--b-b----a-c-|' , values ) ;
9
+ var expected = '-a--b--------c-|' ;
10
+
11
+ var result = e1 . distinctKey ( 'k' ) ;
12
+
13
+ expectObservable ( result ) . toBe ( expected , values ) ;
14
+ } ) ;
15
+
16
+ it ( 'should distinguish between values' , function ( ) {
17
+ var values = { a : { val : 1 } , b : { val : 2 } } ;
18
+ var e1 = hot ( '--a--a--a--b--b--a--|' , values ) ;
19
+ var e1subs = '^ !' ;
20
+ var expected = '--a--------b--------|' ;
21
+
22
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected , values ) ;
23
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
24
+ } ) ;
25
+
26
+ it ( 'should distinguish between values and does not completes' , function ( ) {
27
+ var values = { a : { val : 1 } , b : { val : 2 } } ;
28
+ var e1 = hot ( '--a--a--a--b--b--a-' , values ) ;
29
+ var e1subs = '^ ' ;
30
+ var expected = '--a--------b-------' ;
31
+
32
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected , values ) ;
33
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
34
+ } ) ;
35
+
36
+ it ( 'should distinguish between values with key' , function ( ) {
37
+ var values = { a : { val : 1 } , b : { valOther : 1 } , c : { valOther : 3 } , d : { val : 1 } , e : { val : 5 } } ;
38
+ var e1 = hot ( '--a--b--c--d--e--|' , values ) ;
39
+ var e1subs = '^ !' ;
40
+ var expected = '--a--b--------e--|' ;
41
+
42
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected , values ) ;
43
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
44
+ } ) ;
45
+
46
+ it ( 'should not compare if source does not have element with key' , function ( ) {
47
+ var values = { a : { valOther : 1 } , b : { valOther : 1 } , c : { valOther : 3 } , d : { valOther : 1 } , e : { valOther : 5 } } ;
48
+ var e1 = hot ( '--a--b--c--d--e--|' , values ) ;
49
+ var e1subs = '^ !' ;
50
+ var expected = '--a--------------|' ;
51
+
52
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected , values ) ;
53
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
54
+ } ) ;
55
+
56
+ it ( 'should not completes if source never completes' , function ( ) {
57
+ var e1 = cold ( '-' ) ;
58
+ var e1subs = '^' ;
59
+ var expected = '-' ;
60
+
61
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected ) ;
62
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
63
+ } ) ;
64
+
65
+ it ( 'should not completes if source does not completes' , function ( ) {
66
+ var e1 = hot ( '-' ) ;
67
+ var e1subs = '^' ;
68
+ var expected = '-' ;
69
+
70
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected ) ;
71
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
72
+ } ) ;
73
+
74
+ it ( 'should complete if source is empty' , function ( ) {
75
+ var e1 = cold ( '|' ) ;
76
+ var e1subs = '(^!)' ;
77
+ var expected = '|' ;
78
+
79
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected ) ;
80
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
81
+ } ) ;
82
+
83
+ it ( 'should complete if source does not emit' , function ( ) {
84
+ var e1 = hot ( '------|' ) ;
85
+ var e1subs = '^ !' ;
86
+ var expected = '------|' ;
87
+
88
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected ) ;
89
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
90
+ } ) ;
91
+
92
+ it ( 'should emit if source emits single element only' , function ( ) {
93
+ var values = { a : { val : 1 } } ;
94
+ var e1 = hot ( '--a--|' , values ) ;
95
+ var e1subs = '^ !' ;
96
+ var expected = '--a--|' ;
97
+
98
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected , values ) ;
99
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
100
+ } ) ;
101
+
102
+ it ( 'should emit if source is scalar' , function ( ) {
103
+ var values = { a : { val : 1 } } ;
104
+ var e1 = Observable . of ( values . a ) ;
105
+ var expected = '(a|)' ;
106
+
107
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected , values ) ;
108
+ } ) ;
109
+
110
+ it ( 'should raises error if source raises error' , function ( ) {
111
+ var values = { a : { val : 1 } } ;
112
+ var e1 = hot ( '--a--a--#' , values ) ;
113
+ var e1subs = '^ !' ;
114
+ var expected = '--a-----#' ;
115
+
116
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected , values ) ;
117
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
118
+ } ) ;
119
+
120
+ it ( 'should raises error if source throws' , function ( ) {
121
+ var e1 = cold ( '#' ) ;
122
+ var e1subs = '(^!)' ;
123
+ var expected = '#' ;
124
+
125
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected ) ;
126
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
127
+ } ) ;
128
+
129
+ it ( 'should not omit if source elements are all different' , function ( ) {
130
+ var values = { a : { val : 1 } , b : { val : 2 } , c : { val : 3 } , d : { val : 4 } , e : { val : 5 } } ;
131
+ var e1 = hot ( '--a--b--c--d--e--|' , values ) ;
132
+ var e1subs = '^ !' ;
133
+ var expected = '--a--b--c--d--e--|' ;
134
+
135
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected , values ) ;
136
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
137
+ } ) ;
138
+
139
+ it ( 'should allow unsubscribing early and explicitly' , function ( ) {
140
+ var values = { a : { val : 1 } , b : { val : 2 } , c : { val : 3 } , d : { val : 4 } , e : { val : 5 } } ;
141
+ var e1 = hot ( '--a--b--b--d--a--e--|' , values ) ;
142
+ var e1subs = '^ ! ' ;
143
+ var expected = '--a--b----- ' ;
144
+ var unsub = ' ! ' ;
145
+
146
+ var result = e1 . distinctKey ( 'val' ) ;
147
+
148
+ expectObservable ( result , unsub ) . toBe ( expected , values ) ;
149
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
150
+ } ) ;
151
+
152
+ it ( 'should not break unsubscription chains when unsubscribed explicitly' , function ( ) {
153
+ var values = { a : { val : 1 } , b : { val : 2 } , c : { val : 3 } , d : { val : 4 } , e : { val : 5 } } ;
154
+ var e1 = hot ( '--a--b--b--d--a--e--|' , values ) ;
155
+ var e1subs = '^ ! ' ;
156
+ var expected = '--a--b----- ' ;
157
+ var unsub = ' ! ' ;
158
+
159
+ var result = e1
160
+ . mergeMap ( function ( x ) { return Observable . of ( x ) ; } )
161
+ . distinctKey ( 'val' )
162
+ . mergeMap ( function ( x ) { return Observable . of ( x ) ; } ) ;
163
+
164
+ expectObservable ( result , unsub ) . toBe ( expected , values ) ;
165
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
166
+ } ) ;
167
+
168
+ it ( 'should emit once if source elements are all same' , function ( ) {
169
+ var values = { a : { val : 1 } } ;
170
+ var e1 = hot ( '--a--a--a--a--a--a--|' , values ) ;
171
+ var e1subs = '^ !' ;
172
+ var expected = '--a-----------------|' ;
173
+
174
+ expectObservable ( e1 . distinctKey ( 'val' ) ) . toBe ( expected , values ) ;
175
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
176
+ } ) ;
177
+
178
+ it ( 'should emit once if comparer returns true always regardless of source emits' , function ( ) {
179
+ var values = { a : { val : 1 } , b : { val : 2 } , c : { val : 3 } , d : { val : 4 } , e : { val : 5 } } ;
180
+ var e1 = hot ( '--a--b--c--d--e--|' , values ) ;
181
+ var e1subs = '^ !' ;
182
+ var expected = '--a--------------|' ;
183
+
184
+ expectObservable ( e1 . distinctKey ( 'val' , function ( ) { return true ; } ) ) . toBe ( expected , values ) ;
185
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
186
+ } ) ;
187
+
188
+ it ( 'should emit all if comparer returns false always regardless of source emits' , function ( ) {
189
+ var values = { a : { val : 1 } } ;
190
+ var e1 = hot ( '--a--a--a--a--a--a--|' , values ) ;
191
+ var e1subs = '^ !' ;
192
+ var expected = '--a--a--a--a--a--a--|' ;
193
+
194
+ expectObservable ( e1 . distinctKey ( 'val' , function ( ) { return false ; } ) ) . toBe ( expected , values ) ;
195
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
196
+ } ) ;
197
+
198
+ it ( 'should distinguish values by selector' , function ( ) {
199
+ var values = { a : { val : 1 } , b : { val : 2 } , c : { val : 3 } , d : { val : 4 } , e : { val : 5 } } ;
200
+ var e1 = hot ( '--a--b--c--d--e--|' , values ) ;
201
+ var e1subs = '^ !' ;
202
+ var expected = '--a-----c-----e--|' ;
203
+ var selector = function ( x , y ) {
204
+ return y % 2 === 0 ;
205
+ } ;
206
+
207
+ expectObservable ( e1 . distinctKey ( 'val' , selector ) ) . toBe ( expected , values ) ;
208
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
209
+ } ) ;
210
+
211
+ it ( 'should raises error when comparer throws' , function ( ) {
212
+ var values = { a : { val : 1 } , b : { val : 2 } , c : { val : 3 } , d : { val : 4 } , e : { val : 5 } } ;
213
+ var e1 = hot ( '--a--b--c--d--e--|' , values ) ;
214
+ var e1subs = '^ ! ' ;
215
+ var expected = '--a--b--c--# ' ;
216
+ var selector = function ( x , y ) {
217
+ if ( y === 4 ) {
218
+ throw 'error' ;
219
+ }
220
+ return x === y ;
221
+ } ;
222
+
223
+ expectObservable ( e1 . distinctKey ( 'val' , selector ) ) . toBe ( expected , values ) ;
224
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
225
+ } ) ;
226
+
227
+ it ( 'should support a flushing stream' , function ( ) {
228
+ var values = { a : { val : 1 } , b : { val : 2 } , c : { val : 3 } , d : { val : 4 } , e : { val : 5 } } ;
229
+ var e1 = hot ( '--a--b--a--b--a--b--|' , values ) ;
230
+ var e1subs = '^ !' ;
231
+ var e2 = hot ( '-----------x--------|' ) ;
232
+ var e2subs = '^ !' ;
233
+ var expected = '--a--b--------a--b--|' ;
234
+ var selector = function ( x , y ) {
235
+ return x === y ;
236
+ } ;
237
+
238
+ expectObservable ( e1 . distinct ( selector , e2 ) ) . toBe ( expected , values ) ;
239
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
240
+ expectSubscriptions ( e2 . subscriptions ) . toBe ( e2subs ) ;
241
+ } ) ;
242
+
243
+ it ( 'should unsubscribe from the flushing stream when the main stream is unsubbed' , function ( ) {
244
+ var values = { a : { val : 1 } , b : { val : 2 } , c : { val : 3 } , d : { val : 4 } , e : { val : 5 } } ;
245
+ var e1 = hot ( '--a--b--a--b--a--b--|' , values ) ;
246
+ var e1subs = '^ ! ' ;
247
+ var e2 = hot ( '-----------x--------|' ) ;
248
+ var e2subs = '^ ! ' ;
249
+ var unsub = ' ! ' ;
250
+ var expected = '--a--b------' ;
251
+ var selector = function ( x , y ) {
252
+ return x === y ;
253
+ } ;
254
+
255
+ expectObservable ( e1 . distinct ( selector , e2 ) , unsub ) . toBe ( expected , values ) ;
256
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
257
+ expectSubscriptions ( e2 . subscriptions ) . toBe ( e2subs ) ;
258
+ } ) ;
259
+
260
+ it ( 'should allow opting in to default comparator with flush' , function ( ) {
261
+ var values = { a : { val : 1 } , b : { val : 2 } , c : { val : 3 } , d : { val : 4 } , e : { val : 5 } } ;
262
+ var e1 = hot ( '--a--b--a--b--a--b--|' , values ) ;
263
+ var e1subs = '^ !' ;
264
+ var e2 = hot ( '-----------x--------|' ) ;
265
+ var e2subs = '^ !' ;
266
+ var expected = '--a--b--------a--b--|' ;
267
+
268
+ expectObservable ( e1 . distinct ( null , e2 ) ) . toBe ( expected , values ) ;
269
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
270
+ expectSubscriptions ( e2 . subscriptions ) . toBe ( e2subs ) ;
271
+ } ) ;
272
+ } ) ;
0 commit comments