@@ -10,7 +10,7 @@ import { toCamel } from 'convert-keys'
10
10
import { renderHook , act } from '@testing-library/react-hooks'
11
11
import mockConsole from 'jest-mock-console'
12
12
import * as mockdate from 'mockdate'
13
- import defaults from '../defaults'
13
+ import defaults , { useFetchArgsDefaults } from '../defaults'
14
14
15
15
import { Res , IncomingOptions , CachePolicies } from '../types'
16
16
import { emptyCustomResponse , sleep , makeError , addSlash } from '../utils'
@@ -581,10 +581,25 @@ describe('useFetch - BROWSER - interceptors', (): void => {
581
581
)
582
582
}
583
583
584
+ const response = jest . fn ( ( { response : res } ) => res )
585
+ const mockResInterceptorWrapper = ( { children } : { children ?: ReactNode } ) : ReactElement => {
586
+ const options : IncomingOptions = {
587
+ interceptors : {
588
+ request,
589
+ response
590
+ } ,
591
+ cachePolicy : NO_CACHE
592
+ }
593
+ return (
594
+ < Provider url = 'https://example.com' options = { options } > { children } </ Provider >
595
+ )
596
+ }
597
+
584
598
afterEach ( ( ) : void => {
585
599
fetch . resetMocks ( )
586
600
cleanup ( )
587
601
request . mockClear ( )
602
+ response . mockClear ( )
588
603
} )
589
604
590
605
beforeEach ( ( ) : void => {
@@ -643,6 +658,44 @@ describe('useFetch - BROWSER - interceptors', (): void => {
643
658
expect ( request . mock . calls [ 0 ] [ 0 ] . url ) . toBe ( 'https://example.com' )
644
659
} )
645
660
661
+ it ( 'should pass the proper request to `interceptors.response`' , async ( ) : Promise < void > => {
662
+ const { result } = renderHook (
663
+ ( ) => useFetch ( ) ,
664
+ { wrapper : mockResInterceptorWrapper }
665
+ )
666
+ await act ( result . current . get )
667
+ expect ( fetch . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'https://example.com' )
668
+ expect ( request . mock . calls [ 0 ] [ 0 ] . url ) . toBe ( 'https://example.com' )
669
+ expect ( response . mock . calls [ 0 ] [ 0 ] . request ) . toStrictEqual ( useFetchArgsDefaults . requestInit )
670
+ } )
671
+
672
+ it ( 'should pass custom request options to `interceptors.response`' , async ( ) : Promise < void > => {
673
+ const customReqOptions : RequestInit = {
674
+ headers : {
675
+ Authorization : 'Bearer TOKEN'
676
+ } ,
677
+ credentials : 'include' ,
678
+ cache : 'no-store'
679
+ }
680
+ const { result } = renderHook (
681
+ ( ) => useFetch ( 'https://custom-url.com' , customReqOptions ) ,
682
+ { wrapper : mockResInterceptorWrapper }
683
+ )
684
+ await act ( result . current . get )
685
+ expect ( fetch . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'https://custom-url.com' )
686
+ expect ( request . mock . calls [ 0 ] [ 0 ] . url ) . toBe ( 'https://custom-url.com' )
687
+
688
+ const expectedOpts = {
689
+ headers : {
690
+ ...useFetchArgsDefaults . requestInit . headers ,
691
+ ...customReqOptions . headers
692
+ } ,
693
+ credentials : customReqOptions . credentials ,
694
+ cache : customReqOptions . cache
695
+ }
696
+ expect ( response . mock . calls [ 0 ] [ 0 ] . request ) . toStrictEqual ( expectedOpts )
697
+ } )
698
+
646
699
it ( 'should still call both interceptors when using cache' , async ( ) : Promise < void > => {
647
700
let requestCalled = 0
648
701
let responseCalled = 0
0 commit comments