Fix tests

This commit is contained in:
James 2025-08-06 17:00:48 +01:00
parent addaf6f751
commit 201f6cb6ab

View File

@ -20,6 +20,16 @@ vi.mock('react-i18next', () => ({
}) })
})); }));
// Mock FileContext
vi.mock('../../../contexts/FileContext', () => ({
useFileContext: () => ({
recordOperation: vi.fn(),
markOperationApplied: vi.fn(),
markOperationFailed: vi.fn(),
addFiles: vi.fn()
})
}));
// Mock fetch // Mock fetch
const mockFetch = vi.fn(); const mockFetch = vi.fn();
globalThis.fetch = mockFetch; globalThis.fetch = mockFetch;
@ -29,6 +39,11 @@ globalThis.URL.createObjectURL = vi.fn(() => 'mock-blob-url');
globalThis.URL.revokeObjectURL = vi.fn(); globalThis.URL.revokeObjectURL = vi.fn();
describe('useSanitizeOperation', () => { describe('useSanitizeOperation', () => {
const mockGenerateSanitizedFileName = (originalFileName?: string): string => {
const baseName = originalFileName?.replace(/\.[^/.]+$/, '') || 'document';
return `sanitized_${baseName}.pdf`;
};
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
}); });
@ -68,7 +83,7 @@ describe('useSanitizeOperation', () => {
const testFile = new File(['test'], 'test.pdf', { type: 'application/pdf' }); const testFile = new File(['test'], 'test.pdf', { type: 'application/pdf' });
await act(async () => { await act(async () => {
await result.current.executeOperation(parameters, [testFile]); await result.current.executeOperation(parameters, [testFile], mockGenerateSanitizedFileName);
}); });
expect(mockFetch).toHaveBeenCalledWith('/api/v1/security/sanitize-pdf', { expect(mockFetch).toHaveBeenCalledWith('/api/v1/security/sanitize-pdf', {
@ -104,14 +119,14 @@ describe('useSanitizeOperation', () => {
await act(async () => { await act(async () => {
try { try {
await result.current.executeOperation(parameters, [testFile]); await result.current.executeOperation(parameters, [testFile], mockGenerateSanitizedFileName);
} catch (error) { } catch (error) {
// Expected to throw // Expected to throw
} }
}); });
expect(result.current.isLoading).toBe(false); expect(result.current.isLoading).toBe(false);
expect(result.current.errorMessage).toBe('Sanitization failed: Server error'); expect(result.current.errorMessage).toBe('Failed to sanitize all files: test.pdf');
expect(result.current.downloadUrl).toBe(null); expect(result.current.downloadUrl).toBe(null);
expect(result.current.status).toBe(null); expect(result.current.status).toBe(null);
}); });
@ -131,7 +146,7 @@ describe('useSanitizeOperation', () => {
let thrownError: Error | null = null; let thrownError: Error | null = null;
await act(async () => { await act(async () => {
try { try {
await result.current.executeOperation(parameters, []); await result.current.executeOperation(parameters, [], mockGenerateSanitizedFileName);
} catch (error) { } catch (error) {
thrownError = error as Error; thrownError = error as Error;
} }
@ -165,7 +180,7 @@ describe('useSanitizeOperation', () => {
const testFile = new File(['test'], 'test.pdf', { type: 'application/pdf' }); const testFile = new File(['test'], 'test.pdf', { type: 'application/pdf' });
await act(async () => { await act(async () => {
await result.current.executeOperation(parameters, [testFile]); await result.current.executeOperation(parameters, [testFile], mockGenerateSanitizedFileName);
}); });
const [url, options] = mockFetch.mock.calls[0]; const [url, options] = mockFetch.mock.calls[0];
@ -219,13 +234,13 @@ describe('useSanitizeOperation', () => {
// Trigger an API error // Trigger an API error
await act(async () => { await act(async () => {
try { try {
await result.current.executeOperation(parameters, [testFile]); await result.current.executeOperation(parameters, [testFile], mockGenerateSanitizedFileName);
} catch (error) { } catch (error) {
// Expected to throw // Expected to throw
} }
}); });
expect(result.current.errorMessage).toBeTruthy(); expect(result.current.errorMessage).toBe('Failed to sanitize all files: test.pdf');
act(() => { act(() => {
result.current.clearError(); result.current.clearError();