- tests/helpers/fakeSupabase: in-memory auth events + profiles/profile_roles builder - validation schemas and safeNextPath; classifyAuthError mapping (packages/supabase) - LoginRoute (unavailable, validation, next path, phrased failure, OAuth buttons, show password) - SignupRoute (policy, mismatch, metadata + redirect, user exists) - RequireAuth / RedirectIfSignedIn, AppLayout (nav, active, drawer, sign out), Dashboard, Account forms
76 lines
2.4 KiB
TypeScript
76 lines
2.4 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
|
import { cleanup, render, screen } from '@testing-library/react'
|
|
import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom'
|
|
import RequireAuth from '@/app/guards/RequireAuth'
|
|
import RedirectIfSignedIn from '@/app/guards/RedirectIfSignedIn'
|
|
import { useAuthStore, type AuthStatus } from '@/state/authStore'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
import { fr } from '@/i18n/fr'
|
|
|
|
function ShowLocation() {
|
|
const loc = useLocation()
|
|
return <p>{loc.pathname + loc.search}</p>
|
|
}
|
|
|
|
function mount(path: string) {
|
|
return render(
|
|
<MemoryRouter initialEntries={[path]}>
|
|
<Routes>
|
|
<Route path="/login" element={<ShowLocation />} />
|
|
<Route element={<RedirectIfSignedIn />}>
|
|
<Route path="/signup" element={<p>signup form</p>} />
|
|
</Route>
|
|
<Route path="/app" element={<RequireAuth />}>
|
|
<Route index element={<p>dashboard</p>} />
|
|
<Route path="account" element={<p>account</p>} />
|
|
</Route>
|
|
</Routes>
|
|
</MemoryRouter>,
|
|
)
|
|
}
|
|
|
|
function setStatus(status: AuthStatus) {
|
|
useAuthStore.setState({ status, recovery: false })
|
|
}
|
|
|
|
beforeEach(() => useLocaleStore.setState({ locale: 'fr' }))
|
|
afterEach(cleanup)
|
|
|
|
describe('RequireAuth', () => {
|
|
it('waits while the session loads', () => {
|
|
setStatus('loading')
|
|
mount('/app')
|
|
expect(screen.getByText(fr['shell.loading_session'])).toBeInTheDocument()
|
|
})
|
|
|
|
it('sends a visitor to /login with the return path', () => {
|
|
setStatus('signed_out')
|
|
mount('/app/account?tab=x')
|
|
expect(screen.getByText('/login?next=%2Fapp%2Faccount%3Ftab%3Dx')).toBeInTheDocument()
|
|
})
|
|
|
|
it('also redirects when no backend exists', () => {
|
|
setStatus('unavailable')
|
|
mount('/app')
|
|
expect(screen.getByText(/^\/login/)).toBeInTheDocument()
|
|
})
|
|
|
|
it('renders the page once signed in', () => {
|
|
setStatus('signed_in')
|
|
mount('/app/account')
|
|
expect(screen.getByText('account')).toBeInTheDocument()
|
|
})
|
|
})
|
|
|
|
describe('RedirectIfSignedIn', () => {
|
|
it('shows the form to a visitor and skips it for a signed-in user', () => {
|
|
setStatus('signed_out')
|
|
mount('/signup')
|
|
expect(screen.getByText('signup form')).toBeInTheDocument()
|
|
cleanup()
|
|
setStatus('signed_in')
|
|
mount('/signup')
|
|
expect(screen.queryByText('signup form')).not.toBeInTheDocument()
|
|
})
|
|
})
|