The original public-landing routing refactor migrated WelcomeRouter, WelcomeStep1, and WelcomeStep2 post-onboarding redirects to /home, but left four sites still pointing at the old / + query-string destinations: - WelcomeStep3 `completeWizardAndExit` (Send invites) - WelcomeStep3 `handleSkipStep` (Skip) - VerifyEmailPage post-verify auto-redirect (`setTimeout`) - VerifyEmailPage success-state "Go to dashboard" Link These all worked by accident because PublicLanding redirects authed users from / to /home — so users still landed on the dashboard, but through an unnecessary mount-and-redirect flicker, and the `?welcome=true` / `?verified=1` query markers got dropped on the way. Drop both query markers — neither is read anywhere in the codebase (grepped frontend/src; the dashboard's onboarding UX is driven by `getOnboardingStatus`, not URL state). Carrying dead URL params just invites future "is this load-bearing?" investigations. Test stubs in WelcomeStep3.test.tsx and VerifyEmailPage.test.tsx moved from `<Route path="/">` to `<Route path="/home">` so the assertions verify the new destination instead of accidentally matching the old one (the previous stubs masked the partial migration). Out of scope: AcceptInvitePage and OAuthCallbackPage still use `?welcome=teammate`, but that one carries an explicit "decoded by the dashboard in Task 41" annotation and may be wired up later, so left untouched. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
175 lines
5.0 KiB
TypeScript
175 lines
5.0 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
import { render, screen, waitFor } from '@testing-library/react'
|
|
import { MemoryRouter, Route, Routes } from 'react-router-dom'
|
|
import { HelmetProvider } from 'react-helmet-async'
|
|
|
|
import { VerifyEmailPage } from '../VerifyEmailPage'
|
|
import { authApi } from '@/api/auth'
|
|
import { useAuthStore } from '@/store/authStore'
|
|
import type { User } from '@/types'
|
|
|
|
vi.mock('@/api/auth', () => ({
|
|
authApi: {
|
|
verifyEmail: vi.fn(),
|
|
sendVerificationEmail: vi.fn(),
|
|
me: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
vi.mock('@/lib/toast', () => ({
|
|
toast: {
|
|
success: vi.fn(),
|
|
error: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
function makeUser(overrides: Partial<User> = {}): User {
|
|
return {
|
|
id: 'user-1',
|
|
email: 'test@example.com',
|
|
name: 'Test User',
|
|
role: 'engineer',
|
|
is_super_admin: false,
|
|
is_active: true,
|
|
must_change_password: false,
|
|
account_id: 'acct-1',
|
|
account_role: 'engineer',
|
|
team_id: null,
|
|
created_at: '2026-05-01T00:00:00Z',
|
|
last_login: null,
|
|
phone: null,
|
|
job_title: null,
|
|
timezone: 'UTC',
|
|
avatar_url: null,
|
|
email_verified_at: null,
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
function renderPage(initialPath: string) {
|
|
return render(
|
|
<HelmetProvider>
|
|
<MemoryRouter initialEntries={[initialPath]}>
|
|
<Routes>
|
|
<Route path="/verify-email" element={<VerifyEmailPage />} />
|
|
<Route path="/home" element={<div>dashboard</div>} />
|
|
</Routes>
|
|
</MemoryRouter>
|
|
</HelmetProvider>,
|
|
)
|
|
}
|
|
|
|
describe('VerifyEmailPage', () => {
|
|
beforeEach(() => {
|
|
vi.useFakeTimers({ shouldAdvanceTime: true })
|
|
useAuthStore.setState({
|
|
user: null,
|
|
token: null,
|
|
isAuthenticated: false,
|
|
})
|
|
vi.mocked(authApi.me).mockResolvedValue(
|
|
makeUser({ email_verified_at: '2026-05-06T00:00:00Z' }),
|
|
)
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers()
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('shows success and redirects on valid token', async () => {
|
|
useAuthStore.setState({ user: makeUser() })
|
|
// Override fetchUser to avoid hitting axios/XHR in jsdom — the page calls
|
|
// it after a successful verify to refresh `email_verified_at`.
|
|
useAuthStore.setState({ fetchUser: vi.fn().mockResolvedValue(undefined) })
|
|
vi.mocked(authApi.verifyEmail).mockResolvedValue(undefined)
|
|
|
|
renderPage('/verify-email?token=valid-token')
|
|
|
|
await waitFor(() => {
|
|
expect(authApi.verifyEmail).toHaveBeenCalledWith('valid-token')
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText(/Email verified/i)).toBeInTheDocument()
|
|
})
|
|
|
|
// Advance past the redirect delay.
|
|
vi.advanceTimersByTime(2000)
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('dashboard')).toBeInTheDocument()
|
|
})
|
|
})
|
|
|
|
it('shows already-verified state when user is already verified', async () => {
|
|
useAuthStore.setState({
|
|
user: makeUser({ email_verified_at: '2026-05-05T00:00:00Z' }),
|
|
})
|
|
|
|
renderPage('/verify-email?token=any-token')
|
|
|
|
await waitFor(() => {
|
|
expect(
|
|
screen.getByText(/already verified/i),
|
|
).toBeInTheDocument()
|
|
})
|
|
|
|
// The verify endpoint must NOT have been called when the user is already
|
|
// verified — that would burn a perfectly good token for no reason.
|
|
expect(authApi.verifyEmail).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('only calls verifyEmail once even if the effect re-runs (strict-mode guard)', async () => {
|
|
useAuthStore.setState({ user: makeUser() })
|
|
useAuthStore.setState({ fetchUser: vi.fn().mockResolvedValue(undefined) })
|
|
vi.mocked(authApi.verifyEmail).mockResolvedValue(undefined)
|
|
|
|
const { rerender } = render(
|
|
<HelmetProvider>
|
|
<MemoryRouter initialEntries={['/verify-email?token=valid-token']}>
|
|
<Routes>
|
|
<Route path="/verify-email" element={<VerifyEmailPage />} />
|
|
<Route path="/home" element={<div>dashboard</div>} />
|
|
</Routes>
|
|
</MemoryRouter>
|
|
</HelmetProvider>,
|
|
)
|
|
|
|
// Force a re-render to simulate React 19 strict-mode double-invoke.
|
|
rerender(
|
|
<HelmetProvider>
|
|
<MemoryRouter initialEntries={['/verify-email?token=valid-token']}>
|
|
<Routes>
|
|
<Route path="/verify-email" element={<VerifyEmailPage />} />
|
|
<Route path="/home" element={<div>dashboard</div>} />
|
|
</Routes>
|
|
</MemoryRouter>
|
|
</HelmetProvider>,
|
|
)
|
|
|
|
await waitFor(() => {
|
|
expect(authApi.verifyEmail).toHaveBeenCalled()
|
|
})
|
|
|
|
expect(authApi.verifyEmail).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('shows an error state with a resend CTA on invalid token', async () => {
|
|
useAuthStore.setState({ user: makeUser() })
|
|
vi.mocked(authApi.verifyEmail).mockRejectedValue(
|
|
Object.assign(new Error('boom'), {
|
|
response: { data: { detail: 'Token expired' } },
|
|
}),
|
|
)
|
|
|
|
renderPage('/verify-email?token=stale-token')
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText(/Verification failed/i)).toBeInTheDocument()
|
|
})
|
|
expect(screen.getByText(/Token expired/i)).toBeInTheDocument()
|
|
expect(screen.getByTestId('resend-button')).toBeInTheDocument()
|
|
})
|
|
})
|