|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import { renderToStaticMarkup } from 'react-dom/server' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const { mockGetEnv, mockFeatureFlags } = vi.hoisted(() => ({ |
| 9 | + mockGetEnv: vi.fn((key: string) => { |
| 10 | + if (key === 'NEXT_PUBLIC_EMAIL_PASSWORD_SIGNUP_ENABLED') return 'true' |
| 11 | + if (key === 'NEXT_PUBLIC_SSO_ENABLED') return 'false' |
| 12 | + return undefined |
| 13 | + }), |
| 14 | + mockFeatureFlags: { |
| 15 | + getAuthTermsLinkConfig: (() => ({ href: '/terms', isExternal: false })) as () => { |
| 16 | + href: string |
| 17 | + isExternal: boolean |
| 18 | + } | null, |
| 19 | + getAuthPrivacyLinkConfig: (() => ({ href: '/privacy', isExternal: false })) as () => { |
| 20 | + href: string |
| 21 | + isExternal: boolean |
| 22 | + } | null, |
| 23 | + }, |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('next/link', () => ({ |
| 27 | + default: ({ href, children, ...props }: React.ComponentProps<'a'>) => ( |
| 28 | + <a href={typeof href === 'string' ? href : ''} {...props}> |
| 29 | + {children} |
| 30 | + </a> |
| 31 | + ), |
| 32 | +})) |
| 33 | + |
| 34 | +vi.mock('next/navigation', () => ({ |
| 35 | + useRouter: () => ({ push: vi.fn() }), |
| 36 | + useSearchParams: () => new URLSearchParams(), |
| 37 | +})) |
| 38 | + |
| 39 | +vi.mock('next/font/google', () => ({ |
| 40 | + Inter: () => ({ className: 'font-inter', variable: '--font-inter' }), |
| 41 | +})) |
| 42 | + |
| 43 | +vi.mock('next/font/local', () => ({ |
| 44 | + default: () => ({ className: 'font-soehne', variable: '--font-soehne' }), |
| 45 | +})) |
| 46 | + |
| 47 | +vi.mock('@/lib/core/config/env', () => ({ |
| 48 | + getEnv: mockGetEnv, |
| 49 | + isTruthy: (value: string | undefined) => value === 'true', |
| 50 | + isFalsy: (value: string | undefined) => value === 'false', |
| 51 | + env: { |
| 52 | + NEXT_PUBLIC_EMAIL_PASSWORD_SIGNUP_ENABLED: 'true', |
| 53 | + }, |
| 54 | +})) |
| 55 | + |
| 56 | +vi.mock('@/lib/core/config/feature-flags', () => ({ |
| 57 | + getAuthTermsLinkConfig: () => mockFeatureFlags.getAuthTermsLinkConfig(), |
| 58 | + getAuthPrivacyLinkConfig: () => mockFeatureFlags.getAuthPrivacyLinkConfig(), |
| 59 | +})) |
| 60 | + |
| 61 | +vi.mock('@/lib/auth/auth-client', () => ({ |
| 62 | + client: { |
| 63 | + signIn: { email: vi.fn(), social: vi.fn() }, |
| 64 | + signUp: { email: vi.fn() }, |
| 65 | + forgetPassword: vi.fn(), |
| 66 | + }, |
| 67 | + useSession: () => ({ refetch: vi.fn() }), |
| 68 | +})) |
| 69 | + |
| 70 | +vi.mock('@/hooks/use-branded-button-class', () => ({ |
| 71 | + useBrandedButtonClass: () => 'brand-button', |
| 72 | +})) |
| 73 | + |
| 74 | +vi.mock('@/app/(auth)/components/branded-button', () => ({ |
| 75 | + BrandedButton: ({ children }: { children: React.ReactNode }) => <button>{children}</button>, |
| 76 | +})) |
| 77 | + |
| 78 | +vi.mock('@/app/(auth)/components/social-login-buttons', () => ({ |
| 79 | + SocialLoginButtons: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 80 | +})) |
| 81 | + |
| 82 | +vi.mock('@/app/(auth)/components/sso-login-button', () => ({ |
| 83 | + SSOLoginButton: () => <button>SSO</button>, |
| 84 | +})) |
| 85 | + |
| 86 | +vi.mock('@/components/ui/dialog', () => ({ |
| 87 | + Dialog: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 88 | + DialogContent: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 89 | + DialogDescription: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 90 | + DialogHeader: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 91 | + DialogTitle: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 92 | +})) |
| 93 | + |
| 94 | +vi.mock('@/components/ui/input', () => ({ |
| 95 | + Input: (props: React.ComponentProps<'input'>) => <input {...props} />, |
| 96 | +})) |
| 97 | + |
| 98 | +vi.mock('@/components/ui/label', () => ({ |
| 99 | + Label: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 100 | +})) |
| 101 | + |
| 102 | +vi.mock('@/components/ui/button', () => ({ |
| 103 | + Button: ({ children }: { children: React.ReactNode }) => <button>{children}</button>, |
| 104 | +})) |
| 105 | + |
| 106 | +vi.mock('@/lib/core/utils/cn', () => ({ |
| 107 | + cn: (...values: Array<string | undefined | false>) => values.filter(Boolean).join(' '), |
| 108 | +})) |
| 109 | + |
| 110 | +vi.mock('@/lib/core/utils/urls', () => ({ |
| 111 | + getBaseUrl: () => 'https://example.com', |
| 112 | +})) |
| 113 | + |
| 114 | +vi.mock('@/lib/messaging/email/validation', () => ({ |
| 115 | + quickValidateEmail: () => ({ isValid: true }), |
| 116 | +})) |
| 117 | + |
| 118 | +import SSOForm from '../../ee/sso/components/sso-form' |
| 119 | +import LoginPage from './login/login-form' |
| 120 | +import SignupPage from './signup/signup-form' |
| 121 | + |
| 122 | +describe('auth legal link rendering', () => { |
| 123 | + beforeEach(() => { |
| 124 | + mockFeatureFlags.getAuthTermsLinkConfig = () => ({ href: '/terms', isExternal: false }) |
| 125 | + mockFeatureFlags.getAuthPrivacyLinkConfig = () => ({ href: '/privacy', isExternal: false }) |
| 126 | + }) |
| 127 | + |
| 128 | + it('renders internal legal links on auth surfaces when legal pages are enabled', () => { |
| 129 | + const loginHtml = renderToStaticMarkup( |
| 130 | + <LoginPage githubAvailable={false} googleAvailable={false} isProduction={false} /> |
| 131 | + ) |
| 132 | + const signupHtml = renderToStaticMarkup( |
| 133 | + <SignupPage githubAvailable={false} googleAvailable={false} isProduction={false} /> |
| 134 | + ) |
| 135 | + const ssoHtml = renderToStaticMarkup(<SSOForm />) |
| 136 | + |
| 137 | + expect(loginHtml).toContain('href="/terms"') |
| 138 | + expect(loginHtml).toContain('href="/privacy"') |
| 139 | + expect(signupHtml).toContain('href="/terms"') |
| 140 | + expect(signupHtml).toContain('href="/privacy"') |
| 141 | + expect(ssoHtml).toContain('href="/terms"') |
| 142 | + expect(ssoHtml).toContain('href="/privacy"') |
| 143 | + }) |
| 144 | + |
| 145 | + it('renders external legal links on auth surfaces when legal pages are disabled but external urls exist', () => { |
| 146 | + mockFeatureFlags.getAuthTermsLinkConfig = () => ({ |
| 147 | + href: 'https://legal.example.com/terms', |
| 148 | + isExternal: true, |
| 149 | + }) |
| 150 | + mockFeatureFlags.getAuthPrivacyLinkConfig = () => ({ |
| 151 | + href: 'https://legal.example.com/privacy', |
| 152 | + isExternal: true, |
| 153 | + }) |
| 154 | + |
| 155 | + const loginHtml = renderToStaticMarkup( |
| 156 | + <LoginPage githubAvailable={false} googleAvailable={false} isProduction={false} /> |
| 157 | + ) |
| 158 | + |
| 159 | + expect(loginHtml).toContain('href="https://legal.example.com/terms"') |
| 160 | + expect(loginHtml).toContain('href="https://legal.example.com/privacy"') |
| 161 | + }) |
| 162 | + |
| 163 | + it('hides only the missing individual legal link when no external fallback exists', () => { |
| 164 | + mockFeatureFlags.getAuthTermsLinkConfig = () => null |
| 165 | + mockFeatureFlags.getAuthPrivacyLinkConfig = () => ({ |
| 166 | + href: 'https://legal.example.com/privacy', |
| 167 | + isExternal: true, |
| 168 | + }) |
| 169 | + |
| 170 | + const loginHtml = renderToStaticMarkup( |
| 171 | + <LoginPage githubAvailable={false} googleAvailable={false} isProduction={false} /> |
| 172 | + ) |
| 173 | + |
| 174 | + expect(loginHtml).not.toContain('Terms of Service</a>') |
| 175 | + expect(loginHtml).toContain('Privacy Policy</a>') |
| 176 | + expect(loginHtml).toContain('href="https://legal.example.com/privacy"') |
| 177 | + }) |
| 178 | +}) |
0 commit comments