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([
{ 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', () => {

View File

@ -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);
});
});
});

View File

@ -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 {