@@ -166,7 +166,7 @@ describe("GET /subscription/", () => {
166166 expect ( response . body ) . toEqual ( [ ] ) ;
167167 } ) ;
168168
169- it ( "should return all subscriptions" , async ( ) => {
169+ it ( "should return aggregated subscriptions by mailing list " , async ( ) => {
170170 await SupabaseDB . SUBSCRIPTIONS . insert ( [
171171 { userId : USER_ID_1 , mailingList : VALID_mailingList } ,
172172 { userId : USER_ID_2 , mailingList : VALID_mailingList } ,
@@ -175,14 +175,47 @@ describe("GET /subscription/", () => {
175175 StatusCodes . OK
176176 ) ;
177177
178+ expect ( response . body . length ) . toBe ( 1 ) ;
179+ expect ( response . body [ 0 ] ) . toEqual ( {
180+ mailingList : VALID_mailingList ,
181+ userIds : expect . arrayContaining ( [ USER_ID_1 , USER_ID_2 ] ) ,
182+ } ) ;
183+ expect ( response . body [ 0 ] . userIds . length ) . toBe ( 2 ) ;
184+ } ) ;
185+
186+ it ( "should aggregate multiple mailing lists correctly" , async ( ) => {
187+ const mailingList2 = "newsletter" ;
188+ await SupabaseDB . SUBSCRIPTIONS . insert ( [
189+ { userId : USER_ID_1 , mailingList : VALID_mailingList } ,
190+ { userId : USER_ID_2 , mailingList : VALID_mailingList } ,
191+ { userId : USER_ID_1 , mailingList : mailingList2 } ,
192+ ] ) . throwOnError ( ) ;
193+
194+ const response = await getAsAdmin ( "/subscription/" ) . expect (
195+ StatusCodes . OK
196+ ) ;
197+
178198 expect ( response . body . length ) . toBe ( 2 ) ;
179199
180- expect ( response . body ) . toEqual (
181- expect . arrayContaining ( [
182- { userId : USER_ID_1 , mailingList : VALID_mailingList } ,
183- { userId : USER_ID_2 , mailingList : VALID_mailingList } ,
184- ] )
200+ const validMailingListEntry = response . body . find (
201+ ( item : { mailingList : string ; userIds : string [ ] } ) =>
202+ item . mailingList === VALID_mailingList
203+ ) ;
204+ const newsletterEntry = response . body . find (
205+ ( item : { mailingList : string ; userIds : string [ ] } ) =>
206+ item . mailingList === mailingList2
185207 ) ;
208+
209+ expect ( validMailingListEntry ) . toEqual ( {
210+ mailingList : VALID_mailingList ,
211+ userIds : expect . arrayContaining ( [ USER_ID_1 , USER_ID_2 ] ) ,
212+ } ) ;
213+ expect ( validMailingListEntry . userIds . length ) . toBe ( 2 ) ;
214+
215+ expect ( newsletterEntry ) . toEqual ( {
216+ mailingList : mailingList2 ,
217+ userIds : [ USER_ID_1 ] ,
218+ } ) ;
186219 } ) ;
187220} ) ;
188221
0 commit comments