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:
Felipe Cardoso
2025-11-01 05:24:26 +01:00
parent 035e6af446
commit ee938ce6a6
15 changed files with 934 additions and 536 deletions

View File

@@ -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();
});
});
});
});