Add extensive form tests and enhanced error handling for auth components.
- Introduced comprehensive tests for `RegisterForm`, `PasswordResetRequestForm`, and `PasswordResetConfirmForm` covering successful submissions, validation errors, and API error handling. - Refactored forms to handle unexpected errors gracefully and improve test coverage for edge cases. - Updated `crypto` and `storage` modules with robust error handling for storage issues and encryption key management. - Removed unused `axios-mock-adapter` dependency for cleaner dependency management.
This commit is contained in:
@@ -7,6 +7,31 @@ import userEvent from '@testing-library/user-event';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { LoginForm } from '@/components/auth/LoginForm';
|
||||
|
||||
// Mock the useLogin hook
|
||||
const mockMutateAsync = jest.fn();
|
||||
const mockUseLogin = jest.fn(() => ({
|
||||
mutateAsync: mockMutateAsync,
|
||||
mutate: jest.fn(),
|
||||
isPending: false,
|
||||
isError: false,
|
||||
isSuccess: false,
|
||||
isIdle: true,
|
||||
error: null,
|
||||
data: undefined,
|
||||
status: 'idle' as const,
|
||||
variables: undefined,
|
||||
reset: jest.fn(),
|
||||
context: undefined,
|
||||
failureCount: 0,
|
||||
failureReason: null,
|
||||
isPaused: false,
|
||||
submittedAt: 0,
|
||||
}));
|
||||
|
||||
jest.mock('@/lib/api/hooks/useAuth', () => ({
|
||||
useLogin: () => mockUseLogin(),
|
||||
}));
|
||||
|
||||
// Mock router
|
||||
jest.mock('next/navigation', () => ({
|
||||
useRouter: () => ({
|
||||
@@ -38,6 +63,11 @@ const createWrapper = () => {
|
||||
};
|
||||
|
||||
describe('LoginForm', () => {
|
||||
beforeEach(() => {
|
||||
mockMutateAsync.mockClear();
|
||||
mockUseLogin.mockClear();
|
||||
});
|
||||
|
||||
it('renders login form with email and password fields', () => {
|
||||
render(<LoginForm />, { wrapper: createWrapper() });
|
||||
|
||||
@@ -59,9 +89,6 @@ describe('LoginForm', () => {
|
||||
});
|
||||
});
|
||||
|
||||
// Note: Email validation is primarily handled by HTML5 type="email" attribute
|
||||
// Zod provides additional validation layer
|
||||
|
||||
it('shows password requirements validation', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<LoginForm />, { wrapper: createWrapper() });
|
||||
@@ -92,6 +119,162 @@ describe('LoginForm', () => {
|
||||
expect(screen.getByRole('link', { name: /forgot password/i })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
// Note: Async submission tests require API mocking with MSW
|
||||
// Will be added in Phase 9 (Testing Infrastructure)
|
||||
describe('Form submission', () => {
|
||||
it('calls mutateAsync with form data on valid submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<LoginForm />, { wrapper: createWrapper() });
|
||||
|
||||
const emailInput = screen.getByLabelText(/email/i);
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const submitButton = screen.getByRole('button', { name: /sign in/i });
|
||||
|
||||
await user.type(emailInput, 'test@example.com');
|
||||
await user.type(passwordInput, 'Password123');
|
||||
await user.click(submitButton);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockMutateAsync).toHaveBeenCalledWith({
|
||||
email: 'test@example.com',
|
||||
password: 'Password123',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('calls onSuccess callback after successful login', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onSuccess = jest.fn();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<LoginForm onSuccess={onSuccess} />, { wrapper: createWrapper() });
|
||||
|
||||
const emailInput = screen.getByLabelText(/email/i);
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const submitButton = screen.getByRole('button', { name: /sign in/i });
|
||||
|
||||
await user.type(emailInput, 'test@example.com');
|
||||
await user.type(passwordInput, 'Password123');
|
||||
await user.click(submitButton);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSuccess).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('displays general error message from API', async () => {
|
||||
const user = userEvent.setup();
|
||||
const apiError = [
|
||||
{
|
||||
code: 'AUTH_001',
|
||||
message: 'Invalid credentials',
|
||||
},
|
||||
];
|
||||
mockMutateAsync.mockRejectedValueOnce(apiError);
|
||||
|
||||
render(<LoginForm />, { wrapper: createWrapper() });
|
||||
|
||||
const emailInput = screen.getByLabelText(/email/i);
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const submitButton = screen.getByRole('button', { name: /sign in/i });
|
||||
|
||||
await user.type(emailInput, 'test@example.com');
|
||||
await user.type(passwordInput, 'WrongPassword1');
|
||||
await user.click(submitButton);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Invalid credentials')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('displays field-specific errors from API', async () => {
|
||||
const user = userEvent.setup();
|
||||
const apiError = [
|
||||
{
|
||||
code: 'VALIDATION_ERROR',
|
||||
message: 'Invalid email format',
|
||||
field: 'email',
|
||||
},
|
||||
{
|
||||
code: 'VALIDATION_ERROR',
|
||||
message: 'Password is too weak',
|
||||
field: 'password',
|
||||
},
|
||||
];
|
||||
mockMutateAsync.mockRejectedValueOnce(apiError);
|
||||
|
||||
render(<LoginForm />, { wrapper: createWrapper() });
|
||||
|
||||
const emailInput = screen.getByLabelText(/email/i);
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const submitButton = screen.getByRole('button', { name: /sign in/i });
|
||||
|
||||
await user.type(emailInput, 'test@example.com');
|
||||
await user.type(passwordInput, 'Password123');
|
||||
await user.click(submitButton);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Invalid email format')).toBeInTheDocument();
|
||||
expect(screen.getByText('Password is too weak')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('displays generic error for unexpected error format', async () => {
|
||||
const user = userEvent.setup();
|
||||
const unexpectedError = new Error('Network error');
|
||||
mockMutateAsync.mockRejectedValueOnce(unexpectedError);
|
||||
|
||||
render(<LoginForm />, { wrapper: createWrapper() });
|
||||
|
||||
const emailInput = screen.getByLabelText(/email/i);
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const submitButton = screen.getByRole('button', { name: /sign in/i });
|
||||
|
||||
await user.type(emailInput, 'test@example.com');
|
||||
await user.type(passwordInput, 'Password123');
|
||||
await user.click(submitButton);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('An unexpected error occurred. Please try again.')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('clears previous errors on new submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
const apiError = [
|
||||
{
|
||||
code: 'AUTH_001',
|
||||
message: 'Invalid credentials',
|
||||
},
|
||||
];
|
||||
|
||||
// First submission fails
|
||||
mockMutateAsync.mockRejectedValueOnce(apiError);
|
||||
|
||||
render(<LoginForm />, { wrapper: createWrapper() });
|
||||
|
||||
const emailInput = screen.getByLabelText(/email/i);
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const submitButton = screen.getByRole('button', { name: /sign in/i });
|
||||
|
||||
await user.type(emailInput, 'test@example.com');
|
||||
await user.type(passwordInput, 'WrongPassword1');
|
||||
await user.click(submitButton);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Invalid credentials')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
// Second submission succeeds
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
await user.clear(passwordInput);
|
||||
await user.type(passwordInput, 'CorrectPassword1');
|
||||
await user.click(submitButton);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByText('Invalid credentials')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,6 +7,31 @@ import userEvent from '@testing-library/user-event';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { PasswordResetConfirmForm } from '@/components/auth/PasswordResetConfirmForm';
|
||||
|
||||
// Mock the usePasswordResetConfirm hook
|
||||
const mockMutateAsync = jest.fn();
|
||||
const mockUsePasswordResetConfirm = jest.fn(() => ({
|
||||
mutateAsync: mockMutateAsync,
|
||||
mutate: jest.fn(),
|
||||
isPending: false,
|
||||
isError: false,
|
||||
isSuccess: false,
|
||||
isIdle: true,
|
||||
error: null,
|
||||
data: undefined,
|
||||
status: 'idle' as const,
|
||||
variables: undefined,
|
||||
reset: jest.fn(),
|
||||
context: undefined,
|
||||
failureCount: 0,
|
||||
failureReason: null,
|
||||
isPaused: false,
|
||||
submittedAt: 0,
|
||||
}));
|
||||
|
||||
jest.mock('@/lib/api/hooks/useAuth', () => ({
|
||||
usePasswordResetConfirm: () => mockUsePasswordResetConfirm(),
|
||||
}));
|
||||
|
||||
jest.mock('next/navigation', () => ({
|
||||
useRouter: () => ({
|
||||
push: jest.fn(),
|
||||
@@ -31,6 +56,11 @@ const createWrapper = () => {
|
||||
describe('PasswordResetConfirmForm', () => {
|
||||
const mockToken = 'test-reset-token-123';
|
||||
|
||||
beforeEach(() => {
|
||||
mockMutateAsync.mockClear();
|
||||
mockUsePasswordResetConfirm.mockClear();
|
||||
});
|
||||
|
||||
it('renders password reset confirm form with all fields', () => {
|
||||
render(<PasswordResetConfirmForm token={mockToken} />, {
|
||||
wrapper: createWrapper(),
|
||||
@@ -135,9 +165,6 @@ describe('PasswordResetConfirmForm', () => {
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
// Note: Async submission tests require API mocking with MSW
|
||||
// Will be added in Phase 9 (Testing Infrastructure)
|
||||
|
||||
it('marks required fields with asterisk', () => {
|
||||
render(<PasswordResetConfirmForm token={mockToken} />, {
|
||||
wrapper: createWrapper(),
|
||||
@@ -156,4 +183,198 @@ describe('PasswordResetConfirmForm', () => {
|
||||
const hiddenInput = container.querySelector('input[type="hidden"]');
|
||||
expect(hiddenInput).toHaveValue(mockToken);
|
||||
});
|
||||
|
||||
describe('Form submission', () => {
|
||||
it('calls mutateAsync with token and new_password on valid submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<PasswordResetConfirmForm token={mockToken} />, {
|
||||
wrapper: createWrapper(),
|
||||
});
|
||||
|
||||
await user.type(screen.getByLabelText(/new password/i), 'NewPassword123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'NewPassword123');
|
||||
await user.click(screen.getByRole('button', { name: /reset password/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockMutateAsync).toHaveBeenCalledWith({
|
||||
token: mockToken,
|
||||
new_password: 'NewPassword123',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('does not include confirm_password in API request', async () => {
|
||||
const user = userEvent.setup();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<PasswordResetConfirmForm token={mockToken} />, {
|
||||
wrapper: createWrapper(),
|
||||
});
|
||||
|
||||
await user.type(screen.getByLabelText(/new password/i), 'NewPassword123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'NewPassword123');
|
||||
await user.click(screen.getByRole('button', { name: /reset password/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockMutateAsync).toHaveBeenCalled();
|
||||
const callArgs = mockMutateAsync.mock.calls[0][0];
|
||||
expect(callArgs).not.toHaveProperty('confirm_password');
|
||||
});
|
||||
});
|
||||
|
||||
it('displays success message after successful submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<PasswordResetConfirmForm token={mockToken} />, {
|
||||
wrapper: createWrapper(),
|
||||
});
|
||||
|
||||
await user.type(screen.getByLabelText(/new password/i), 'NewPassword123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'NewPassword123');
|
||||
await user.click(screen.getByRole('button', { name: /reset password/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/your password has been successfully reset/i)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('resets form after successful submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<PasswordResetConfirmForm token={mockToken} />, {
|
||||
wrapper: createWrapper(),
|
||||
});
|
||||
|
||||
const passwordInput = screen.getByLabelText(/new password/i) as HTMLInputElement;
|
||||
const confirmInput = screen.getByLabelText(/confirm password/i) as HTMLInputElement;
|
||||
|
||||
await user.type(passwordInput, 'NewPassword123');
|
||||
await user.type(confirmInput, 'NewPassword123');
|
||||
await user.click(screen.getByRole('button', { name: /reset password/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(passwordInput.value).toBe('');
|
||||
expect(confirmInput.value).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
it('calls onSuccess callback after successful submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onSuccess = jest.fn();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<PasswordResetConfirmForm token={mockToken} onSuccess={onSuccess} />, {
|
||||
wrapper: createWrapper(),
|
||||
});
|
||||
|
||||
await user.type(screen.getByLabelText(/new password/i), 'NewPassword123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'NewPassword123');
|
||||
await user.click(screen.getByRole('button', { name: /reset password/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSuccess).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('displays general error message from API', async () => {
|
||||
const user = userEvent.setup();
|
||||
const apiError = [
|
||||
{
|
||||
code: 'AUTH_003',
|
||||
message: 'Invalid or expired token',
|
||||
},
|
||||
];
|
||||
mockMutateAsync.mockRejectedValueOnce(apiError);
|
||||
|
||||
render(<PasswordResetConfirmForm token={mockToken} />, {
|
||||
wrapper: createWrapper(),
|
||||
});
|
||||
|
||||
await user.type(screen.getByLabelText(/new password/i), 'NewPassword123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'NewPassword123');
|
||||
await user.click(screen.getByRole('button', { name: /reset password/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Invalid or expired token')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('displays field-specific errors from API', async () => {
|
||||
const user = userEvent.setup();
|
||||
const apiError = [
|
||||
{
|
||||
code: 'VAL_003',
|
||||
message: 'Password does not meet requirements',
|
||||
field: 'new_password',
|
||||
},
|
||||
];
|
||||
mockMutateAsync.mockRejectedValueOnce(apiError);
|
||||
|
||||
render(<PasswordResetConfirmForm token={mockToken} />, {
|
||||
wrapper: createWrapper(),
|
||||
});
|
||||
|
||||
await user.type(screen.getByLabelText(/new password/i), 'NewPassword123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'NewPassword123');
|
||||
await user.click(screen.getByRole('button', { name: /reset password/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Password does not meet requirements')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('displays generic error for unexpected error format', async () => {
|
||||
const user = userEvent.setup();
|
||||
const unexpectedError = new Error('Network error');
|
||||
mockMutateAsync.mockRejectedValueOnce(unexpectedError);
|
||||
|
||||
render(<PasswordResetConfirmForm token={mockToken} />, {
|
||||
wrapper: createWrapper(),
|
||||
});
|
||||
|
||||
await user.type(screen.getByLabelText(/new password/i), 'NewPassword123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'NewPassword123');
|
||||
await user.click(screen.getByRole('button', { name: /reset password/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('An unexpected error occurred. Please try again.')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('clears success message on new submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
// First submission succeeds
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<PasswordResetConfirmForm token={mockToken} />, {
|
||||
wrapper: createWrapper(),
|
||||
});
|
||||
|
||||
await user.type(screen.getByLabelText(/new password/i), 'NewPassword123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'NewPassword123');
|
||||
await user.click(screen.getByRole('button', { name: /reset password/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/your password has been successfully reset/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
// Second submission with error
|
||||
mockMutateAsync.mockRejectedValueOnce([
|
||||
{ code: 'AUTH_003', message: 'Invalid or expired token' },
|
||||
]);
|
||||
|
||||
await user.type(screen.getByLabelText(/new password/i), 'AnotherPassword456');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'AnotherPassword456');
|
||||
await user.click(screen.getByRole('button', { name: /reset password/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByText(/your password has been successfully reset/i)).not.toBeInTheDocument();
|
||||
expect(screen.getByText('Invalid or expired token')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,6 +7,31 @@ import userEvent from '@testing-library/user-event';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { PasswordResetRequestForm } from '@/components/auth/PasswordResetRequestForm';
|
||||
|
||||
// Mock the usePasswordResetRequest hook
|
||||
const mockMutateAsync = jest.fn();
|
||||
const mockUsePasswordResetRequest = jest.fn(() => ({
|
||||
mutateAsync: mockMutateAsync,
|
||||
mutate: jest.fn(),
|
||||
isPending: false,
|
||||
isError: false,
|
||||
isSuccess: false,
|
||||
isIdle: true,
|
||||
error: null,
|
||||
data: undefined,
|
||||
status: 'idle' as const,
|
||||
variables: undefined,
|
||||
reset: jest.fn(),
|
||||
context: undefined,
|
||||
failureCount: 0,
|
||||
failureReason: null,
|
||||
isPaused: false,
|
||||
submittedAt: 0,
|
||||
}));
|
||||
|
||||
jest.mock('@/lib/api/hooks/useAuth', () => ({
|
||||
usePasswordResetRequest: () => mockUsePasswordResetRequest(),
|
||||
}));
|
||||
|
||||
jest.mock('next/navigation', () => ({
|
||||
useRouter: () => ({
|
||||
push: jest.fn(),
|
||||
@@ -29,6 +54,11 @@ const createWrapper = () => {
|
||||
};
|
||||
|
||||
describe('PasswordResetRequestForm', () => {
|
||||
beforeEach(() => {
|
||||
mockMutateAsync.mockClear();
|
||||
mockUsePasswordResetRequest.mockClear();
|
||||
});
|
||||
|
||||
it('renders password reset form with email field', () => {
|
||||
render(<PasswordResetRequestForm />, { wrapper: createWrapper() });
|
||||
|
||||
@@ -74,13 +104,153 @@ describe('PasswordResetRequestForm', () => {
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
// Note: Async submission tests require API mocking with MSW
|
||||
// Will be added in Phase 9 (Testing Infrastructure)
|
||||
|
||||
it('marks email field as required with asterisk', () => {
|
||||
render(<PasswordResetRequestForm />, { wrapper: createWrapper() });
|
||||
|
||||
const labels = screen.getAllByText('*');
|
||||
expect(labels.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
describe('Form submission', () => {
|
||||
it('calls mutateAsync with email on valid submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<PasswordResetRequestForm />, { wrapper: createWrapper() });
|
||||
|
||||
await user.type(screen.getByLabelText(/email/i), 'test@example.com');
|
||||
await user.click(screen.getByRole('button', { name: /send reset instructions/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockMutateAsync).toHaveBeenCalledWith({ email: 'test@example.com' });
|
||||
});
|
||||
});
|
||||
|
||||
it('displays success message after successful submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<PasswordResetRequestForm />, { wrapper: createWrapper() });
|
||||
|
||||
await user.type(screen.getByLabelText(/email/i), 'test@example.com');
|
||||
await user.click(screen.getByRole('button', { name: /send reset instructions/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/password reset instructions have been sent/i)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('resets form after successful submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<PasswordResetRequestForm />, { wrapper: createWrapper() });
|
||||
|
||||
const emailInput = screen.getByLabelText(/email/i) as HTMLInputElement;
|
||||
await user.type(emailInput, 'test@example.com');
|
||||
await user.click(screen.getByRole('button', { name: /send reset instructions/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(emailInput.value).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
it('calls onSuccess callback after successful submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onSuccess = jest.fn();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<PasswordResetRequestForm onSuccess={onSuccess} />, { wrapper: createWrapper() });
|
||||
|
||||
await user.type(screen.getByLabelText(/email/i), 'test@example.com');
|
||||
await user.click(screen.getByRole('button', { name: /send reset instructions/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSuccess).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('displays general error message from API', async () => {
|
||||
const user = userEvent.setup();
|
||||
const apiError = [
|
||||
{
|
||||
code: 'USER_001',
|
||||
message: 'User not found',
|
||||
},
|
||||
];
|
||||
mockMutateAsync.mockRejectedValueOnce(apiError);
|
||||
|
||||
render(<PasswordResetRequestForm />, { wrapper: createWrapper() });
|
||||
|
||||
await user.type(screen.getByLabelText(/email/i), 'notfound@example.com');
|
||||
await user.click(screen.getByRole('button', { name: /send reset instructions/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('User not found')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('displays field-specific errors from API', async () => {
|
||||
const user = userEvent.setup();
|
||||
const apiError = [
|
||||
{
|
||||
code: 'VAL_002',
|
||||
message: 'Invalid email format',
|
||||
field: 'email',
|
||||
},
|
||||
];
|
||||
mockMutateAsync.mockRejectedValueOnce(apiError);
|
||||
|
||||
render(<PasswordResetRequestForm />, { wrapper: createWrapper() });
|
||||
|
||||
await user.type(screen.getByLabelText(/email/i), 'test@example.com');
|
||||
await user.click(screen.getByRole('button', { name: /send reset instructions/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Invalid email format')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('displays generic error for unexpected error format', async () => {
|
||||
const user = userEvent.setup();
|
||||
const unexpectedError = new Error('Network error');
|
||||
mockMutateAsync.mockRejectedValueOnce(unexpectedError);
|
||||
|
||||
render(<PasswordResetRequestForm />, { wrapper: createWrapper() });
|
||||
|
||||
await user.type(screen.getByLabelText(/email/i), 'test@example.com');
|
||||
await user.click(screen.getByRole('button', { name: /send reset instructions/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('An unexpected error occurred. Please try again.')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('clears success message on new submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
// First submission succeeds
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<PasswordResetRequestForm />, { wrapper: createWrapper() });
|
||||
|
||||
const emailInput = screen.getByLabelText(/email/i);
|
||||
await user.type(emailInput, 'test@example.com');
|
||||
await user.click(screen.getByRole('button', { name: /send reset instructions/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/password reset instructions have been sent/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
// Second submission with error
|
||||
mockMutateAsync.mockRejectedValueOnce([{ code: 'USER_001', message: 'User not found' }]);
|
||||
|
||||
await user.type(emailInput, 'another@example.com');
|
||||
await user.click(screen.getByRole('button', { name: /send reset instructions/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByText(/password reset instructions have been sent/i)).not.toBeInTheDocument();
|
||||
expect(screen.getByText('User not found')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,6 +7,31 @@ import userEvent from '@testing-library/user-event';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { RegisterForm } from '@/components/auth/RegisterForm';
|
||||
|
||||
// Mock the useRegister hook
|
||||
const mockMutateAsync = jest.fn();
|
||||
const mockUseRegister = jest.fn(() => ({
|
||||
mutateAsync: mockMutateAsync,
|
||||
mutate: jest.fn(),
|
||||
isPending: false,
|
||||
isError: false,
|
||||
isSuccess: false,
|
||||
isIdle: true,
|
||||
error: null,
|
||||
data: undefined,
|
||||
status: 'idle' as const,
|
||||
variables: undefined,
|
||||
reset: jest.fn(),
|
||||
context: undefined,
|
||||
failureCount: 0,
|
||||
failureReason: null,
|
||||
isPaused: false,
|
||||
submittedAt: 0,
|
||||
}));
|
||||
|
||||
jest.mock('@/lib/api/hooks/useAuth', () => ({
|
||||
useRegister: () => mockUseRegister(),
|
||||
}));
|
||||
|
||||
jest.mock('next/navigation', () => ({
|
||||
useRouter: () => ({
|
||||
push: jest.fn(),
|
||||
@@ -36,6 +61,11 @@ const createWrapper = () => {
|
||||
};
|
||||
|
||||
describe('RegisterForm', () => {
|
||||
beforeEach(() => {
|
||||
mockMutateAsync.mockClear();
|
||||
mockUseRegister.mockClear();
|
||||
});
|
||||
|
||||
it('renders registration form with all fields', () => {
|
||||
render(<RegisterForm />, { wrapper: createWrapper() });
|
||||
|
||||
@@ -109,4 +139,131 @@ describe('RegisterForm', () => {
|
||||
const labels = screen.getAllByText('*');
|
||||
expect(labels.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
describe('Form submission', () => {
|
||||
it('calls mutateAsync with form data on valid submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<RegisterForm />, { wrapper: createWrapper() });
|
||||
|
||||
await user.type(screen.getByLabelText(/first name/i), 'John');
|
||||
await user.type(screen.getByLabelText(/last name/i), 'Doe');
|
||||
await user.type(screen.getByLabelText(/^email/i), 'john@example.com');
|
||||
await user.type(screen.getByLabelText(/^password/i), 'Password123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'Password123');
|
||||
await user.click(screen.getByRole('button', { name: /create account/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockMutateAsync).toHaveBeenCalledWith({
|
||||
first_name: 'John',
|
||||
last_name: 'Doe',
|
||||
email: 'john@example.com',
|
||||
password: 'Password123',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('excludes confirmPassword from API request', async () => {
|
||||
const user = userEvent.setup();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<RegisterForm />, { wrapper: createWrapper() });
|
||||
|
||||
await user.type(screen.getByLabelText(/first name/i), 'John');
|
||||
await user.type(screen.getByLabelText(/^email/i), 'john@example.com');
|
||||
await user.type(screen.getByLabelText(/^password/i), 'Password123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'Password123');
|
||||
await user.click(screen.getByRole('button', { name: /create account/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockMutateAsync).toHaveBeenCalled();
|
||||
const callArgs = mockMutateAsync.mock.calls[0][0];
|
||||
expect(callArgs).not.toHaveProperty('confirmPassword');
|
||||
});
|
||||
});
|
||||
|
||||
it('calls onSuccess callback after successful registration', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onSuccess = jest.fn();
|
||||
mockMutateAsync.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<RegisterForm onSuccess={onSuccess} />, { wrapper: createWrapper() });
|
||||
|
||||
await user.type(screen.getByLabelText(/first name/i), 'John');
|
||||
await user.type(screen.getByLabelText(/^email/i), 'john@example.com');
|
||||
await user.type(screen.getByLabelText(/^password/i), 'Password123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'Password123');
|
||||
await user.click(screen.getByRole('button', { name: /create account/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSuccess).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('displays general error message from API', async () => {
|
||||
const user = userEvent.setup();
|
||||
const apiError = [
|
||||
{
|
||||
code: 'USER_002',
|
||||
message: 'This email is already registered',
|
||||
},
|
||||
];
|
||||
mockMutateAsync.mockRejectedValueOnce(apiError);
|
||||
|
||||
render(<RegisterForm />, { wrapper: createWrapper() });
|
||||
|
||||
await user.type(screen.getByLabelText(/first name/i), 'John');
|
||||
await user.type(screen.getByLabelText(/^email/i), 'existing@example.com');
|
||||
await user.type(screen.getByLabelText(/^password/i), 'Password123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'Password123');
|
||||
await user.click(screen.getByRole('button', { name: /create account/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('This email is already registered')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('displays field-specific errors from API', async () => {
|
||||
const user = userEvent.setup();
|
||||
const apiError = [
|
||||
{
|
||||
code: 'VALIDATION_ERROR',
|
||||
message: 'Invalid email format',
|
||||
field: 'email',
|
||||
},
|
||||
];
|
||||
mockMutateAsync.mockRejectedValueOnce(apiError);
|
||||
|
||||
render(<RegisterForm />, { wrapper: createWrapper() });
|
||||
|
||||
await user.type(screen.getByLabelText(/first name/i), 'John');
|
||||
await user.type(screen.getByLabelText(/^email/i), 'john@example.com');
|
||||
await user.type(screen.getByLabelText(/^password/i), 'Password123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'Password123');
|
||||
await user.click(screen.getByRole('button', { name: /create account/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Invalid email format')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('displays generic error for unexpected error format', async () => {
|
||||
const user = userEvent.setup();
|
||||
const unexpectedError = new Error('Network error');
|
||||
mockMutateAsync.mockRejectedValueOnce(unexpectedError);
|
||||
|
||||
render(<RegisterForm />, { wrapper: createWrapper() });
|
||||
|
||||
await user.type(screen.getByLabelText(/first name/i), 'John');
|
||||
await user.type(screen.getByLabelText(/^email/i), 'john@example.com');
|
||||
await user.type(screen.getByLabelText(/^password/i), 'Password123');
|
||||
await user.type(screen.getByLabelText(/confirm password/i), 'Password123');
|
||||
await user.click(screen.getByRole('button', { name: /create account/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('An unexpected error occurred. Please try again.')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user