@@ -542,7 +542,7 @@ test('ky.create() does not mangle search params', async t => {
542
542
await server . close ( ) ;
543
543
} ) ;
544
544
545
- test ( 'ky.extend()' , async t => {
545
+ const extendHooksMacro = test . macro < [ { useFunction : boolean } ] > ( async ( t , { useFunction } ) => {
546
546
const server = await createHttpTestServer ( ) ;
547
547
server . get ( '/' , ( _request , response ) => {
548
548
response . end ( ) ;
@@ -551,25 +551,28 @@ test('ky.extend()', async t => {
551
551
let isOriginBeforeRequestTrigged = false ;
552
552
let isExtendBeforeRequestTrigged = false ;
553
553
554
+ const intermediateOptions = {
555
+ hooks : {
556
+ beforeRequest : [
557
+ ( ) => {
558
+ isOriginBeforeRequestTrigged = true ;
559
+ } ,
560
+ ] ,
561
+ } ,
562
+ } ;
563
+ const extendedOptions = {
564
+ hooks : {
565
+ beforeRequest : [
566
+ ( ) => {
567
+ isExtendBeforeRequestTrigged = true ;
568
+ } ,
569
+ ] ,
570
+ } ,
571
+ } ;
572
+
554
573
const extended = ky
555
- . extend ( {
556
- hooks : {
557
- beforeRequest : [
558
- ( ) => {
559
- isOriginBeforeRequestTrigged = true ;
560
- } ,
561
- ] ,
562
- } ,
563
- } )
564
- . extend ( {
565
- hooks : {
566
- beforeRequest : [
567
- ( ) => {
568
- isExtendBeforeRequestTrigged = true ;
569
- } ,
570
- ] ,
571
- } ,
572
- } ) ;
574
+ . extend ( useFunction ? ( ) => intermediateOptions : intermediateOptions )
575
+ . extend ( useFunction ? ( ) => extendedOptions : extendedOptions ) ;
573
576
574
577
await extended ( server . url ) ;
575
578
@@ -582,6 +585,60 @@ test('ky.extend()', async t => {
582
585
await server . close ( ) ;
583
586
} ) ;
584
587
588
+ test ( 'ky.extend() appends hooks' , extendHooksMacro , { useFunction : false } ) ;
589
+
590
+ test ( 'ky.extend() with function appends hooks' , extendHooksMacro , { useFunction : false } ) ;
591
+
592
+ test ( 'ky.extend() with function overrides primitives in parent defaults' , async t => {
593
+ const server = await createHttpTestServer ( ) ;
594
+ server . get ( '*' , ( request , response ) => {
595
+ response . end ( request . url ) ;
596
+ } ) ;
597
+
598
+ const api = ky . create ( { prefixUrl : `${ server . url } /api` } ) ;
599
+ const usersApi = api . extend ( options => ( { prefixUrl : `${ options . prefixUrl ! . toString ( ) } /users` } ) ) ;
600
+
601
+ t . is ( await usersApi . get ( '123' ) . text ( ) , '/api/users/123' ) ;
602
+ t . is ( await api . get ( 'version' ) . text ( ) , '/api/version' ) ;
603
+
604
+ {
605
+ const { ok} = await api . head ( server . url ) ;
606
+ t . true ( ok ) ;
607
+ }
608
+
609
+ {
610
+ const { ok} = await usersApi . head ( server . url ) ;
611
+ t . true ( ok ) ;
612
+ }
613
+
614
+ await server . close ( ) ;
615
+ } ) ;
616
+
617
+ test ( 'ky.extend() with function retains parent defaults when not specified' , async t => {
618
+ const server = await createHttpTestServer ( ) ;
619
+ server . get ( '*' , ( request , response ) => {
620
+ response . end ( request . url ) ;
621
+ } ) ;
622
+
623
+ const api = ky . create ( { prefixUrl : `${ server . url } /api` } ) ;
624
+ const extendedApi = api . extend ( ( ) => ( { } ) ) ;
625
+
626
+ t . is ( await api . get ( 'version' ) . text ( ) , '/api/version' ) ;
627
+ t . is ( await extendedApi . get ( 'something' ) . text ( ) , '/api/something' ) ;
628
+
629
+ {
630
+ const { ok} = await api . head ( server . url ) ;
631
+ t . true ( ok ) ;
632
+ }
633
+
634
+ {
635
+ const { ok} = await extendedApi . head ( server . url ) ;
636
+ t . true ( ok ) ;
637
+ }
638
+
639
+ await server . close ( ) ;
640
+ } ) ;
641
+
585
642
test ( 'throws DOMException/Error with name AbortError when aborted by user' , async t => {
586
643
const server = await createHttpTestServer ( ) ;
587
644
// eslint-disable-next-line @typescript-eslint/no-empty-function
0 commit comments