|
| 1 | +import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 2 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 3 | +import { MemoryRouter } from 'react-router-dom'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import i18n from '@/i18n'; |
| 6 | +import SettingsPage from '@/pages/SettingsPage'; |
| 7 | + |
| 8 | +const mocks = vi.hoisted(() => ({ |
| 9 | + subscription: { |
| 10 | + tier: 'trial', |
| 11 | + loading: false, |
| 12 | + isPro: false, |
| 13 | + dailyUsed: 4, |
| 14 | + dailyLimit: 5, |
| 15 | + trialEndsAt: '2026-10-01T00:00:00.000Z', |
| 16 | + hasStripeBilling: false, |
| 17 | + refresh: vi.fn(), |
| 18 | + }, |
| 19 | + redirectToPortal: vi.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock('@/lib/supabase/AuthProvider', () => ({ |
| 23 | + useAuth: () => ({ user: null as { id: string; email: string } | null, signOut: vi.fn() }), |
| 24 | +})); |
| 25 | +vi.mock('@/lib/supabase/client', () => ({ supabase: { from: vi.fn() } })); |
| 26 | +vi.mock('@/lib/stores/settings-store', () => ({ useSettingsStore: () => ({ |
| 27 | + language: 'en', outputLanguage: 'en', theme: 'dark', |
| 28 | + setLanguage: vi.fn(), setOutputLanguage: vi.fn(), setTheme: vi.fn(), |
| 29 | +}) })); |
| 30 | +vi.mock('@/lib/stores/session-store', () => ({ useSessionStore: () => ({ clearSession: vi.fn(), clearAllData: vi.fn() }) })); |
| 31 | +vi.mock('@/lib/stores/sparring-store', () => ({ useSparringStore: () => vi.fn() })); |
| 32 | +vi.mock('@/components/features/subscription/PricingModal', () => ({ |
| 33 | + PricingModal: ({ open }: { open: boolean }) => open ? <div>Pricing open</div> : null, |
| 34 | +})); |
| 35 | +vi.mock('@/hooks/use-subscription', () => ({ useSubscription: () => mocks.subscription })); |
| 36 | +vi.mock('@/lib/stripe', () => ({ redirectToPortal: mocks.redirectToPortal })); |
| 37 | + |
| 38 | +function renderSettings() { |
| 39 | + const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }); |
| 40 | + return render( |
| 41 | + <QueryClientProvider client={client}> |
| 42 | + <MemoryRouter><SettingsPage /></MemoryRouter> |
| 43 | + </QueryClientProvider>, |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +beforeEach(async () => { |
| 48 | + await i18n.changeLanguage('en'); |
| 49 | + Object.assign(mocks.subscription, { tier: 'trial', loading: false, hasStripeBilling: false }); |
| 50 | + mocks.redirectToPortal.mockReset().mockResolvedValue(undefined); |
| 51 | +}); |
| 52 | + |
| 53 | +afterEach(() => cleanup()); |
| 54 | + |
| 55 | +describe('Settings subscription card', () => { |
| 56 | + it.each([ |
| 57 | + ['trial', false, 'Free Trial', 'AI access is active. Usage limits may apply.', 'Upgrade to Pro'], |
| 58 | + ['pro', true, 'Pro', 'AI access is active. Usage limits may apply.', 'Manage Subscription'], |
| 59 | + ['expired', false, 'Expired', 'Your access has expired. Upgrade to continue using AI tools.', 'Upgrade to Pro'], |
| 60 | + ])('shows non-numeric access status for %s users', (tier, hasStripeBilling, badge, status, action) => { |
| 61 | + Object.assign(mocks.subscription, { tier, hasStripeBilling }); |
| 62 | + |
| 63 | + renderSettings(); |
| 64 | + |
| 65 | + expect(screen.getByText(badge)).toBeInTheDocument(); |
| 66 | + expect(screen.getByText(status)).toBeInTheDocument(); |
| 67 | + expect(screen.getByRole('button', { name: action })).toBeInTheDocument(); |
| 68 | + expect(screen.queryByRole('progressbar')).not.toBeInTheDocument(); |
| 69 | + expect(screen.queryByText('4 / 5 uses')).not.toBeInTheDocument(); |
| 70 | + expect(screen.queryByText(/Resets in/)).not.toBeInTheDocument(); |
| 71 | + if (tier === 'trial') expect(screen.getByText('Ends 01/10/2026')).toBeInTheDocument(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('does not claim access is active while the subscription is loading', () => { |
| 75 | + Object.assign(mocks.subscription, { loading: true }); |
| 76 | + |
| 77 | + renderSettings(); |
| 78 | + |
| 79 | + expect(screen.getByText('Loading...')).toBeInTheDocument(); |
| 80 | + expect(screen.queryByText('AI access is active. Usage limits may apply.')).not.toBeInTheDocument(); |
| 81 | + expect(screen.queryByText('Free Trial')).not.toBeInTheDocument(); |
| 82 | + expect(screen.queryByRole('button', { name: 'Upgrade to Pro' })).not.toBeInTheDocument(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('opens the upgrade flow', () => { |
| 86 | + Object.assign(mocks.subscription, { tier: 'expired' }); |
| 87 | + renderSettings(); |
| 88 | + |
| 89 | + fireEvent.click(screen.getByRole('button', { name: 'Upgrade to Pro' })); |
| 90 | + |
| 91 | + expect(screen.getByText('Pricing open')).toBeInTheDocument(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('opens billing management for Stripe-backed Pro users', async () => { |
| 95 | + Object.assign(mocks.subscription, { tier: 'pro', hasStripeBilling: true }); |
| 96 | + renderSettings(); |
| 97 | + |
| 98 | + fireEvent.click(screen.getByRole('button', { name: 'Manage Subscription' })); |
| 99 | + |
| 100 | + await waitFor(() => expect(mocks.redirectToPortal).toHaveBeenCalledOnce()); |
| 101 | + }); |
| 102 | +}); |
0 commit comments