@@ -4,6 +4,7 @@ import { WasapPageStateHandler } from './WasapPageStateHandler';
44import { covidResistanceMutations } from '../../components/views/wasap/resistanceMutations' ;
55import {
66 VARIANT_TIME_FRAME ,
7+ type WasapCollectionFilter ,
78 type WasapManualFilter ,
89 type WasapPageConfig ,
910 type WasapResistanceFilter ,
@@ -67,6 +68,19 @@ const config: WasapPageConfig = {
6768 } ,
6869} ;
6970
71+ const configWithCollection : WasapPageConfig = {
72+ ...config ,
73+ collectionAnalysisModeEnabled : true ,
74+ collectionsApiBaseUrl : 'https://collections.example.org' ,
75+ filterDefaults : {
76+ ...config . filterDefaults ,
77+ collection : {
78+ mode : 'collection' ,
79+ collectionId : undefined ,
80+ } ,
81+ } ,
82+ } ;
83+
7084describe ( 'WasapPageStateHandler' , ( ) => {
7185 const handler = new WasapPageStateHandler ( config ) ;
7286
@@ -322,4 +336,69 @@ describe('WasapPageStateHandler', () => {
322336 expect ( analysis2 . excludeVariants ) . toEqual ( analysis1 . excludeVariants ) ;
323337 } ) ;
324338 } ) ;
339+
340+ describe ( 'collection mode' , ( ) => {
341+ const handlerWithCollection = new WasapPageStateHandler ( configWithCollection ) ;
342+
343+ it ( 'throws error when feature is disabled' , ( ) => {
344+ const url = '/wastewater/covid?analysisMode=collection&collectionId=123&' ;
345+ expect ( ( ) => handler . parsePageStateFromUrl ( new URL ( `http://example.com${ url } ` ) ) ) . toThrow (
346+ "The 'collection' analysis mode is not enabled." ,
347+ ) ;
348+ } ) ;
349+
350+ it ( 'parses and encodes collection filter with collectionId' , ( ) => {
351+ const url =
352+ '/wastewater/covid?' +
353+ 'locationName=Z%C3%BCrich+%28ZH%29&' +
354+ 'granularity=day&' +
355+ 'analysisMode=collection&' +
356+ 'collectionId=123&' ;
357+ const filter = handlerWithCollection . parsePageStateFromUrl ( new URL ( `http://example.com${ url } ` ) ) ;
358+
359+ expect ( filter . analysis . mode ) . toBe ( 'collection' ) ;
360+ const analysis = filter . analysis as WasapCollectionFilter ;
361+ expect ( analysis . collectionId ) . toBe ( 123 ) ;
362+
363+ const newUrl = handlerWithCollection . toUrl ( filter ) ;
364+ expect ( newUrl ) . toBe ( url ) ;
365+ } ) ;
366+
367+ it ( 'parses collection filter without collectionId' , ( ) => {
368+ const url = '/wastewater/covid?analysisMode=collection&' ;
369+ const filter = handlerWithCollection . parsePageStateFromUrl ( new URL ( `http://example.com${ url } ` ) ) ;
370+
371+ expect ( filter . analysis . mode ) . toBe ( 'collection' ) ;
372+ const analysis = filter . analysis as WasapCollectionFilter ;
373+ expect ( analysis . collectionId ) . toBeUndefined ( ) ;
374+ } ) ;
375+
376+ it ( 'encodes collection filter omits undefined collectionId' , ( ) => {
377+ const url = '/wastewater/covid?analysisMode=collection&' ;
378+ const filter = handlerWithCollection . parsePageStateFromUrl ( new URL ( `http://example.com${ url } ` ) ) ;
379+
380+ const encodedUrl = handlerWithCollection . toUrl ( filter ) ;
381+ expect ( encodedUrl ) . not . toContain ( 'collectionId' ) ;
382+ } ) ;
383+
384+ it ( 'converts collectionId string to number' , ( ) => {
385+ const url = '/wastewater/covid?analysisMode=collection&collectionId=456&' ;
386+ const filter = handlerWithCollection . parsePageStateFromUrl ( new URL ( `http://example.com${ url } ` ) ) ;
387+
388+ const analysis = filter . analysis as WasapCollectionFilter ;
389+ expect ( typeof analysis . collectionId ) . toBe ( 'number' ) ;
390+ expect ( analysis . collectionId ) . toBe ( 456 ) ;
391+ } ) ;
392+
393+ it ( 'collection mode round-trip preserves collectionId' , ( ) => {
394+ const url = '/wastewater/covid?analysisMode=collection&collectionId=789&' ;
395+ const filter1 = handlerWithCollection . parsePageStateFromUrl ( new URL ( `http://example.com${ url } ` ) ) ;
396+ const url2 = handlerWithCollection . toUrl ( filter1 ) ;
397+ const filter2 = handlerWithCollection . parsePageStateFromUrl ( new URL ( `http://example.com${ url2 } ` ) ) ;
398+
399+ const analysis1 = filter1 . analysis as WasapCollectionFilter ;
400+ const analysis2 = filter2 . analysis as WasapCollectionFilter ;
401+ expect ( analysis2 . collectionId ) . toBe ( analysis1 . collectionId ) ;
402+ } ) ;
403+ } ) ;
325404} ) ;
0 commit comments