Fix tests

This commit is contained in:
James Brunton 2025-09-16 14:10:22 +01:00
parent d3745fb63d
commit ff0e82bb95
3 changed files with 28 additions and 21 deletions

View File

@ -54,25 +54,29 @@ describe('useRotateOperation', () => {
}); });
test.each([ test.each([
{ angle: 0 }, { angle: 0, expectedNormalized: 0 },
{ angle: 90 }, { angle: 90, expectedNormalized: 90 },
{ angle: 180 }, { angle: 180, expectedNormalized: 180 },
{ angle: 270 }, { angle: 270, expectedNormalized: 270 },
])('should create form data correctly with angle $angle', ({ angle }) => { { angle: 360, expectedNormalized: 0 },
{ angle: -90, expectedNormalized: 270 },
{ angle: -180, expectedNormalized: 180 },
{ angle: -270, expectedNormalized: 90 },
{ angle: 450, expectedNormalized: 90 },
])('should create form data correctly with angle $angle (normalized to $expectedNormalized)', ({ angle, expectedNormalized }) => {
renderHook(() => useRotateOperation()); renderHook(() => useRotateOperation());
const callArgs = getToolConfig(); const callArgs = getToolConfig();
const buildFormData = callArgs.buildFormData;
const testParameters: RotateParameters = { angle }; const testParameters: RotateParameters = { angle };
const testFile = new File(['test content'], 'test.pdf', { type: 'application/pdf' }); const testFile = new File(['test content'], 'test.pdf', { type: 'application/pdf' });
const formData = buildFormData(testParameters, testFile); const formData = callArgs.buildFormData(testParameters, testFile);
// Verify the form data contains the file // Verify the form data contains the file
expect(formData.get('fileInput')).toBe(testFile); expect(formData.get('fileInput')).toBe(testFile);
// Verify angle parameter // Verify angle parameter is normalized for backend
expect(formData.get('angle')).toBe(angle.toString()); expect(formData.get('angle')).toBe(expectedNormalized.toString());
}); });
test('should use correct translation for error messages', () => { test('should use correct translation for error messages', () => {

View File

@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest'; import { describe, expect, test } from 'vitest';
import { renderHook, act } from '@testing-library/react'; import { renderHook, act } from '@testing-library/react';
import { useRotateParameters, defaultParameters } from './useRotateParameters'; import { useRotateParameters, defaultParameters, normalizeAngle } from './useRotateParameters';
describe('useRotateParameters', () => { describe('useRotateParameters', () => {
test('should initialize with default parameters', () => { test('should initialize with default parameters', () => {
@ -52,7 +52,8 @@ describe('useRotateParameters', () => {
act(() => { act(() => {
result.current.rotateClockwise(); result.current.rotateClockwise();
}); });
expect(result.current.parameters.angle).toBe(0); // Should wrap around expect(result.current.parameters.angle).toBe(360);
expect(normalizeAngle(result.current.parameters.angle)).toBe(0);
expect(result.current.hasRotation).toBe(false); expect(result.current.hasRotation).toBe(false);
}); });
@ -62,23 +63,24 @@ describe('useRotateParameters', () => {
act(() => { act(() => {
result.current.rotateAnticlockwise(); result.current.rotateAnticlockwise();
}); });
expect(result.current.parameters.angle).toBe(270); expect(result.current.parameters.angle).toBe(-90);
expect(result.current.hasRotation).toBe(true); expect(result.current.hasRotation).toBe(true);
act(() => { act(() => {
result.current.rotateAnticlockwise(); result.current.rotateAnticlockwise();
}); });
expect(result.current.parameters.angle).toBe(180); expect(result.current.parameters.angle).toBe(-180);
act(() => { act(() => {
result.current.rotateAnticlockwise(); result.current.rotateAnticlockwise();
}); });
expect(result.current.parameters.angle).toBe(90); expect(result.current.parameters.angle).toBe(-270);
act(() => { act(() => {
result.current.rotateAnticlockwise(); result.current.rotateAnticlockwise();
}); });
expect(result.current.parameters.angle).toBe(0); // Should wrap around expect(result.current.parameters.angle).toBe(-360);
expect(normalizeAngle(result.current.parameters.angle)).toBe(0);
expect(result.current.hasRotation).toBe(false); expect(result.current.hasRotation).toBe(false);
}); });
@ -113,18 +115,20 @@ describe('useRotateParameters', () => {
expect(result.current.hasRotation).toBe(false); expect(result.current.hasRotation).toBe(false);
}); });
test('should update parameters and normalize angles', () => { test('should update parameters', () => {
const { result } = renderHook(() => useRotateParameters()); const { result } = renderHook(() => useRotateParameters());
act(() => { act(() => {
result.current.updateParameter('angle', 450); result.current.updateParameter('angle', 450);
}); });
expect(result.current.parameters.angle).toBe(90); // Should be normalized expect(result.current.parameters.angle).toBe(450);
expect(normalizeAngle(result.current.parameters.angle)).toBe(90);
act(() => { act(() => {
result.current.updateParameter('angle', -90); result.current.updateParameter('angle', -90);
}); });
expect(result.current.parameters.angle).toBe(270); // Should be normalized expect(result.current.parameters.angle).toBe(-90);
expect(normalizeAngle(result.current.parameters.angle)).toBe(270);
}); });
test('should return correct endpoint name', () => { test('should return correct endpoint name', () => {

View File

@ -4,8 +4,7 @@ import { useMemo, useCallback } from 'react';
// Normalize angle to valid backend values (0, 90, 180, 270) // Normalize angle to valid backend values (0, 90, 180, 270)
export const normalizeAngle = (angle: number): number => { export const normalizeAngle = (angle: number): number => {
const normalized = angle % 360; return ((angle % 360) + 360) % 360;
return normalized < 0 ? normalized + 360 : normalized;
}; };
export interface RotateParameters extends BaseParameters { export interface RotateParameters extends BaseParameters {