Skip to content

Commit 88a0be0

Browse files
committed
added GoTrueClient tests
1 parent f0fe344 commit 88a0be0

File tree

2 files changed

+142
-34
lines changed

2 files changed

+142
-34
lines changed

test/GoTrueClient.browser.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,24 @@ describe('Web3 functionality in browser', () => {
355355
await expect(pkceClient.signInWithWeb3(credentials)).rejects.toThrow()
356356
})
357357
})
358+
359+
describe('GoTrueClient constructor edge cases', () => {
360+
361+
it('should handle userStorage with persistSession', () => {
362+
const customUserStorage = {
363+
getItem: jest.fn(),
364+
setItem: jest.fn(),
365+
removeItem: jest.fn(),
366+
}
367+
368+
const client = new (require('../src/GoTrueClient').default)({
369+
url: 'http://localhost:9999',
370+
userStorage: customUserStorage,
371+
persistSession: true,
372+
autoRefreshToken: false,
373+
})
374+
375+
expect(client).toBeDefined()
376+
expect((client as any).userStorage).toBe(customUserStorage)
377+
})
378+
})

test/GoTrueClient.test.ts

Lines changed: 121 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -492,15 +492,49 @@ describe('GoTrueClient', () => {
492492
expect(error?.message).toContain('Unable to validate email address: invalid format')
493493
})
494494

495-
test('resend() with email', async () => {
496-
const { email } = mockUserCredentials()
497-
498-
const { error } = await auth.resend({
499-
email,
500-
type: 'signup',
501-
options: { emailRedirectTo: email },
502-
})
495+
test.each([
496+
{
497+
name: 'resend with email with options',
498+
params: {
499+
email: mockUserCredentials().email,
500+
type: 'signup' as const,
501+
options: { emailRedirectTo: mockUserCredentials().email },
502+
},
503+
},
504+
{
505+
name: 'resend with email with empty options',
506+
params: {
507+
email: mockUserCredentials().email,
508+
type: 'signup' as const,
509+
options: {},
510+
},
511+
},
512+
])('$name', async ({ params }) => {
513+
const { error } = await auth.resend(params)
514+
expect(error).toBeNull()
515+
})
503516

517+
test.each([
518+
{
519+
name: 'resend with phone with options',
520+
params: {
521+
phone: mockUserCredentials().phone,
522+
type: 'phone_change' as const,
523+
options: {
524+
captchaToken: 'some_token',
525+
},
526+
},
527+
},
528+
{
529+
name: 'resend with phone with empty options',
530+
params: {
531+
phone: mockUserCredentials().phone,
532+
type: 'phone_change' as const,
533+
options: {},
534+
},
535+
},
536+
])('$name', async ({ params }) => {
537+
const { error } = await phoneClient.resend(params)
504538
expect(error).toBeNull()
505539
})
506540

@@ -540,6 +574,40 @@ describe('GoTrueClient', () => {
540574
expect(data.session).toBeNull()
541575
})
542576

577+
test.each([
578+
{
579+
name: 'signInWithOtp for email with empty options',
580+
testFn: async () => {
581+
const { email } = mockUserCredentials()
582+
const { data, error } = await auth.signInWithOtp({
583+
email,
584+
options: {},
585+
})
586+
expect(error).toBeNull()
587+
expect(data.user).toBeNull()
588+
expect(data.session).toBeNull()
589+
},
590+
},
591+
{
592+
name: 'verifyOTP fails with empty options',
593+
testFn: async () => {
594+
const { phone } = mockUserCredentials()
595+
596+
const { error } = await phoneClient.verifyOtp({
597+
phone,
598+
type: 'phone_change',
599+
token: '123456',
600+
options: {},
601+
})
602+
603+
expect(error).not.toBeNull()
604+
expect(error?.message).toContain('Token has expired or is invalid')
605+
},
606+
},
607+
])('$name', async ({ testFn }) => {
608+
await testFn()
609+
})
610+
543611
test('signInWithOtp() pkce flow fails with invalid sms provider', async () => {
544612
const { phone } = mockUserCredentials()
545613

@@ -634,19 +702,7 @@ describe('GoTrueClient', () => {
634702
}
635703
})
636704

637-
test('resend() with phone', async () => {
638-
const { phone } = mockUserCredentials()
639705

640-
const { error } = await phoneClient.resend({
641-
phone,
642-
type: 'phone_change',
643-
options: {
644-
captchaToken: 'some_token',
645-
},
646-
})
647-
648-
expect(error).toBeNull()
649-
})
650706

651707
test('verifyOTP() fails with invalid token', async () => {
652708
const { phone } = mockUserCredentials()
@@ -664,6 +720,8 @@ describe('GoTrueClient', () => {
664720
expect(error).not.toBeNull()
665721
expect(error?.message).toContain('Token has expired or is invalid')
666722
})
723+
724+
667725
})
668726

669727
test('signUp() the same user twice should not share email already registered message', async () => {
@@ -2324,8 +2382,8 @@ describe('Storage adapter edge cases', () => {
23242382
getItem: async () => {
23252383
throw new Error('getItem failed message')
23262384
},
2327-
setItem: async () => {},
2328-
removeItem: async () => {},
2385+
setItem: async () => { },
2386+
removeItem: async () => { },
23292387
}
23302388
const client = getClientWithSpecificStorage(brokenStorage)
23312389
await expect(client.getSession()).rejects.toThrow('getItem failed message')
@@ -2337,7 +2395,7 @@ describe('Storage adapter edge cases', () => {
23372395
setItem: async () => {
23382396
throw new Error('setItem failed message')
23392397
},
2340-
removeItem: async () => {},
2398+
removeItem: async () => { },
23412399
}
23422400
const client = getClientWithSpecificStorage(brokenStorage)
23432401
const session = {
@@ -2358,7 +2416,7 @@ describe('Storage adapter edge cases', () => {
23582416
test('should handle storage removeItem failure in _removeSession', async () => {
23592417
const brokenStorage = {
23602418
getItem: async () => '{}',
2361-
setItem: async () => {},
2419+
setItem: async () => { },
23622420
removeItem: async () => {
23632421
throw new Error('removeItem failed message')
23642422
},
@@ -2371,8 +2429,8 @@ describe('Storage adapter edge cases', () => {
23712429
test('should handle invalid JSON in storage', async () => {
23722430
const invalidStorage = {
23732431
getItem: async () => 'invalid-json',
2374-
setItem: async () => {},
2375-
removeItem: async () => {},
2432+
setItem: async () => { },
2433+
removeItem: async () => { },
23762434
}
23772435
const client = getClientWithSpecificStorage(invalidStorage)
23782436
const { data, error } = await client.getSession()
@@ -2383,8 +2441,8 @@ describe('Storage adapter edge cases', () => {
23832441
test('should handle null storage value', async () => {
23842442
const nullStorage = {
23852443
getItem: async () => null,
2386-
setItem: async () => {},
2387-
removeItem: async () => {},
2444+
setItem: async () => { },
2445+
removeItem: async () => { },
23882446
}
23892447
const client = getClientWithSpecificStorage(nullStorage)
23902448
const { data, error } = await client.getSession()
@@ -2395,8 +2453,8 @@ describe('Storage adapter edge cases', () => {
23952453
test('should handle empty storage value', async () => {
23962454
const emptyStorage = {
23972455
getItem: async () => '',
2398-
setItem: async () => {},
2399-
removeItem: async () => {},
2456+
setItem: async () => { },
2457+
removeItem: async () => { },
24002458
}
24012459
const client = getClientWithSpecificStorage(emptyStorage)
24022460
const { data, error } = await client.getSession()
@@ -2407,8 +2465,8 @@ describe('Storage adapter edge cases', () => {
24072465
test('should handle malformed session data', async () => {
24082466
const malformedStorage = {
24092467
getItem: async () => JSON.stringify({ access_token: 'test' }), // Missing required fields
2410-
setItem: async () => {},
2411-
removeItem: async () => {},
2468+
setItem: async () => { },
2469+
removeItem: async () => { },
24122470
}
24132471
const client = getClientWithSpecificStorage(malformedStorage)
24142472
const { data, error } = await client.getSession()
@@ -2427,8 +2485,8 @@ describe('Storage adapter edge cases', () => {
24272485
token_type: 'bearer',
24282486
user: null,
24292487
}),
2430-
setItem: async () => {},
2431-
removeItem: async () => {},
2488+
setItem: async () => { },
2489+
removeItem: async () => { },
24322490
}
24332491
const client = getClientWithSpecificStorage(expiredStorage)
24342492
// @ts-expect-error private method
@@ -2488,13 +2546,42 @@ describe('SSO Authentication', () => {
24882546
providerId: 'valid-provider-id',
24892547
options: {
24902548
redirectTo: 'http://localhost:3000/callback',
2549+
captchaToken: 'some-token',
24912550
},
24922551
})
24932552

24942553
expect(error).not.toBeNull()
24952554
expect(error?.message).toContain('SAML 2.0 is disabled')
24962555
expect(data).toBeNull()
24972556
})
2557+
2558+
test.each([
2559+
{
2560+
name: 'with empty options',
2561+
params: {
2562+
providerId: 'valid-provider-id',
2563+
domain: 'valid-domain',
2564+
options: {},
2565+
},
2566+
},
2567+
{
2568+
name: 'without params',
2569+
params: {} as any,
2570+
},
2571+
{
2572+
name: 'with minimal params',
2573+
params: {
2574+
providerId: 'test-provider',
2575+
domain: 'test-domain',
2576+
},
2577+
},
2578+
])('signInWithSSO $name', async ({ params }) => {
2579+
const { data, error } = await pkceClient.signInWithSSO(params)
2580+
2581+
expect(error).not.toBeNull()
2582+
expect(error?.message).toContain('SAML 2.0 is disabled')
2583+
expect(data).toBeNull()
2584+
})
24982585
})
24992586

25002587
describe('Lock functionality', () => {

0 commit comments

Comments
 (0)