Add comprehensive tests for authentication, settings, and password reset pages

- Introduced smoke tests for Login, Register, Password Reset, Password Reset Confirm, and Settings pages.
- Enhanced test coverage for all dynamic imports using mocks and added Jest exclusions for non-testable Next.js files.
- Added component-specific test files for better structure and maintainability.
- Improved test isolation by mocking navigation, providers, and rendering contexts.
This commit is contained in:
2025-11-02 17:33:57 +01:00
parent 77594e478d
commit fded54e61a
23 changed files with 599 additions and 2 deletions

View File

@@ -0,0 +1,164 @@
/**
* Tests for Password Reset Confirm Content Component
* Verifies token validation and form rendering
*/
import { render, screen, act } from '@testing-library/react';
import { useSearchParams, useRouter } from 'next/navigation';
import PasswordResetConfirmContent from '@/app/(auth)/password-reset/confirm/PasswordResetConfirmContent';
// Mock Next.js navigation
jest.mock('next/navigation', () => ({
useSearchParams: jest.fn(),
useRouter: jest.fn(),
default: jest.fn(),
}));
// Mock Next.js Link
jest.mock('next/link', () => ({
__esModule: true,
default: ({ children, href }: { children: React.ReactNode; href: string }) => (
<a href={href}>{children}</a>
),
}));
// Mock dynamic import
jest.mock('next/dynamic', () => ({
__esModule: true,
default: (importFn: () => Promise<any>, options?: any) => {
const Component = ({ onSuccess }: { onSuccess?: () => void }) => (
<div data-testid="password-reset-confirm-form">
<button onClick={onSuccess}>Submit</button>
</div>
);
Component.displayName = 'PasswordResetConfirmForm';
return Component;
},
}));
// Mock Alert component
jest.mock('@/components/ui/alert', () => ({
Alert: ({ children }: { children: React.ReactNode }) => (
<div data-testid="alert">{children}</div>
),
}));
describe('PasswordResetConfirmContent', () => {
let mockPush: jest.Mock;
beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers();
mockPush = jest.fn();
(useRouter as jest.Mock).mockReturnValue({
push: mockPush,
});
});
afterEach(() => {
jest.runOnlyPendingTimers();
jest.useRealTimers();
});
describe('With valid token', () => {
beforeEach(() => {
(useSearchParams as jest.Mock).mockReturnValue({
get: jest.fn((key: string) => (key === 'token' ? 'valid-token-123' : null)),
});
});
it('renders without crashing', () => {
render(<PasswordResetConfirmContent />);
expect(screen.getByText('Set new password')).toBeInTheDocument();
});
it('renders heading and description', () => {
render(<PasswordResetConfirmContent />);
expect(screen.getByRole('heading', { name: /set new password/i })).toBeInTheDocument();
expect(screen.getByText(/choose a strong password/i)).toBeInTheDocument();
});
it('renders PasswordResetConfirmForm with token', () => {
render(<PasswordResetConfirmContent />);
expect(screen.getByTestId('password-reset-confirm-form')).toBeInTheDocument();
});
it('redirects to login after successful password reset', () => {
render(<PasswordResetConfirmContent />);
const submitButton = screen.getByRole('button', { name: /submit/i });
// Trigger success handler
act(() => {
submitButton.click();
});
// Fast-forward time by 3 seconds
act(() => {
jest.advanceTimersByTime(3000);
});
expect(mockPush).toHaveBeenCalledWith('/login');
});
it('cleans up timeout on unmount', () => {
const { unmount } = render(<PasswordResetConfirmContent />);
const submitButton = screen.getByRole('button', { name: /submit/i });
// Trigger success handler
act(() => {
submitButton.click();
});
// Unmount before timeout fires
unmount();
// Fast-forward time
act(() => {
jest.advanceTimersByTime(3000);
});
// Should not redirect because component was unmounted
expect(mockPush).not.toHaveBeenCalled();
});
});
describe('Without token', () => {
beforeEach(() => {
(useSearchParams as jest.Mock).mockReturnValue({
get: jest.fn(() => null),
});
});
it('shows invalid reset link error', () => {
render(<PasswordResetConfirmContent />);
expect(screen.getByRole('heading', { name: /invalid reset link/i })).toBeInTheDocument();
expect(screen.getByTestId('alert')).toBeInTheDocument();
});
it('shows error message', () => {
render(<PasswordResetConfirmContent />);
expect(screen.getByText(/this password reset link is invalid or has expired/i)).toBeInTheDocument();
});
it('shows link to request new reset', () => {
render(<PasswordResetConfirmContent />);
const link = screen.getByRole('link', { name: /request new reset link/i });
expect(link).toBeInTheDocument();
expect(link).toHaveAttribute('href', '/password-reset');
});
it('does not render form when token is missing', () => {
render(<PasswordResetConfirmContent />);
expect(screen.queryByTestId('password-reset-confirm-form')).not.toBeInTheDocument();
});
});
});

View File

@@ -0,0 +1,26 @@
/**
* Tests for Password Reset Confirm Page
* Verifies Suspense wrapper and fallback
*/
import { render, screen } from '@testing-library/react';
import PasswordResetConfirmPage from '@/app/(auth)/password-reset/confirm/page';
// Mock the content component
jest.mock('@/app/(auth)/password-reset/confirm/PasswordResetConfirmContent', () => ({
__esModule: true,
default: () => <div data-testid="password-reset-confirm-content">Content</div>,
}));
describe('PasswordResetConfirmPage', () => {
it('renders without crashing', () => {
render(<PasswordResetConfirmPage />);
expect(screen.getByTestId('password-reset-confirm-content')).toBeInTheDocument();
});
it('wraps content in Suspense boundary', () => {
render(<PasswordResetConfirmPage />);
// Content should render successfully (not fallback)
expect(screen.getByTestId('password-reset-confirm-content')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,37 @@
/**
* Tests for Password Reset Page
* Smoke tests to verify page structure and component rendering
*/
import { render, screen } from '@testing-library/react';
import PasswordResetPage from '@/app/(auth)/password-reset/page';
// Mock dynamic import
jest.mock('next/dynamic', () => ({
__esModule: true,
default: (importFn: () => Promise<any>, options?: any) => {
const Component = () => <div data-testid="password-reset-form">Mocked PasswordResetRequestForm</div>;
Component.displayName = 'PasswordResetRequestForm';
return Component;
},
}));
describe('PasswordResetPage', () => {
it('renders without crashing', () => {
render(<PasswordResetPage />);
expect(screen.getByText('Reset your password')).toBeInTheDocument();
});
it('renders heading and description', () => {
render(<PasswordResetPage />);
expect(screen.getByRole('heading', { name: /reset your password/i })).toBeInTheDocument();
expect(screen.getByText(/we'll send you an email with instructions/i)).toBeInTheDocument();
});
it('renders PasswordResetRequestForm component', () => {
render(<PasswordResetPage />);
expect(screen.getByTestId('password-reset-form')).toBeInTheDocument();
});
});