52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { renderHook, waitFor } from '@testing-library/react';
|
|
import { useAdmin } from '@/hooks/useAdmin';
|
|
import { inventoryApi } from '@/lib/api';
|
|
import { vi, describe, it, expect } from 'vitest';
|
|
|
|
// Mock the API
|
|
vi.mock('@/lib/api', () => ({
|
|
inventoryApi: {
|
|
getUsers: vi.fn(),
|
|
getCategories: vi.fn(),
|
|
getLdapConfig: vi.fn(),
|
|
getDbBackups: vi.fn(),
|
|
getDbStats: vi.fn(),
|
|
getDbSettings: vi.fn(),
|
|
getAiPrompt: vi.fn(),
|
|
getAiConfig: vi.fn(),
|
|
}
|
|
}));
|
|
|
|
// Mock toast
|
|
vi.mock('react-hot-toast', () => ({
|
|
toast: {
|
|
error: vi.fn(),
|
|
success: vi.fn(),
|
|
}
|
|
}));
|
|
|
|
describe('useAdmin hook', () => {
|
|
it('should initialize with loading state and fetch data', async () => {
|
|
(inventoryApi.getUsers as any).mockResolvedValue([{ id: 1, username: 'testuser' }]);
|
|
(inventoryApi.getCategories as any).mockResolvedValue([]);
|
|
(inventoryApi.getLdapConfig as any).mockResolvedValue({ server_uri: 'ldap://fake' });
|
|
(inventoryApi.getDbBackups as any).mockResolvedValue([]);
|
|
(inventoryApi.getDbStats as any).mockResolvedValue({ backup_count: 0 });
|
|
(inventoryApi.getDbSettings as any).mockResolvedValue({ retention_count: 5 });
|
|
(inventoryApi.getAiPrompt as any).mockResolvedValue({ value: 'test prompt' });
|
|
(inventoryApi.getAiConfig as any).mockResolvedValue({ active_provider: 'gemini' });
|
|
|
|
const { result } = renderHook(() => useAdmin());
|
|
|
|
expect(result.current.loading).toBe(true);
|
|
|
|
await waitFor(() => {
|
|
expect(result.current.loading).toBe(false);
|
|
});
|
|
|
|
expect(result.current.users).toHaveLength(1);
|
|
expect(result.current.users[0].username).toBe('testuser');
|
|
expect(result.current.aiPrompt).toBe('test prompt');
|
|
});
|
|
});
|