nextjs-rewrite (#5)
Reviewed-on: #5 Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
This commit was merged in pull request #5.
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { getSession } from '@/lib/auth-simple'
|
||||
|
||||
// Mock next/headers
|
||||
vi.mock('next/headers', () => ({
|
||||
cookies: vi.fn().mockResolvedValue({
|
||||
get: vi.fn().mockReturnValue({ name: 'better-auth.session_token', value: 'test-token' }),
|
||||
}),
|
||||
}))
|
||||
|
||||
// Mock fetch
|
||||
const mockFetch = vi.fn()
|
||||
global.fetch = mockFetch as any
|
||||
|
||||
describe('getSession', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('returns session data when auth API returns 200', async () => {
|
||||
const mockSession = {
|
||||
session: { token: 'test-token', expiresAt: new Date().toISOString() },
|
||||
user: { id: 'user-123', email: 'test@example.com' },
|
||||
}
|
||||
|
||||
mockFetch.mockResolvedValue(
|
||||
new Response(JSON.stringify(mockSession), { status: 200 })
|
||||
)
|
||||
|
||||
const result = await getSession()
|
||||
|
||||
expect(result).toEqual(mockSession)
|
||||
expect(mockFetch).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns null when auth API returns non-200 status', async () => {
|
||||
mockFetch.mockResolvedValue(
|
||||
new Response(null, { status: 401 })
|
||||
)
|
||||
|
||||
const result = await getSession()
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null when an error occurs', async () => {
|
||||
mockFetch.mockRejectedValue(new Error('Network error'))
|
||||
|
||||
const result = await getSession()
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user