test(frontend): improve test coverage and update edge case handling

- Refactor tests to handle empty `model_params` in AgentTypeForm.
- Add return type annotations (`: never`) for throwing functions in ErrorBoundary tests.
- Mock `useAuth` in home page tests for consistent auth state handling.
- Update Header test to validate updated `/dashboard` link.
This commit is contained in:
2026-01-03 01:19:35 +01:00
parent c72f6aa2f9
commit a79d923dc1
4 changed files with 18 additions and 9 deletions

View File

@@ -55,6 +55,15 @@ jest.mock('@/lib/api/hooks/useAuth', () => ({
})), })),
})); }));
// Mock AuthContext - Home page uses useAuth to check if user is authenticated
jest.mock('@/lib/auth/AuthContext', () => ({
useAuth: jest.fn(() => ({
isAuthenticated: false,
isLoading: false,
user: null,
})),
}));
// Mock Theme components // Mock Theme components
jest.mock('@/components/theme', () => ({ jest.mock('@/components/theme', () => ({
ThemeToggle: () => <div data-testid="theme-toggle">Theme Toggle</div>, ThemeToggle: () => <div data-testid="theme-toggle">Theme Toggle</div>,

View File

@@ -549,14 +549,14 @@ describe('AgentTypeForm', () => {
}); });
}); });
describe('Null Model Params Handling', () => { describe('Empty Model Params Handling', () => {
it('handles null model_params gracefully', () => { it('handles empty model_params gracefully', () => {
const agentTypeWithNullParams: AgentTypeResponse = { const agentTypeWithEmptyParams: AgentTypeResponse = {
...mockAgentType, ...mockAgentType,
model_params: null, model_params: {},
}; };
render(<AgentTypeForm {...defaultProps} agentType={agentTypeWithNullParams} />); render(<AgentTypeForm {...defaultProps} agentType={agentTypeWithEmptyParams} />);
// Should render without errors // Should render without errors
expect(screen.getByText('Edit Agent Type')).toBeInTheDocument(); expect(screen.getByText('Edit Agent Type')).toBeInTheDocument();

View File

@@ -248,7 +248,7 @@ describe('ErrorBoundary', () => {
describe('error without message', () => { describe('error without message', () => {
it('handles error with null message gracefully', () => { it('handles error with null message gracefully', () => {
function ThrowNullError() { function ThrowNullError(): never {
const error = new Error(); const error = new Error();
error.message = ''; error.message = '';
throw error; throw error;
@@ -338,7 +338,7 @@ describe('ErrorBoundary', () => {
describe('edge cases', () => { describe('edge cases', () => {
it('handles deeply nested errors', () => { it('handles deeply nested errors', () => {
function DeepChild() { function DeepChild(): never {
throw new Error('Deep error'); throw new Error('Deep error');
} }
@@ -377,7 +377,7 @@ describe('ErrorBoundary', () => {
}); });
it('allows nested error boundaries', () => { it('allows nested error boundaries', () => {
function InnerThrowing() { function InnerThrowing(): never {
throw new Error('Inner error'); throw new Error('Inner error');
} }

View File

@@ -124,7 +124,7 @@ describe('Header', () => {
render(<Header />); render(<Header />);
const homeLink = screen.getByRole('link', { name: /home/i }); const homeLink = screen.getByRole('link', { name: /home/i });
expect(homeLink).toHaveAttribute('href', '/'); expect(homeLink).toHaveAttribute('href', '/dashboard');
}); });
it('renders admin link for superusers', () => { it('renders admin link for superusers', () => {