forked from cardosofelipe/fast-next-template
Add comprehensive test coverage for dashboard components: - Dashboard.test.tsx: Main component integration tests - WelcomeHeader.test.tsx: User greeting and time-based messages - DashboardQuickStats.test.tsx: Stats cards rendering and links - RecentProjects.test.tsx: Project cards grid and navigation - PendingApprovals.test.tsx: Approval items and actions - EmptyState.test.tsx: New user onboarding experience 46 tests covering rendering, interactions, and edge cases. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
132 lines
3.5 KiB
TypeScript
132 lines
3.5 KiB
TypeScript
/**
|
|
* WelcomeHeader Component Tests
|
|
*/
|
|
|
|
import { render, screen } from '@testing-library/react';
|
|
import { WelcomeHeader } from '@/components/dashboard/WelcomeHeader';
|
|
import { useAuth } from '@/lib/auth/AuthContext';
|
|
|
|
// Mock useAuth hook
|
|
jest.mock('@/lib/auth/AuthContext', () => ({
|
|
useAuth: jest.fn(),
|
|
}));
|
|
|
|
// Mock next-intl navigation
|
|
jest.mock('@/lib/i18n/routing', () => ({
|
|
Link: ({ children, href }: { children: React.ReactNode; href: string }) => (
|
|
<a href={href}>{children}</a>
|
|
),
|
|
}));
|
|
|
|
const mockUseAuth = useAuth as jest.MockedFunction<typeof useAuth>;
|
|
|
|
describe('WelcomeHeader', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('displays greeting with user first name', () => {
|
|
mockUseAuth.mockReturnValue({
|
|
user: { id: '1', email: 'john@example.com', first_name: 'John', is_active: true, is_superuser: false, created_at: '' },
|
|
isAuthenticated: true,
|
|
isLoading: false,
|
|
error: null,
|
|
login: jest.fn(),
|
|
logout: jest.fn(),
|
|
clearError: jest.fn(),
|
|
checkAuth: jest.fn(),
|
|
});
|
|
|
|
render(<WelcomeHeader />);
|
|
|
|
expect(screen.getByText(/John/)).toBeInTheDocument();
|
|
});
|
|
|
|
it('falls back to email prefix when first_name is empty', () => {
|
|
mockUseAuth.mockReturnValue({
|
|
user: { id: '1', email: 'jane@example.com', first_name: '', is_active: true, is_superuser: false, created_at: '' },
|
|
isAuthenticated: true,
|
|
isLoading: false,
|
|
error: null,
|
|
login: jest.fn(),
|
|
logout: jest.fn(),
|
|
clearError: jest.fn(),
|
|
checkAuth: jest.fn(),
|
|
});
|
|
|
|
render(<WelcomeHeader />);
|
|
|
|
expect(screen.getByText(/jane/)).toBeInTheDocument();
|
|
});
|
|
|
|
it('displays default greeting when no user', () => {
|
|
mockUseAuth.mockReturnValue({
|
|
user: null,
|
|
isAuthenticated: false,
|
|
isLoading: false,
|
|
error: null,
|
|
login: jest.fn(),
|
|
logout: jest.fn(),
|
|
clearError: jest.fn(),
|
|
checkAuth: jest.fn(),
|
|
});
|
|
|
|
render(<WelcomeHeader />);
|
|
|
|
expect(screen.getByText(/there/)).toBeInTheDocument();
|
|
});
|
|
|
|
it('displays subtitle text', () => {
|
|
mockUseAuth.mockReturnValue({
|
|
user: { id: '1', email: 'test@example.com', first_name: 'Test', is_active: true, is_superuser: false, created_at: '' },
|
|
isAuthenticated: true,
|
|
isLoading: false,
|
|
error: null,
|
|
login: jest.fn(),
|
|
logout: jest.fn(),
|
|
clearError: jest.fn(),
|
|
checkAuth: jest.fn(),
|
|
});
|
|
|
|
render(<WelcomeHeader />);
|
|
|
|
expect(screen.getByText(/Here's what's happening/)).toBeInTheDocument();
|
|
});
|
|
|
|
it('displays Create Project button', () => {
|
|
mockUseAuth.mockReturnValue({
|
|
user: { id: '1', email: 'test@example.com', first_name: 'Test', is_active: true, is_superuser: false, created_at: '' },
|
|
isAuthenticated: true,
|
|
isLoading: false,
|
|
error: null,
|
|
login: jest.fn(),
|
|
logout: jest.fn(),
|
|
clearError: jest.fn(),
|
|
checkAuth: jest.fn(),
|
|
});
|
|
|
|
render(<WelcomeHeader />);
|
|
|
|
const createButton = screen.getByRole('link', { name: /Create Project/i });
|
|
expect(createButton).toBeInTheDocument();
|
|
expect(createButton).toHaveAttribute('href', '/projects/new');
|
|
});
|
|
|
|
it('applies custom className', () => {
|
|
mockUseAuth.mockReturnValue({
|
|
user: null,
|
|
isAuthenticated: false,
|
|
isLoading: false,
|
|
error: null,
|
|
login: jest.fn(),
|
|
logout: jest.fn(),
|
|
clearError: jest.fn(),
|
|
checkAuth: jest.fn(),
|
|
});
|
|
|
|
const { container } = render(<WelcomeHeader className="custom-class" />);
|
|
|
|
expect(container.firstChild).toHaveClass('custom-class');
|
|
});
|
|
});
|