From ff0e82bb95a66396c0abe248c21ab9dbe3ffe313 Mon Sep 17 00:00:00 2001 From: James Brunton Date: Tue, 16 Sep 2025 14:10:22 +0100 Subject: [PATCH] Fix tests --- .../tools/rotate/useRotateOperation.test.ts | 22 ++++++++++------- .../tools/rotate/useRotateParameters.test.ts | 24 +++++++++++-------- .../hooks/tools/rotate/useRotateParameters.ts | 3 +-- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/frontend/src/hooks/tools/rotate/useRotateOperation.test.ts b/frontend/src/hooks/tools/rotate/useRotateOperation.test.ts index beccd8c64..642fdbada 100644 --- a/frontend/src/hooks/tools/rotate/useRotateOperation.test.ts +++ b/frontend/src/hooks/tools/rotate/useRotateOperation.test.ts @@ -54,25 +54,29 @@ describe('useRotateOperation', () => { }); test.each([ - { angle: 0 }, - { angle: 90 }, - { angle: 180 }, - { angle: 270 }, - ])('should create form data correctly with angle $angle', ({ angle }) => { + { angle: 0, expectedNormalized: 0 }, + { angle: 90, expectedNormalized: 90 }, + { angle: 180, expectedNormalized: 180 }, + { angle: 270, expectedNormalized: 270 }, + { 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()); const callArgs = getToolConfig(); - const buildFormData = callArgs.buildFormData; const testParameters: RotateParameters = { angle }; 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 expect(formData.get('fileInput')).toBe(testFile); - // Verify angle parameter - expect(formData.get('angle')).toBe(angle.toString()); + // Verify angle parameter is normalized for backend + expect(formData.get('angle')).toBe(expectedNormalized.toString()); }); test('should use correct translation for error messages', () => { diff --git a/frontend/src/hooks/tools/rotate/useRotateParameters.test.ts b/frontend/src/hooks/tools/rotate/useRotateParameters.test.ts index 8b0169611..6d3393fcc 100644 --- a/frontend/src/hooks/tools/rotate/useRotateParameters.test.ts +++ b/frontend/src/hooks/tools/rotate/useRotateParameters.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from 'vitest'; import { renderHook, act } from '@testing-library/react'; -import { useRotateParameters, defaultParameters } from './useRotateParameters'; +import { useRotateParameters, defaultParameters, normalizeAngle } from './useRotateParameters'; describe('useRotateParameters', () => { test('should initialize with default parameters', () => { @@ -52,7 +52,8 @@ describe('useRotateParameters', () => { act(() => { 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); }); @@ -62,23 +63,24 @@ describe('useRotateParameters', () => { act(() => { result.current.rotateAnticlockwise(); }); - expect(result.current.parameters.angle).toBe(270); + expect(result.current.parameters.angle).toBe(-90); expect(result.current.hasRotation).toBe(true); act(() => { result.current.rotateAnticlockwise(); }); - expect(result.current.parameters.angle).toBe(180); + expect(result.current.parameters.angle).toBe(-180); act(() => { result.current.rotateAnticlockwise(); }); - expect(result.current.parameters.angle).toBe(90); + expect(result.current.parameters.angle).toBe(-270); act(() => { 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); }); @@ -113,18 +115,20 @@ describe('useRotateParameters', () => { expect(result.current.hasRotation).toBe(false); }); - test('should update parameters and normalize angles', () => { + test('should update parameters', () => { const { result } = renderHook(() => useRotateParameters()); act(() => { 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(() => { 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', () => { @@ -153,4 +157,4 @@ describe('useRotateParameters', () => { } expect(result.current.hasRotation).toBe(false); }); -}); \ No newline at end of file +}); diff --git a/frontend/src/hooks/tools/rotate/useRotateParameters.ts b/frontend/src/hooks/tools/rotate/useRotateParameters.ts index a2c7ccaa9..decc574c8 100644 --- a/frontend/src/hooks/tools/rotate/useRotateParameters.ts +++ b/frontend/src/hooks/tools/rotate/useRotateParameters.ts @@ -4,8 +4,7 @@ import { useMemo, useCallback } from 'react'; // Normalize angle to valid backend values (0, 90, 180, 270) export const normalizeAngle = (angle: number): number => { - const normalized = angle % 360; - return normalized < 0 ? normalized + 360 : normalized; + return ((angle % 360) + 360) % 360; }; export interface RotateParameters extends BaseParameters {