import { describe, it, expect, vi } from 'vitest'; import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { ProfileDrawer } from '../../../src/components/auth/ProfileDrawer'; const makeProps = (overrides: Record = {}) => ({ isOpen: true, onClose: vi.fn(), loggedInUser: { id: 1, email: 'doc@acupedia.com', display_name: 'Dr. Lin', phone_number: '+919876543210', country: 'India', active_subscriptions: [ { id: 1, plan_code: 'POINTS_ACU', status: 'ACTIVE', started_at: '2026-01-01', expires_at: '2027-01-01', payment_id: 'pay_1', invoice_number: 'INV-1' }, ], } as any, profileDisplayName: 'Dr. Lin', setProfileDisplayName: vi.fn(), profilePhone: '+919876543210', setProfilePhone: vi.fn(), profileCountry: 'India', setProfileCountry: vi.fn(), setCurrency: vi.fn(), profileSaving: false, profileError: null, profileSuccess: false, onUpdateProfile: vi.fn((e) => e.preventDefault()), showPasswordChange: false, setShowPasswordChange: vi.fn(), changeOldPassword: '', setChangeOldPassword: vi.fn(), changeNewPassword: '', setChangeNewPassword: vi.fn(), changeConfirmPassword: '', setChangeConfirmPassword: vi.fn(), changePasswordLoading: false, changePasswordError: null, setChangePasswordError: vi.fn(), changePasswordSuccess: false, setChangePasswordSuccess: vi.fn(), onChangePassword: vi.fn((e) => e.preventDefault()), onLogout: vi.fn(), onBrowseStorefront: vi.fn(), onViewSubscriptions: vi.fn(), curriculumModules: [ { code: 'POINTS_ACU', name: 'Acu Points', category: 'Points', desc: 'Desc', priceInr: 300, priceUsd: 5, priceEur: 5 }, ], onDownloadInvoice: vi.fn(), ...overrides, }); describe('ProfileDrawer', () => { it('returns null when closed', () => { const { container } = render(); expect(container.firstChild).toBeNull(); }); it('renders profile form, success/error banners, invoice download, and logout', async () => { const user = userEvent.setup(); const props = makeProps({ profileSuccess: true, profileError: 'Save error' }); const { container } = render(); expect(screen.getByText('Save error')).toBeInTheDocument(); expect(screen.getByText(/Changes saved successfully/i)).toBeInTheDocument(); const nameInput = container.querySelector('input[type="text"]') as HTMLInputElement; await user.type(nameInput, 'X'); expect(props.setProfileDisplayName).toHaveBeenCalled(); // Change country & phone inside ProfileDrawer await user.click(screen.getByRole('button', { name: /India/i })); await user.click(screen.getByText('United States')); expect(props.setProfileCountry).toHaveBeenCalledWith('United States'); expect(props.setCurrency).toHaveBeenCalledWith('USD'); const phoneInput = container.querySelector('#phone-input') as HTMLInputElement; await user.type(phoneInput, '5'); expect(props.setProfilePhone).toHaveBeenCalled(); // Download invoice from ProfileDrawer await user.click(screen.getByRole('button', { name: /Invoice/i })); expect(props.onDownloadInvoice).toHaveBeenCalled(); await user.click(screen.getByRole('button', { name: /Save Profile Details/i })); expect(props.onUpdateProfile).toHaveBeenCalled(); await user.click(screen.getByRole('button', { name: /Change Password/i })); expect(props.setShowPasswordChange).toHaveBeenCalledWith(true); await user.click(screen.getByRole('button', { name: /Log Out Session/i })); expect(props.onLogout).toHaveBeenCalledTimes(1); }); it('renders change password form when expanded and submits', async () => { const user = userEvent.setup(); const props = makeProps({ showPasswordChange: true, changeOldPassword: 'OldPassword1!', changeNewPassword: 'NewPassword1!', changeConfirmPassword: 'NewPassword1!', changePasswordError: 'Weak password', }); const { container } = render(); expect(screen.getByText('Weak password')).toBeInTheDocument(); const pwdInputs = container.querySelectorAll('input[type="password"]'); await user.type(pwdInputs[0] as HTMLInputElement, '1'); await user.type(pwdInputs[1] as HTMLInputElement, '2'); await user.type(pwdInputs[2] as HTMLInputElement, '3'); expect(props.setChangeOldPassword).toHaveBeenCalled(); expect(props.setChangeNewPassword).toHaveBeenCalled(); expect(props.setChangeConfirmPassword).toHaveBeenCalled(); await user.click(screen.getByRole('button', { name: /Update Password/i })); expect(props.onChangePassword).toHaveBeenCalled(); // Toggle password visibility icons & hide form const eyeBtns = Array.from(container.querySelectorAll('button[type="button"]')).filter((b) => b.querySelector('svg.lucide-eye, svg.lucide-eye-off') ); for (const btn of eyeBtns) { await user.click(btn); } await user.click(screen.getByRole('button', { name: /Hide Form/i })); expect(props.setShowPasswordChange).toHaveBeenCalledWith(false); }); });