From b688eb27d0e59bc99768e003bdd8fb4f48060f84 Mon Sep 17 00:00:00 2001 From: Chris Tran <6634746+sYnVerse@users.noreply.github.com> Date: Sat, 6 Jun 2026 14:24:25 -0700 Subject: [PATCH] Add crop-image sizing utilities and tests Introduce getViewportDimensions and getBaseDimensions utility functions (src/tools/crop-image/crop-image.service.ts) to centralize viewport/base sizing logic and handle missing natural dimensions. Refactor crop-image.vue to consume these utilities for computed viewport and base dimensions. Add unit tests covering landscape/portrait calculations and cover scaling (src/tools/crop-image/crop-image.service.test.ts) and expand the e2e spec to upload a 1x1 PNG and assert presence of the cropper, sliders and control buttons (src/tools/crop-image/crop-image.e2e.spec.ts). Improves testability and keeps sizing logic consistent across the component. --- src/tools/crop-image/crop-image.e2e.spec.ts | 34 ++++++++++++- .../crop-image/crop-image.service.test.ts | 46 +++++++++++++++-- src/tools/crop-image/crop-image.service.ts | 50 +++++++++++++++++++ src/tools/crop-image/crop-image.vue | 44 ++++------------ 4 files changed, 135 insertions(+), 39 deletions(-) diff --git a/src/tools/crop-image/crop-image.e2e.spec.ts b/src/tools/crop-image/crop-image.e2e.spec.ts index e614293f..446bbeec 100644 --- a/src/tools/crop-image/crop-image.e2e.spec.ts +++ b/src/tools/crop-image/crop-image.e2e.spec.ts @@ -9,7 +9,39 @@ test.describe('Tool - Crop image', () => { await expect(page).toHaveTitle('Crop image - IT Tools'); }); - test('', async ({ page }) => { + test('Upload an image and interact with crop controls', async ({ page }) => { + // Check that we start in upload mode + const fileInput = page.locator('input[type="file"]'); + await expect(fileInput).toBeAttached(); + // 1x1 transparent PNG buffer + const pixelPng = Buffer.from( + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==', + 'base64' + ); + + // Upload the file + await fileInput.setInputFiles({ + name: 'test.png', + mimeType: 'image/png', + buffer: pixelPng, + }); + + // Interactive cropper grid should now be visible + const viewportBox = page.locator('.viewport-box'); + await expect(viewportBox).toBeVisible(); + + // Check sliders exist for zooming and rotation + const sliders = page.locator('.n-slider'); + await expect(sliders).toHaveCount(2); + + // Export button should be visible + const exportButton = page.getByRole('button', { name: /Export & Download Cropped Image/i }); + await expect(exportButton).toBeVisible(); + + // Change image button should be visible + const changeImageButton = page.getByRole('button', { name: /Change Image/i }); + await expect(changeImageButton).toBeVisible(); }); }); + diff --git a/src/tools/crop-image/crop-image.service.test.ts b/src/tools/crop-image/crop-image.service.test.ts index 451fbde1..126d7b3a 100644 --- a/src/tools/crop-image/crop-image.service.test.ts +++ b/src/tools/crop-image/crop-image.service.test.ts @@ -1,7 +1,47 @@ import { describe, expect, it } from 'vitest'; +import { getViewportDimensions, getBaseDimensions } from './crop-image.service'; -describe('crop-image service', () => { - it('should pass', () => { - expect(true).toBe(true); +describe('crop-image service utilities', () => { + describe('getViewportDimensions', () => { + it('calculates landscape/square ratios correctly', () => { + // 1:1 ratio + const sq = getViewportDimensions(1, 400, 400); + expect(sq).toEqual({ width: 400, height: 400 }); + + // 2:1 ratio (landscape) + const ls = getViewportDimensions(2, 400, 400); + expect(ls).toEqual({ width: 400, height: 200 }); + }); + + it('calculates portrait ratios correctly', () => { + // 1:2 ratio (portrait) + const pt = getViewportDimensions(0.5, 400, 400); + expect(pt).toEqual({ width: 200, height: 400 }); + }); + }); + + describe('getBaseDimensions', () => { + it('returns viewport dimensions when natural dimensions are zero or missing', () => { + const fallback = getBaseDimensions(300, 200, 0, 0); + expect(fallback).toEqual({ width: 300, height: 200 }); + }); + + it('calculates cover dimensions for landscape image in portrait viewport', () => { + // Image: 800x600 (4:3 landscape, ratio 1.33) + // Viewport: 300x400 (3:4 portrait, ratio 0.75) + // Image should fit to height (400) and scale width to 400 * 1.3333 = 533.33 + const cover = getBaseDimensions(300, 400, 800, 600); + expect(cover.height).toBe(400); + expect(cover.width).toBeCloseTo(533.33, 1); + }); + + it('calculates cover dimensions for portrait image in landscape viewport', () => { + // Image: 600x800 (3:4 portrait, ratio 0.75) + // Viewport: 400x300 (4:3 landscape, ratio 1.33) + // Image should fit to width (400) and scale height to 400 / 0.75 = 533.33 + const cover = getBaseDimensions(400, 300, 600, 800); + expect(cover.width).toBe(400); + expect(cover.height).toBeCloseTo(533.33, 1); + }); }); }); diff --git a/src/tools/crop-image/crop-image.service.ts b/src/tools/crop-image/crop-image.service.ts index e69de29b..4e41902d 100644 --- a/src/tools/crop-image/crop-image.service.ts +++ b/src/tools/crop-image/crop-image.service.ts @@ -0,0 +1,50 @@ +/** + * Calculates viewport dimensions bounded by max width and height. + */ +export function getViewportDimensions( + ratio: number, + maxWidth: number = 450, + maxHeight: number = 450 +): { width: number; height: number } { + if (ratio >= 1) { + return { + width: maxWidth, + height: maxWidth / ratio, + }; + } else { + return { + width: maxHeight * ratio, + height: maxHeight, + }; + } +} + +/** + * Calculates base dimensions of the image when fit to cover the viewport. + */ +export function getBaseDimensions( + vW: number, + vH: number, + naturalWidth: number, + naturalHeight: number +): { width: number; height: number } { + if (!naturalWidth || !naturalHeight) { + return { width: vW, height: vH }; + } + const vRatio = vW / vH; + const iRatio = naturalWidth / naturalHeight; + + if (iRatio > vRatio) { + // Landscape relative to viewport + return { + width: vH * iRatio, + height: vH, + }; + } else { + // Portrait relative to viewport + return { + width: vW, + height: vW / iRatio, + }; + } +} diff --git a/src/tools/crop-image/crop-image.vue b/src/tools/crop-image/crop-image.vue index d5f53007..1fbc30ec 100644 --- a/src/tools/crop-image/crop-image.vue +++ b/src/tools/crop-image/crop-image.vue @@ -11,6 +11,8 @@ import { RotateClockwise, } from '@vicons/tabler'; +import { getViewportDimensions, getBaseDimensions } from './crop-image.service'; + // Image references const imageSrc = ref(null); const fileName = ref('image'); @@ -90,45 +92,17 @@ const maxViewportWidth = 450; const maxViewportHeight = 450; const viewportDimensions = computed(() => { - const ratio = currentRatio.value; - if (ratio >= 1) { - return { - width: maxViewportWidth, - height: maxViewportWidth / ratio, - }; - } - else { - return { - width: maxViewportHeight * ratio, - height: maxViewportHeight, - }; - } + return getViewportDimensions(currentRatio.value, maxViewportWidth, maxViewportHeight); }); // Image base dimensions when fitting cover const baseDimensions = computed(() => { - const vW = viewportDimensions.value.width; - const vH = viewportDimensions.value.height; - if (!imgNaturalWidth.value || !imgNaturalHeight.value) { - return { width: vW, height: vH }; - } - const vRatio = vW / vH; - const iRatio = imgNaturalWidth.value / imgNaturalHeight.value; - - if (iRatio > vRatio) { - // Landscape relative to viewport - return { - width: vH * iRatio, - height: vH, - }; - } - else { - // Portrait relative to viewport - return { - width: vW, - height: vW / iRatio, - }; - } + return getBaseDimensions( + viewportDimensions.value.width, + viewportDimensions.value.height, + imgNaturalWidth.value, + imgNaturalHeight.value + ); }); // Reset image to center with cover scale