fix(unit): fix more broken tests

This commit is contained in:
perf3ct 2025-06-18 02:55:35 +00:00
parent 1ec8235f63
commit def227c2ef
No known key found for this signature in database
GPG Key ID: 569C4EEC436F5232
4 changed files with 48 additions and 41 deletions

View File

@ -7,10 +7,12 @@ import GlobalSearchBar from '../GlobalSearchBar';
import { documentService } from '../../../services/api'; import { documentService } from '../../../services/api';
// Mock the API service // Mock the API service
const mockDocumentService = {
enhancedSearch: vi.fn(),
};
vi.mock('../../../services/api', () => ({ vi.mock('../../../services/api', () => ({
documentService: { documentService: mockDocumentService,
enhancedSearch: vi.fn(),
}
})); }));
// Mock useNavigate // Mock useNavigate
@ -76,7 +78,7 @@ describe('GlobalSearchBar', () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
localStorageMock.getItem.mockReturnValue(null); localStorageMock.getItem.mockReturnValue(null);
(documentService.enhancedSearch as any).mockResolvedValue(mockSearchResponse); mockDocumentService.enhancedSearch.mockResolvedValue(mockSearchResponse);
}); });
test('renders search input with placeholder', () => { test('renders search input with placeholder', () => {
@ -265,7 +267,7 @@ describe('GlobalSearchBar', () => {
test('shows "View all results" link when there are many results', async () => { test('shows "View all results" link when there are many results', async () => {
// Mock response with 5 or more results to trigger the link // Mock response with 5 or more results to trigger the link
(documentService.enhancedSearch as any).mockResolvedValue({ mockDocumentService.enhancedSearch.mockResolvedValue({
data: { data: {
documents: Array.from({ length: 5 }, (_, i) => ({ documents: Array.from({ length: 5 }, (_, i) => ({
id: `${i + 1}`, id: `${i + 1}`,

View File

@ -104,9 +104,10 @@ const MimeTypeFacetFilter: React.FC<MimeTypeFacetFilterProps> = ({
try { try {
setLoading(true); setLoading(true);
const response = await documentService.getFacets(); const response = await documentService.getFacets();
setFacets(response.data.mime_types); setFacets(response.data.mime_types || []);
} catch (error) { } catch (error) {
console.error('Failed to load facets:', error); console.error('Failed to load facets:', error);
setFacets([]);
} finally { } finally {
setLoading(false); setLoading(false);
} }
@ -170,7 +171,7 @@ const MimeTypeFacetFilter: React.FC<MimeTypeFacetFilterProps> = ({
} }
}; };
const filteredFacets = facets.filter(facet => const filteredFacets = (facets || []).filter(facet =>
searchTerm === '' || searchTerm === '' ||
facet.value.toLowerCase().includes(searchTerm.toLowerCase()) || facet.value.toLowerCase().includes(searchTerm.toLowerCase()) ||
getMimeTypeLabel(facet.value).toLowerCase().includes(searchTerm.toLowerCase()) getMimeTypeLabel(facet.value).toLowerCase().includes(searchTerm.toLowerCase())

View File

@ -6,13 +6,15 @@ import SearchPage from '../SearchPage';
import { documentService } from '../../services/api'; import { documentService } from '../../services/api';
// Mock the document service // Mock the document service
const mockDocumentService = {
search: vi.fn(),
enhancedSearch: vi.fn(),
getFacets: vi.fn(),
download: vi.fn(),
};
vi.mock('../../services/api', () => ({ vi.mock('../../services/api', () => ({
documentService: { documentService: mockDocumentService,
search: vi.fn(),
enhancedSearch: vi.fn(),
getFacets: vi.fn(),
download: vi.fn(),
},
})); }));
const mockSearchResponse = { const mockSearchResponse = {
@ -87,9 +89,9 @@ const renderSearchPage = () => {
describe('SearchPage Integration Tests', () => { describe('SearchPage Integration Tests', () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
(documentService.enhancedSearch as any).mockResolvedValue(mockSearchResponse); mockDocumentService.enhancedSearch.mockResolvedValue(mockSearchResponse);
(documentService.search as any).mockResolvedValue(mockSearchResponse); mockDocumentService.search.mockResolvedValue(mockSearchResponse);
(documentService.getFacets as any).mockResolvedValue(mockFacetsResponse); mockDocumentService.getFacets.mockResolvedValue(mockFacetsResponse);
}); });
test('performs complete search workflow', async () => { test('performs complete search workflow', async () => {
@ -102,7 +104,7 @@ describe('SearchPage Integration Tests', () => {
}); });
// Enter search query // Enter search query
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
await user.type(searchInput, 'invoice'); await user.type(searchInput, 'invoice');
// Wait for search results // Wait for search results
@ -137,7 +139,7 @@ describe('SearchPage Integration Tests', () => {
}); });
// Enter search query first // Enter search query first
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
await user.type(searchInput, 'invoice'); await user.type(searchInput, 'invoice');
// Wait for initial results // Wait for initial results
@ -187,7 +189,7 @@ describe('SearchPage Integration Tests', () => {
await user.click(screen.getByText('Long (400 chars)')); await user.click(screen.getByText('Long (400 chars)'));
// Perform search // Perform search
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
await user.type(searchInput, 'invoice'); await user.type(searchInput, 'invoice');
// Verify advanced settings are applied // Verify advanced settings are applied
@ -207,7 +209,7 @@ describe('SearchPage Integration Tests', () => {
renderSearchPage(); renderSearchPage();
// Perform search // Perform search
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
await user.type(searchInput, 'invoice'); await user.type(searchInput, 'invoice');
// Wait for results with snippets // Wait for results with snippets
@ -245,7 +247,7 @@ describe('SearchPage Integration Tests', () => {
await user.click(exampleButtons[0]); await user.click(exampleButtons[0]);
// Verify search input is populated // Verify search input is populated
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
expect(searchInput).toHaveValue('invoice'); expect(searchInput).toHaveValue('invoice');
// Verify search is triggered // Verify search is triggered
@ -264,7 +266,7 @@ describe('SearchPage Integration Tests', () => {
renderSearchPage(); renderSearchPage();
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
await user.type(searchInput, 'invoice'); await user.type(searchInput, 'invoice');
// Should show error message // Should show error message
@ -278,7 +280,7 @@ describe('SearchPage Integration Tests', () => {
renderSearchPage(); renderSearchPage();
// Perform search first // Perform search first
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
await user.type(searchInput, 'invoice'); await user.type(searchInput, 'invoice');
// Wait for results // Wait for results
@ -299,7 +301,7 @@ describe('SearchPage Integration Tests', () => {
const user = userEvent.setup(); const user = userEvent.setup();
renderSearchPage(); renderSearchPage();
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
await user.type(searchInput, 'invoice'); await user.type(searchInput, 'invoice');
// Wait for suggestions to appear // Wait for suggestions to appear
@ -327,7 +329,7 @@ describe('SearchPage Integration Tests', () => {
}); });
// Enter search query // Enter search query
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
await user.type(searchInput, 'invoice'); await user.type(searchInput, 'invoice');
// Apply PDF filter // Apply PDF filter
@ -388,10 +390,10 @@ describe('SearchPage Integration Tests', () => {
}, },
}; };
(documentService.enhancedSearch as any).mockResolvedValue(emptyResponse); mockDocumentService.enhancedSearch.mockResolvedValue(emptyResponse);
renderSearchPage(); renderSearchPage();
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
await user.type(searchInput, 'nonexistent'); await user.type(searchInput, 'nonexistent');
await waitFor(() => { await waitFor(() => {
@ -403,7 +405,7 @@ describe('SearchPage Integration Tests', () => {
const user = userEvent.setup(); const user = userEvent.setup();
renderSearchPage(); renderSearchPage();
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
await user.type(searchInput, 'invoice'); await user.type(searchInput, 'invoice');
// Verify URL is updated (this would require checking window.location or using a memory router) // Verify URL is updated (this would require checking window.location or using a memory router)
@ -416,15 +418,15 @@ describe('SearchPage Integration Tests', () => {
describe('SearchPage Performance Tests', () => { describe('SearchPage Performance Tests', () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
(documentService.enhancedSearch as any).mockResolvedValue(mockSearchResponse); mockDocumentService.enhancedSearch.mockResolvedValue(mockSearchResponse);
(documentService.getFacets as any).mockResolvedValue(mockFacetsResponse); mockDocumentService.getFacets.mockResolvedValue(mockFacetsResponse);
}); });
test('debounces search input to avoid excessive API calls', async () => { test('debounces search input to avoid excessive API calls', async () => {
const user = userEvent.setup(); const user = userEvent.setup();
renderSearchPage(); renderSearchPage();
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
// Type quickly // Type quickly
await user.type(searchInput, 'invoice', { delay: 50 }); await user.type(searchInput, 'invoice', { delay: 50 });
@ -452,7 +454,7 @@ describe('SearchPage Performance Tests', () => {
renderSearchPage(); renderSearchPage();
const searchInput = screen.getByPlaceholderText(/search your documents/i); const searchInput = screen.getByPlaceholderText(/search documents/i);
await user.type(searchInput, 'invoice'); await user.type(searchInput, 'invoice');
// Should show loading indicator // Should show loading indicator

View File

@ -6,12 +6,14 @@ import SearchPage from '../SearchPage';
import { documentService } from '../../services/api'; import { documentService } from '../../services/api';
// Mock the API service // Mock the API service
const mockDocumentService = {
enhancedSearch: vi.fn(),
search: vi.fn(),
download: vi.fn(),
};
vi.mock('../../services/api', () => ({ vi.mock('../../services/api', () => ({
documentService: { documentService: mockDocumentService,
enhancedSearch: vi.fn(),
search: vi.fn(),
download: vi.fn(),
}
})); }));
// Mock SearchGuidance component // Mock SearchGuidance component
@ -79,8 +81,8 @@ const renderWithRouter = (component) => {
describe('SearchPage', () => { describe('SearchPage', () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
(documentService.enhancedSearch as any).mockResolvedValue(mockSearchResponse); mockDocumentService.enhancedSearch.mockResolvedValue(mockSearchResponse);
(documentService.search as any).mockResolvedValue(mockSearchResponse); mockDocumentService.search.mockResolvedValue(mockSearchResponse);
}); });
test('renders search page with prominent search bar', () => { test('renders search page with prominent search bar', () => {
@ -387,7 +389,7 @@ describe('SearchPage', () => {
test('handles document download', async () => { test('handles document download', async () => {
const user = userEvent.setup(); const user = userEvent.setup();
const mockBlob = new Blob(['test content'], { type: 'application/pdf' }); const mockBlob = new Blob(['test content'], { type: 'application/pdf' });
(documentService.download as any).mockResolvedValue({ data: mockBlob }); mockDocumentService.download.mockResolvedValue({ data: mockBlob });
// Mock URL.createObjectURL // Mock URL.createObjectURL
global.URL.createObjectURL = vi.fn(() => 'mock-url'); global.URL.createObjectURL = vi.fn(() => 'mock-url');
@ -516,7 +518,7 @@ describe('Enhanced Search Features', () => {
const user = userEvent.setup(); const user = userEvent.setup();
// Mock empty response // Mock empty response
(documentService.enhancedSearch as any).mockResolvedValue({ mockDocumentService.enhancedSearch.mockResolvedValue({
data: { data: {
documents: [], documents: [],
total: 0, total: 0,