@@ -2,8 +2,8 @@ import { atom, computed } from "nanostores";
22import { afterEach , beforeAll , describe , expect , it , vi } from "vitest" ;
33// @ts -expect-error
44import createFetchMock from "vitest-fetch-mock" ;
5- import type { paths } from "../test/v1 .js" ;
6- import createClient from "./index .js" ;
5+ import createClient from "../src/index .js" ;
6+ import type { paths } from "./v1.d .js" ;
77
88const fetchMocker = createFetchMock ( vi ) ;
99
@@ -395,7 +395,7 @@ describe("client", () => {
395395 expect ( options ?. headers ) . toEqual ( new Headers ( ) ) ;
396396 } ) ;
397397
398- it ( "accepts a custom fetch function" , async ( ) => {
398+ it ( "accepts a custom fetch function on createClient " , async ( ) => {
399399 function createCustomFetch ( data : any ) {
400400 const response = {
401401 clone : ( ) => ( { ...response } ) ,
@@ -407,15 +407,48 @@ describe("client", () => {
407407 return async ( ) => Promise . resolve ( response ) ;
408408 }
409409
410- const baseData = { works : true } ;
411- const customBaseFetch = createCustomFetch ( baseData ) ;
412- const client = createClient < paths > ( { fetch : customBaseFetch } ) ;
413- expect ( ( await client . GET ( "/self" ) ) . data ) . toBe ( baseData ) ;
410+ const customFetch = createCustomFetch ( { works : true } ) ;
411+ mockFetchOnce ( { status : 200 , body : "{}" } ) ;
412+
413+ const client = createClient < paths > ( { fetch : customFetch } ) ;
414+ const { data } = await client . GET ( "/self" ) ;
415+
416+ // assert data was returned from custom fetcher
417+ expect ( data ) . toEqual ( { works : true } ) ;
418+
419+ // assert global fetch was never called
420+ expect ( fetchMocker ) . not . toHaveBeenCalled ( ) ;
421+ } ) ;
422+
423+ it ( "accepts a custom fetch function per-request" , async ( ) => {
424+ function createCustomFetch ( data : any ) {
425+ const response = {
426+ clone : ( ) => ( { ...response } ) ,
427+ headers : new Headers ( ) ,
428+ json : async ( ) => data ,
429+ status : 200 ,
430+ ok : true ,
431+ } as Response ;
432+ return async ( ) => Promise . resolve ( response ) ;
433+ }
434+
435+ const fallbackFetch = createCustomFetch ( { fetcher : "fallback" } ) ;
436+ const overrideFetch = createCustomFetch ( { fetcher : "override" } ) ;
437+
438+ mockFetchOnce ( { status : 200 , body : "{}" } ) ;
439+
440+ const client = createClient < paths > ( { fetch : fallbackFetch } ) ;
441+
442+ // assert override function was called
443+ const fetch1 = await client . GET ( "/self" , { fetch : overrideFetch } ) ;
444+ expect ( fetch1 . data ) . toEqual ( { fetcher : "override" } ) ;
445+
446+ // assert fallback function still persisted (and wasn’t overridden)
447+ const fetch2 = await client . GET ( "/self" ) ;
448+ expect ( fetch2 . data ) . toEqual ( { fetcher : "fallback" } ) ;
414449
415- const data = { result : "it's working" } ;
416- const customFetch = createCustomFetch ( data ) ;
417- const customResponse = await client . GET ( "/self" , { fetch : customFetch } ) ;
418- expect ( customResponse . data ) . toBe ( data ) ;
450+ // assert global fetch was never called
451+ expect ( fetchMocker ) . not . toHaveBeenCalled ( ) ;
419452 } ) ;
420453 } ) ;
421454
0 commit comments