@@ -40,3 +40,75 @@ test('override setting defaults', async () => {
4040 // Existing property values should remain constant
4141 expect ( settings . db . schema ) . toBe ( defaults . db . schema )
4242} )
43+
44+ describe ( 'resolveFetch' , ( ) => {
45+ const TEST_URL = 'https://example.com'
46+ const TEST_OPTIONS = { method : 'GET' }
47+
48+ beforeEach ( ( ) => {
49+ // Reset any mocks between tests
50+ jest . resetModules ( )
51+ jest . clearAllMocks ( )
52+ } )
53+
54+ test ( 'should use custom fetch if provided' , async ( ) => {
55+ const customFetch = jest . fn ( )
56+ const resolvedFetch = helpers . resolveFetch ( customFetch )
57+
58+ await resolvedFetch ( TEST_URL , TEST_OPTIONS )
59+
60+ expect ( customFetch ) . toHaveBeenCalledTimes ( 1 )
61+ expect ( customFetch ) . toHaveBeenCalledWith ( TEST_URL , TEST_OPTIONS )
62+ } )
63+
64+ test ( 'should use global fetch if no custom fetch is provided' , async ( ) => {
65+ const globalFetch = jest . fn ( )
66+ global . fetch = globalFetch
67+ const resolvedFetch = helpers . resolveFetch ( )
68+
69+ await resolvedFetch ( TEST_URL , TEST_OPTIONS )
70+
71+ expect ( globalFetch ) . toHaveBeenCalledTimes ( 1 )
72+ expect ( globalFetch ) . toHaveBeenCalledWith ( TEST_URL , TEST_OPTIONS )
73+ } )
74+
75+ test ( 'should use node-fetch if global fetch is not available' , async ( ) => {
76+ const nodeFetch = jest . fn ( )
77+ jest . mock ( '@supabase/node-fetch' , ( ) => nodeFetch )
78+
79+ global . fetch = undefined as any
80+ const resolvedFetch = helpers . resolveFetch ( )
81+
82+ await resolvedFetch ( TEST_URL , TEST_OPTIONS )
83+
84+ expect ( nodeFetch ) . toHaveBeenCalledTimes ( 1 )
85+ expect ( nodeFetch ) . toHaveBeenCalledWith ( TEST_URL , TEST_OPTIONS )
86+ } )
87+ } )
88+
89+ describe ( 'resolveHeadersConstructor' , ( ) => {
90+ beforeEach ( ( ) => {
91+ // Reset any mocks between tests
92+ jest . resetModules ( )
93+ jest . clearAllMocks ( )
94+ } )
95+
96+ test ( 'should use Headers if available' , async ( ) => {
97+ const resolvedHeadersConstructor = await helpers . resolveHeadersConstructor ( )
98+ expect ( resolvedHeadersConstructor ) . toBe ( Headers )
99+ } )
100+
101+ test ( 'should use node-fetch Headers if global Headers is not available' , async ( ) => {
102+ const MockHeaders = jest . fn ( )
103+ jest . mock ( '@supabase/node-fetch' , ( ) => ( {
104+ Headers : MockHeaders ,
105+ } ) )
106+
107+ // Cannot assign read-only property, delete is available
108+ // @ts -ignore
109+ delete global . Headers
110+
111+ const resolvedHeadersConstructor = await helpers . resolveHeadersConstructor ( )
112+ expect ( resolvedHeadersConstructor ) . toBe ( MockHeaders )
113+ } )
114+ } )
0 commit comments