@@ -10,7 +10,7 @@ import { toCamel } from 'convert-keys'
1010import { renderHook , act } from '@testing-library/react-hooks'
1111import mockConsole from 'jest-mock-console'
1212import * as mockdate from 'mockdate'
13- import defaults from '../defaults'
13+ import defaults , { useFetchArgsDefaults } from '../defaults'
1414
1515import { Res , IncomingOptions , CachePolicies } from '../types'
1616import { emptyCustomResponse , sleep , makeError , addSlash } from '../utils'
@@ -581,10 +581,25 @@ describe('useFetch - BROWSER - interceptors', (): void => {
581581 )
582582 }
583583
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+
584598 afterEach ( ( ) : void => {
585599 fetch . resetMocks ( )
586600 cleanup ( )
587601 request . mockClear ( )
602+ response . mockClear ( )
588603 } )
589604
590605 beforeEach ( ( ) : void => {
@@ -643,6 +658,44 @@ describe('useFetch - BROWSER - interceptors', (): void => {
643658 expect ( request . mock . calls [ 0 ] [ 0 ] . url ) . toBe ( 'https://example.com' )
644659 } )
645660
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+
646699 it ( 'should still call both interceptors when using cache' , async ( ) : Promise < void > => {
647700 let requestCalled = 0
648701 let responseCalled = 0
0 commit comments