@@ -104,6 +104,18 @@ describe('SupabaseClient', () => {
104
104
// @ts -ignore
105
105
expect ( client . storageKey ) . toBe ( customStorageKey )
106
106
} )
107
+
108
+ test ( 'should handle undefined storageKey and headers' , ( ) => {
109
+ const client = createClient ( URL , KEY , {
110
+ auth : { storageKey : undefined } ,
111
+ global : { headers : undefined } ,
112
+ } )
113
+ expect ( client ) . toBeDefined ( )
114
+ // @ts -ignore
115
+ expect ( client . storageKey ) . toBe ( '' )
116
+ // @ts -ignore
117
+ expect ( client . headers ) . toHaveProperty ( 'X-Client-Info' )
118
+ } )
107
119
} )
108
120
109
121
describe ( 'Client Methods' , ( ) => {
@@ -166,6 +178,14 @@ describe('SupabaseClient', () => {
166
178
} )
167
179
} )
168
180
181
+ describe ( 'Table/View Queries' , ( ) => {
182
+ test ( 'should query table with string relation' , ( ) => {
183
+ const client = createClient < Database > ( URL , KEY )
184
+ const queryBuilder = client . from ( 'users' )
185
+ expect ( queryBuilder ) . toBeDefined ( )
186
+ } )
187
+ } )
188
+
169
189
describe ( 'RPC Calls' , ( ) => {
170
190
test ( 'should make RPC call with arguments' , ( ) => {
171
191
const client = createClient < Database > ( URL , KEY )
@@ -179,4 +199,125 @@ describe('SupabaseClient', () => {
179
199
expect ( rpcCall ) . toBeDefined ( )
180
200
} )
181
201
} )
202
+
203
+ describe ( 'Access Token Handling' , ( ) => {
204
+ test ( 'should use custom accessToken when provided' , async ( ) => {
205
+ const customToken = 'custom-jwt-token'
206
+ const client = createClient ( URL , KEY , {
207
+ accessToken : async ( ) => customToken ,
208
+ } )
209
+
210
+ // Access the private method through the client instance
211
+ const accessToken = await ( client as any ) . _getAccessToken ( )
212
+ expect ( accessToken ) . toBe ( customToken )
213
+ } )
214
+
215
+ test ( 'should fallback to session access token when no custom accessToken' , async ( ) => {
216
+ const client = createClient ( URL , KEY )
217
+
218
+ // Mock the auth.getSession method
219
+ const mockSession = {
220
+ data : {
221
+ session : {
222
+ access_token : 'session-jwt-token' ,
223
+ } ,
224
+ } ,
225
+ }
226
+ client . auth . getSession = jest . fn ( ) . mockResolvedValue ( mockSession )
227
+
228
+ const accessToken = await ( client as any ) . _getAccessToken ( )
229
+ expect ( accessToken ) . toBe ( 'session-jwt-token' )
230
+ } )
231
+
232
+ test ( 'should fallback to supabaseKey when no session' , async ( ) => {
233
+ const client = createClient ( URL , KEY )
234
+
235
+ // Mock the auth.getSession method to return no session
236
+ const mockSession = {
237
+ data : {
238
+ session : null ,
239
+ } ,
240
+ }
241
+ client . auth . getSession = jest . fn ( ) . mockResolvedValue ( mockSession )
242
+
243
+ const accessToken = await ( client as any ) . _getAccessToken ( )
244
+ expect ( accessToken ) . toBe ( KEY )
245
+ } )
246
+ } )
247
+
248
+ describe ( 'Auth Event Handling' , ( ) => {
249
+ test ( 'should handle TOKEN_REFRESHED event' , ( ) => {
250
+ const client = createClient ( URL , KEY )
251
+ const newToken = 'new-refreshed-token'
252
+
253
+ // Mock realtime.setAuth
254
+ client . realtime . setAuth = jest . fn ( )
255
+ ; ( client as any ) . _handleTokenChanged ( 'TOKEN_REFRESHED' , 'CLIENT' , newToken )
256
+
257
+ expect ( ( client as any ) . changedAccessToken ) . toBe ( newToken )
258
+ } )
259
+
260
+ test ( 'should listen for auth events' , ( ) => {
261
+ const client = createClient ( URL , KEY )
262
+
263
+ // Mock auth.onAuthStateChange
264
+ const mockCallback = jest . fn ( )
265
+ client . auth . onAuthStateChange = jest . fn ( ) . mockReturnValue ( mockCallback )
266
+
267
+ // Call the private method
268
+ const result = ( client as any ) . _listenForAuthEvents ( )
269
+
270
+ expect ( client . auth . onAuthStateChange ) . toHaveBeenCalled ( )
271
+ expect ( result ) . toBe ( mockCallback )
272
+ } )
273
+
274
+ test ( 'should handle SIGNED_IN event' , ( ) => {
275
+ const client = createClient ( URL , KEY )
276
+ const newToken = 'new-signed-in-token'
277
+
278
+ // Mock realtime.setAuth
279
+ client . realtime . setAuth = jest . fn ( )
280
+ ; ( client as any ) . _handleTokenChanged ( 'SIGNED_IN' , 'CLIENT' , newToken )
281
+
282
+ expect ( ( client as any ) . changedAccessToken ) . toBe ( newToken )
283
+ } )
284
+
285
+ test ( 'should not update token if it is the same' , ( ) => {
286
+ const client = createClient ( URL , KEY )
287
+ const existingToken = 'existing-token'
288
+ ; ( client as any ) . changedAccessToken = existingToken
289
+
290
+ // Mock realtime.setAuth
291
+ client . realtime . setAuth = jest . fn ( )
292
+ ; ( client as any ) . _handleTokenChanged ( 'TOKEN_REFRESHED' , 'CLIENT' , existingToken )
293
+
294
+ expect ( ( client as any ) . changedAccessToken ) . toBe ( existingToken )
295
+ } )
296
+
297
+ test ( 'should handle SIGNED_OUT event from CLIENT source' , ( ) => {
298
+ const client = createClient ( URL , KEY )
299
+ ; ( client as any ) . changedAccessToken = 'old-token'
300
+
301
+ // Mock realtime.setAuth
302
+ client . realtime . setAuth = jest . fn ( )
303
+ ; ( client as any ) . _handleTokenChanged ( 'SIGNED_OUT' , 'CLIENT' )
304
+
305
+ expect ( client . realtime . setAuth ) . toHaveBeenCalled ( )
306
+ expect ( ( client as any ) . changedAccessToken ) . toBeUndefined ( )
307
+ } )
308
+
309
+ test ( 'should handle SIGNED_OUT event from STORAGE source' , ( ) => {
310
+ const client = createClient ( URL , KEY )
311
+ ; ( client as any ) . changedAccessToken = 'old-token'
312
+
313
+ // Mock realtime.setAuth and auth.signOut
314
+ client . realtime . setAuth = jest . fn ( )
315
+ client . auth . signOut = jest . fn ( )
316
+ ; ( client as any ) . _handleTokenChanged ( 'SIGNED_OUT' , 'STORAGE' )
317
+
318
+ expect ( client . realtime . setAuth ) . toHaveBeenCalled ( )
319
+ expect ( client . auth . signOut ) . toHaveBeenCalled ( )
320
+ expect ( ( client as any ) . changedAccessToken ) . toBeUndefined ( )
321
+ } )
322
+ } )
182
323
} )
0 commit comments