|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act, type InputHTMLAttributes, type ReactNode } from 'react' |
| 3 | +import { createRoot, type Root } from 'react-dom/client' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | +import SignupForm from '@/app/(auth)/signup/signup-form' |
| 6 | + |
| 7 | +const { push, signUp, refetchSession } = vi.hoisted(() => ({ |
| 8 | + push: vi.fn(), |
| 9 | + signUp: vi.fn(), |
| 10 | + refetchSession: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('next/navigation', () => ({ |
| 14 | + useRouter: () => ({ push }), |
| 15 | + useSearchParams: () => new URLSearchParams(), |
| 16 | +})) |
| 17 | +vi.mock('@marsidev/react-turnstile', () => ({ Turnstile: () => null })) |
| 18 | +vi.mock('posthog-js/react', () => ({ usePostHog: () => null })) |
| 19 | +vi.mock('@/lib/analytics/google', () => ({ trackGoogleEvent: vi.fn() })) |
| 20 | +vi.mock('@/lib/auth/auth-client', () => ({ |
| 21 | + client: { signUp: { email: signUp } }, |
| 22 | + useSession: () => ({ refetch: refetchSession }), |
| 23 | +})) |
| 24 | +vi.mock('@/lib/consent/tracking-consent', () => ({ |
| 25 | + useTrackingConsent: () => ({ measurement: false }), |
| 26 | +})) |
| 27 | +vi.mock('@/lib/core/config/env', () => ({ getEnv: () => undefined, isFalsy: () => false })) |
| 28 | +vi.mock('@/lib/core/config/env-flags', () => ({ isSsoEnabled: false })) |
| 29 | +vi.mock('@/lib/core/security/input-validation', () => ({ validateCallbackUrl: () => false })) |
| 30 | +vi.mock('@/lib/messaging/email/validation', () => ({ |
| 31 | + quickValidateEmail: () => ({ isValid: true }), |
| 32 | +})) |
| 33 | +vi.mock('@/lib/posthog/client', () => ({ captureClientEvent: vi.fn(), captureEvent: vi.fn() })) |
| 34 | +vi.mock('@/app/(auth)/components', () => ({ |
| 35 | + AuthDivider: () => null, |
| 36 | + AuthField: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 37 | + AuthFormMessage: () => null, |
| 38 | + AuthHeader: () => null, |
| 39 | + AuthInput: ({ error, ...props }: InputHTMLAttributes<HTMLInputElement> & { error?: boolean }) => ( |
| 40 | + <input {...props} /> |
| 41 | + ), |
| 42 | + AuthLegalFooter: () => null, |
| 43 | + AuthNavPrompt: () => null, |
| 44 | + AuthSubmitButton: ({ children }: { children: ReactNode }) => ( |
| 45 | + <button type='submit'>{children}</button> |
| 46 | + ), |
| 47 | + PasswordInput: ({ |
| 48 | + error, |
| 49 | + ...props |
| 50 | + }: InputHTMLAttributes<HTMLInputElement> & { error?: boolean }) => <input {...props} />, |
| 51 | + SocialLoginButtons: () => null, |
| 52 | + SSOLoginButton: () => null, |
| 53 | +})) |
| 54 | + |
| 55 | +let root: Root |
| 56 | +let host: HTMLDivElement |
| 57 | +let destination: { href: string } |
| 58 | + |
| 59 | +beforeEach(() => { |
| 60 | + vi.clearAllMocks() |
| 61 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 62 | + destination = { href: '' } |
| 63 | + const browser = window |
| 64 | + vi.stubGlobal( |
| 65 | + 'window', |
| 66 | + new Proxy(browser, { |
| 67 | + get(target, key) { |
| 68 | + return key === 'location' ? destination : Reflect.get(target, key, target) |
| 69 | + }, |
| 70 | + }) |
| 71 | + ) |
| 72 | + signUp.mockResolvedValue({ data: { user: { id: 'new-user' } } }) |
| 73 | + refetchSession.mockResolvedValue(undefined) |
| 74 | + host = document.createElement('div') |
| 75 | + document.body.append(host) |
| 76 | + root = createRoot(host) |
| 77 | +}) |
| 78 | + |
| 79 | +afterEach(() => { |
| 80 | + act(() => root.unmount()) |
| 81 | + host.remove() |
| 82 | + vi.unstubAllGlobals() |
| 83 | +}) |
| 84 | + |
| 85 | +async function submit(emailVerificationEnabled: boolean) { |
| 86 | + act(() => |
| 87 | + root.render( |
| 88 | + <SignupForm |
| 89 | + githubAvailable={false} |
| 90 | + googleAvailable={false} |
| 91 | + microsoftAvailable={false} |
| 92 | + emailSignupEnabled |
| 93 | + emailVerificationEnabled={emailVerificationEnabled} |
| 94 | + /> |
| 95 | + ) |
| 96 | + ) |
| 97 | + const fields = { name: 'Test Builder', email: 'builder@example.com', password: 'SafePass1!' } |
| 98 | + for (const [name, value] of Object.entries(fields)) { |
| 99 | + const input = host.querySelector<HTMLInputElement>(`input[name="${name}"]`) |
| 100 | + if (!input) throw new Error(`Missing ${name} input`) |
| 101 | + input.value = value |
| 102 | + } |
| 103 | + const form = host.querySelector('form') |
| 104 | + if (!form) throw new Error('Missing signup form') |
| 105 | + await act(async () => |
| 106 | + form.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })) |
| 107 | + ) |
| 108 | +} |
| 109 | + |
| 110 | +describe('signup shell navigation', () => { |
| 111 | + it('starts a document navigation after a successful signup without verification', async () => { |
| 112 | + await submit(false) |
| 113 | + expect(signUp).toHaveBeenCalledOnce() |
| 114 | + expect(refetchSession).toHaveBeenCalledOnce() |
| 115 | + expect(destination.href).toBe('/workspace') |
| 116 | + expect(push).not.toHaveBeenCalled() |
| 117 | + }) |
| 118 | + |
| 119 | + it('keeps verification within the auth shell and stores the email for the next step', async () => { |
| 120 | + await submit(true) |
| 121 | + expect(push).toHaveBeenCalledWith('/verify?fromSignup=true') |
| 122 | + expect(sessionStorage.getItem('verificationEmail')).toBe('builder@example.com') |
| 123 | + expect(destination.href).toBe('') |
| 124 | + }) |
| 125 | + |
| 126 | + it('does not navigate when signup fails', async () => { |
| 127 | + signUp.mockResolvedValue({ error: { message: 'Signup failed' } }) |
| 128 | + await submit(false) |
| 129 | + expect(push).not.toHaveBeenCalled() |
| 130 | + expect(destination.href).toBe('') |
| 131 | + }) |
| 132 | +}) |
0 commit comments