acupedia-store-ui/tests/unit/components/AuthModal.test.tsx

144 lines
6.1 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { AuthModal } from '../../../src/components/auth/AuthModal';
const makeProps = (overrides: Record<string, any> = {}) => ({
isOpen: true,
onClose: vi.fn(),
authMode: 'login' as const,
setAuthMode: vi.fn(),
authEmail: 'doc@acupedia.com',
setAuthEmail: vi.fn(),
authPassword: 'Password1!',
setAuthPassword: vi.fn(),
authDisplayName: 'Dr. Lin',
setAuthDisplayName: vi.fn(),
authPhone: '9876543210',
setAuthPhone: vi.fn(),
authDialCode: '+91',
setAuthDialCode: vi.fn(),
authCountry: 'India',
setAuthCountry: vi.fn(),
authLoading: false,
authError: null,
setAuthError: vi.fn(),
onAuthSubmit: vi.fn((e) => e.preventDefault()),
resetStep: 'request' as const,
setResetStep: vi.fn(),
resetOtp: '',
setResetOtp: vi.fn(),
resetNewPassword: '',
setResetNewPassword: vi.fn(),
resetConfirmPassword: '',
setResetConfirmPassword: vi.fn(),
resetSuccessMessage: null,
setResetSuccessMessage: vi.fn(),
onForgotPasswordRequest: vi.fn((e) => e.preventDefault()),
onResetPasswordSubmit: vi.fn((e) => e.preventDefault()),
...overrides,
});
describe('AuthModal', () => {
it('returns null when closed', () => {
const { container } = render(<AuthModal {...makeProps({ isOpen: false })} />);
expect(container.firstChild).toBeNull();
});
it('renders login mode, error alert, password toggle, and mode switches', async () => {
const user = userEvent.setup();
const props = makeProps({ authError: 'Invalid credentials' });
const { container } = render(<AuthModal {...props} />);
expect(screen.getByText('Welcome Back!')).toBeInTheDocument();
expect(screen.getByText('Invalid credentials')).toBeInTheDocument();
await user.click(screen.getByRole('button', { name: /Forgot password/i }));
expect(props.setAuthMode).toHaveBeenCalledWith('forgot');
await user.click(screen.getByRole('button', { name: 'Create an account' }));
expect(props.setAuthMode).toHaveBeenCalledWith('register');
await user.click(screen.getByRole('button', { name: /Sign In/i }));
expect(props.onAuthSubmit).toHaveBeenCalled();
// Test email & password inputs & password visibility toggle
const emailInput = container.querySelector('input[type="email"]') as HTMLInputElement;
await user.type(emailInput, 'x');
expect(props.setAuthEmail).toHaveBeenCalled();
const pwdInput = screen.getByPlaceholderText('••••••••');
await user.type(pwdInput, 'y');
expect(props.setAuthPassword).toHaveBeenCalled();
});
it('renders register mode with phone/country fields and submits', async () => {
const user = userEvent.setup();
const props = makeProps({ authMode: 'register', authPhone: '' });
const { container } = render(<AuthModal {...props} />);
expect(screen.getByRole('heading', { name: 'Create Account' })).toBeInTheDocument();
const textInputs = container.querySelectorAll('input[type="text"]');
await user.type(textInputs[0] as HTMLInputElement, 'A');
expect(props.setAuthDisplayName).toHaveBeenCalled();
const phoneInput = container.querySelector('#phone-input') as HTMLInputElement;
await user.type(phoneInput, '1');
expect(props.setAuthPhone).toHaveBeenCalled();
// Toggle password visibility & change country
const eyeBtns = container.querySelectorAll('button[type="button"]');
for (const b of eyeBtns) {
if (b.querySelector('svg.lucide-eye, svg.lucide-eye-off')) {
await user.click(b);
break;
}
}
await user.click(screen.getByRole('button', { name: /India/i }));
await user.click(screen.getByText('United States'));
expect(props.setAuthCountry).toHaveBeenCalledWith('United States');
await user.click(screen.getByRole('button', { name: 'Create Account' }));
expect(props.onAuthSubmit).toHaveBeenCalled();
await user.click(screen.getByRole('button', { name: 'Sign in here' }));
expect(props.setAuthMode).toHaveBeenCalledWith('login');
});
it('renders forgot password request and verify steps', async () => {
const user = userEvent.setup();
const propsReq = makeProps({ authMode: 'forgot', resetStep: 'request' });
const { rerender, container } = render(<AuthModal {...propsReq} />);
expect(screen.getByText('Reset Password')).toBeInTheDocument();
await user.click(screen.getByRole('button', { name: /Send Verification Code/i }));
expect(propsReq.onForgotPasswordRequest).toHaveBeenCalled();
const propsVerify = makeProps({
authMode: 'forgot',
resetStep: 'verify',
resetOtp: '',
resetNewPassword: 'NewPassword1!',
resetConfirmPassword: 'NewPassword1!',
resetSuccessMessage: 'OTP sent!',
});
rerender(<AuthModal {...propsVerify} />);
expect(screen.getByText('OTP sent!')).toBeInTheDocument();
const otpInput = container.querySelector('input[type="text"]') as HTMLInputElement;
await user.type(otpInput, '9');
expect(propsVerify.setResetOtp).toHaveBeenCalled();
const pwdInputs = container.querySelectorAll('input[type="password"]');
await user.type(pwdInputs[0] as HTMLInputElement, 'x');
await user.type(pwdInputs[1] as HTMLInputElement, 'y');
expect(propsVerify.setResetNewPassword).toHaveBeenCalled();
expect(propsVerify.setResetConfirmPassword).toHaveBeenCalled();
rerender(<AuthModal {...makeProps({ authMode: 'forgot', resetStep: 'verify', resetOtp: '123456', resetNewPassword: 'NewPassword1!', resetConfirmPassword: 'NewPassword1!', onResetPasswordSubmit: propsVerify.onResetPasswordSubmit, setResetStep: propsVerify.setResetStep, setAuthMode: propsVerify.setAuthMode })} />);
await user.click(screen.getByRole('button', { name: /Set New Password/i }));
expect(propsVerify.onResetPasswordSubmit).toHaveBeenCalled();
await user.click(screen.getByRole('button', { name: /Resend code/i }));
expect(propsVerify.setResetStep).toHaveBeenCalledWith('request');
await user.click(screen.getByRole('button', { name: /Back to sign in/i }));
expect(propsVerify.setAuthMode).toHaveBeenCalledWith('login');
});
});