@@ -521,6 +521,83 @@ describe("FetchHttpApi", () => {
521
521
describe ( "when fetch.opts.baseUrl does have a trailing slash" , ( ) => {
522
522
runTests ( baseUrlWithTrailingSlash ) ;
523
523
} ) ;
524
+
525
+ describe ( "extraParams handling" , ( ) => {
526
+ const makeApiWithExtraParams = ( extraParams : QueryDict ) : FetchHttpApi < any > => {
527
+ const fetchFn = jest . fn ( ) ;
528
+ const emitter = new TypedEventEmitter < HttpApiEvent , HttpApiEventHandlerMap > ( ) ;
529
+ return new FetchHttpApi ( emitter , { baseUrl : localBaseUrl , prefix, fetchFn, extraParams } ) ;
530
+ } ;
531
+
532
+ const userId = "@rsb-tbg:localhost" ;
533
+ const encodedUserId = encodeURIComponent ( userId ) ;
534
+
535
+ it ( "should include extraParams in URL when no queryParams provided" , ( ) => {
536
+ const extraParams = { user_id : userId , version : "1.0" } ;
537
+ const api = makeApiWithExtraParams ( extraParams ) ;
538
+
539
+ const result = api . getUrl ( "/test" ) ;
540
+ expect ( result . toString ( ) ) . toBe ( `${ localBaseUrl } ${ prefix } /test?user_id=${ encodedUserId } &version=1.0` ) ;
541
+ } ) ;
542
+
543
+ it ( "should merge extraParams with queryParams" , ( ) => {
544
+ const extraParams = { user_id : userId , version : "1.0" } ;
545
+ const api = makeApiWithExtraParams ( extraParams ) ;
546
+
547
+ const queryParams = { userId : "123" , filter : "active" } ;
548
+ const result = api . getUrl ( "/test" , queryParams ) ;
549
+
550
+ expect ( result . searchParams . get ( "user_id" ) ! ) . toBe ( userId ) ;
551
+ expect ( result . searchParams . get ( "version" ) ! ) . toBe ( "1.0" ) ;
552
+ expect ( result . searchParams . get ( "userId" ) ! ) . toBe ( "123" ) ;
553
+ expect ( result . searchParams . get ( "filter" ) ! ) . toBe ( "active" ) ;
554
+ } ) ;
555
+
556
+ it ( "should allow queryParams to override extraParams" , ( ) => {
557
+ const extraParams = { user_id : "@default:localhost" , version : "1.0" } ;
558
+ const api = makeApiWithExtraParams ( extraParams ) ;
559
+
560
+ const queryParams = { user_id : "@override:localhost" , userId : "123" } ;
561
+ const result = api . getUrl ( "/test" , queryParams ) ;
562
+
563
+ expect ( result . searchParams . get ( "user_id" ) ) . toBe ( "@override:localhost" ) ;
564
+ expect ( result . searchParams . get ( "version" ) ! ) . toBe ( "1.0" ) ;
565
+ expect ( result . searchParams . get ( "userId" ) ! ) . toBe ( "123" ) ;
566
+ } ) ;
567
+
568
+ it ( "should handle empty extraParams" , ( ) => {
569
+ const extraParams = { } ;
570
+ const api = makeApiWithExtraParams ( extraParams ) ;
571
+
572
+ const queryParams = { userId : "123" } ;
573
+ const result = api . getUrl ( "/test" , queryParams ) ;
574
+
575
+ expect ( result . searchParams . get ( "userId" ) ! ) . toBe ( "123" ) ;
576
+ expect ( result . searchParams . has ( "user_id" ) ) . toBe ( false ) ;
577
+ } ) ;
578
+
579
+ it ( "should work when extraParams is undefined" , ( ) => {
580
+ const fetchFn = jest . fn ( ) ;
581
+ const emitter = new TypedEventEmitter < HttpApiEvent , HttpApiEventHandlerMap > ( ) ;
582
+ const api = new FetchHttpApi ( emitter , { baseUrl : localBaseUrl , prefix, fetchFn } ) ;
583
+
584
+ const queryParams = { userId : "123" } ;
585
+ const result = api . getUrl ( "/test" , queryParams ) ;
586
+
587
+ expect ( result . searchParams . get ( "userId" ) ! ) . toBe ( "123" ) ;
588
+ expect ( result . toString ( ) ) . toBe ( `${ localBaseUrl } ${ prefix } /test?userId=123` ) ;
589
+ } ) ;
590
+
591
+ it ( "should work when queryParams is undefined" , ( ) => {
592
+ const extraParams = { user_id : userId , version : "1.0" } ;
593
+ const api = makeApiWithExtraParams ( extraParams ) ;
594
+
595
+ const result = api . getUrl ( "/test" ) ;
596
+
597
+ expect ( result . searchParams . get ( "user_id" ) ! ) . toBe ( userId ) ;
598
+ expect ( result . toString ( ) ) . toBe ( `${ localBaseUrl } ${ prefix } /test?user_id=${ encodedUserId } &version=1.0` ) ;
599
+ } ) ;
600
+ } ) ;
524
601
} ) ;
525
602
526
603
it ( "should not log query parameters" , async ( ) => {
0 commit comments