@@ -12,7 +12,7 @@ describe("partitionByKey", () => {
12
12
describe ( "activeKeys$" , ( ) => {
13
13
it ( "emits a list with all the active keys" , ( ) => {
14
14
scheduler ( ) . run ( ( { expectObservable, cold } ) => {
15
- const source = cold ( "-ab-- -cd---" )
15
+ const source = cold ( "-ab-a -cd---" )
16
16
const expectedStr = "efg---hi---"
17
17
const [ , result ] = partitionByKey (
18
18
source ,
@@ -30,14 +30,31 @@ describe("partitionByKey", () => {
30
30
} )
31
31
} )
32
32
33
+ it ( "emits all the synchronous groups in a single emission" , ( ) => {
34
+ scheduler ( ) . run ( ( { expectObservable, cold } ) => {
35
+ const source = concat ( of ( "a" , "b" ) , cold ( "--c--" ) )
36
+ const expectedStr = " g-h--"
37
+ const [ , result ] = partitionByKey (
38
+ source ,
39
+ ( v ) => v ,
40
+ ( ) => NEVER ,
41
+ )
42
+
43
+ expectObservable ( result ) . toBe ( expectedStr , {
44
+ g : [ "a" , "b" ] ,
45
+ h : [ "a" , "b" , "c" ] ,
46
+ } )
47
+ } )
48
+ } )
49
+
33
50
it ( "removes a key from the list when its inner stream completes" , ( ) => {
34
51
scheduler ( ) . run ( ( { expectObservable, cold } ) => {
35
52
const source = cold ( "-ab---c--" )
36
53
const a = cold ( " --1---2-" )
37
54
const b = cold ( " ---|" )
38
55
const c = cold ( " 1-|" )
39
56
const expectedStr = "efg--hi-j"
40
- const innerStreams : Record < string , Observable < any > > = { a, b, c }
57
+ const innerStreams : Record < string , Observable < string > > = { a, b, c }
41
58
const [ , result ] = partitionByKey (
42
59
source ,
43
60
( v ) => v ,
@@ -65,7 +82,7 @@ describe("partitionByKey", () => {
65
82
const a = cold ( " --1---2-|" )
66
83
const b = cold ( " ---|" )
67
84
const expectedStr = "efg--h---(i|)"
68
- const innerStreams = { a, b }
85
+ const innerStreams : Record < string , Observable < string > > = { a, b }
69
86
const [ , result ] = partitionByKey (
70
87
source ,
71
88
( v ) => v ,
@@ -85,6 +102,84 @@ describe("partitionByKey", () => {
85
102
} )
86
103
} )
87
104
} )
105
+
106
+ it ( "completes when no key is alive and the source completes" , ( ) => {
107
+ scheduler ( ) . run ( ( { expectObservable, cold } ) => {
108
+ const source = cold ( "-ab---|" )
109
+ const a = cold ( " --1|" )
110
+ const b = cold ( " ---|" )
111
+ const expectedStr = "efg-hi|"
112
+ const innerStreams : Record < string , Observable < string > > = { a, b }
113
+ const [ , result ] = partitionByKey (
114
+ source ,
115
+ ( v ) => v ,
116
+ ( v$ ) =>
117
+ v$ . pipe (
118
+ take ( 1 ) ,
119
+ switchMap ( ( v ) => innerStreams [ v ] ) ,
120
+ ) ,
121
+ )
122
+
123
+ expectObservable ( result ) . toBe ( expectedStr , {
124
+ e : [ ] ,
125
+ f : [ "a" ] ,
126
+ g : [ "a" , "b" ] ,
127
+ h : [ "b" ] ,
128
+ i : [ ] ,
129
+ } )
130
+ } )
131
+ } )
132
+
133
+ it ( "errors when the source emits an error" , ( ) => {
134
+ scheduler ( ) . run ( ( { expectObservable, cold } ) => {
135
+ const source = cold ( "-ab--#" )
136
+ const a = cold ( " --1---2" )
137
+ const b = cold ( " ------" )
138
+ const expectedStr = "efg--#"
139
+ const innerStreams : Record < string , Observable < string > > = { a, b }
140
+ const [ , result ] = partitionByKey (
141
+ source ,
142
+ ( v ) => v ,
143
+ ( v$ ) =>
144
+ v$ . pipe (
145
+ take ( 1 ) ,
146
+ switchMap ( ( v ) => innerStreams [ v ] ) ,
147
+ ) ,
148
+ )
149
+
150
+ expectObservable ( result ) . toBe ( expectedStr , {
151
+ e : [ ] ,
152
+ f : [ "a" ] ,
153
+ g : [ "a" , "b" ] ,
154
+ } )
155
+ } )
156
+ } )
157
+
158
+ it ( "removes a key when its inner stream emits an error" , ( ) => {
159
+ scheduler ( ) . run ( ( { expectObservable, cold } ) => {
160
+ const source = cold ( "-ab-----" )
161
+ const a = cold ( " --1-#" )
162
+ const b = cold ( " ------" )
163
+ const expectedStr = "efg--h"
164
+ const innerStreams : Record < string , Observable < string > > = { a, b }
165
+ const [ , result ] = partitionByKey (
166
+ source ,
167
+ ( v ) => v ,
168
+ ( v$ ) =>
169
+ v$ . pipe (
170
+ take ( 1 ) ,
171
+ switchMap ( ( v ) => innerStreams [ v ] ) ,
172
+ ) ,
173
+ )
174
+
175
+ expectObservable ( result ) . toBe ( expectedStr , {
176
+ e : [ ] ,
177
+ f : [ "a" ] ,
178
+ g : [ "a" , "b" ] ,
179
+ h : [ "b" ] ,
180
+ } )
181
+ } )
182
+ } )
88
183
} )
89
184
90
185
describe ( "getInstance$" , ( ) => {
@@ -98,7 +193,7 @@ describe("partitionByKey", () => {
98
193
const expectB = " -----|"
99
194
const expectC = " ------1-|"
100
195
101
- const innerStreams : Record < string , Observable < any > > = { a, b, c }
196
+ const innerStreams : Record < string , Observable < string > > = { a, b, c }
102
197
const [ getInstance$ ] = partitionByKey (
103
198
source ,
104
199
( v ) => v ,
0 commit comments