mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-11-16 01:21:16 +01:00
# Description of Changes Redesigns `ToolOperationConfig` so that the types of the functions are always known depending on whether the tool runs on single files, multiple files, or uses custom behaviour
108 lines
3.4 KiB
TypeScript
108 lines
3.4 KiB
TypeScript
import { describe, expect, test, vi, beforeEach } from 'vitest';
|
|
import { renderHook } from '@testing-library/react';
|
|
import { useRemovePasswordOperation } from './useRemovePasswordOperation';
|
|
import type { RemovePasswordParameters } from './useRemovePasswordParameters';
|
|
|
|
vi.mock('../shared/useToolOperation', async () => {
|
|
const actual = await vi.importActual('../shared/useToolOperation'); // Need to keep ToolType etc.
|
|
return {
|
|
...actual,
|
|
useToolOperation: vi.fn()
|
|
};
|
|
});
|
|
|
|
// Mock the translation hook
|
|
const mockT = vi.fn((key: string) => `translated-${key}`);
|
|
vi.mock('react-i18next', () => ({
|
|
useTranslation: () => ({ t: mockT })
|
|
}));
|
|
|
|
// Mock the error handler
|
|
vi.mock('../../../utils/toolErrorHandler', () => ({
|
|
createStandardErrorHandler: vi.fn(() => 'error-handler-function')
|
|
}));
|
|
|
|
// Import the mocked function
|
|
import { SingleFileToolOperationConfig, ToolOperationHook, ToolType, useToolOperation } from '../shared/useToolOperation';
|
|
|
|
describe('useRemovePasswordOperation', () => {
|
|
const mockUseToolOperation = vi.mocked(useToolOperation);
|
|
|
|
const getToolConfig = () => mockUseToolOperation.mock.calls[0][0] as SingleFileToolOperationConfig<RemovePasswordParameters>;
|
|
|
|
const mockToolOperationReturn: ToolOperationHook<unknown> = {
|
|
files: [],
|
|
thumbnails: [],
|
|
downloadUrl: null,
|
|
downloadFilename: '',
|
|
isLoading: false,
|
|
errorMessage: null,
|
|
status: '',
|
|
isGeneratingThumbnails: false,
|
|
progress: null,
|
|
executeOperation: vi.fn(),
|
|
resetResults: vi.fn(),
|
|
clearError: vi.fn(),
|
|
cancelOperation: vi.fn(),
|
|
};
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mockUseToolOperation.mockReturnValue(mockToolOperationReturn);
|
|
});
|
|
|
|
test.each([
|
|
{
|
|
description: 'with valid password',
|
|
password: 'test-password'
|
|
},
|
|
{
|
|
description: 'with complex password',
|
|
password: 'C0mpl3x@P@ssw0rd!'
|
|
},
|
|
{
|
|
description: 'with single character password',
|
|
password: 'a'
|
|
}
|
|
])('should create form data correctly $description', ({ password }) => {
|
|
renderHook(() => useRemovePasswordOperation());
|
|
|
|
const callArgs = getToolConfig();
|
|
const buildFormData = callArgs.buildFormData;
|
|
|
|
const testParameters: RemovePasswordParameters = {
|
|
password
|
|
};
|
|
|
|
const testFile = new File(['test content'], 'test.pdf', { type: 'application/pdf' });
|
|
const formData = buildFormData(testParameters, testFile as any);
|
|
|
|
// Verify the form data contains the file
|
|
expect(formData.get('fileInput')).toBe(testFile);
|
|
|
|
// Verify password parameter
|
|
expect(formData.get('password')).toBe(password);
|
|
});
|
|
|
|
test('should use correct translation for error messages', () => {
|
|
renderHook(() => useRemovePasswordOperation());
|
|
|
|
expect(mockT).toHaveBeenCalledWith(
|
|
'removePassword.error.failed',
|
|
'An error occurred while removing the password from the PDF.'
|
|
);
|
|
});
|
|
|
|
test.each([
|
|
{ property: 'toolType' as const, expectedValue: ToolType.singleFile },
|
|
{ property: 'endpoint' as const, expectedValue: '/api/v1/security/remove-password' },
|
|
{ property: 'filePrefix' as const, expectedValue: 'translated-removePassword.filenamePrefix_' },
|
|
{ property: 'operationType' as const, expectedValue: 'removePassword' }
|
|
])('should configure $property correctly', ({ property, expectedValue }) => {
|
|
renderHook(() => useRemovePasswordOperation());
|
|
|
|
const callArgs = getToolConfig();
|
|
expect(callArgs[property]).toBe(expectedValue);
|
|
});
|
|
});
|