Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+91
@@ -0,0 +1,91 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { MAX_ZOOM, MIN_ZOOM, centeredOffset, clampOffset, clampZoom, coverScale, cropRegion, offsetAfterZoom } from "./avatarCrop";
|
||||
|
||||
const landscape = { imageWidth: 800, imageHeight: 400, viewport: 200, zoom: 1 };
|
||||
const portrait = { imageWidth: 400, imageHeight: 800, viewport: 200, zoom: 1 };
|
||||
|
||||
describe("coverScale", () => {
|
||||
it("scales the shortest edge onto the viewport", () => {
|
||||
expect(coverScale(landscape)).toBe(0.5);
|
||||
expect(coverScale(portrait)).toBe(0.5);
|
||||
});
|
||||
|
||||
it("multiplies by the zoom factor", () => {
|
||||
expect(coverScale({ ...landscape, zoom: 2 })).toBe(1);
|
||||
});
|
||||
|
||||
it("returns 0 for an image that has no measurable size yet", () => {
|
||||
expect(coverScale({ ...landscape, imageWidth: 0, imageHeight: 0 })).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("clampZoom", () => {
|
||||
it("keeps the zoom inside the supported range", () => {
|
||||
expect(clampZoom(0.2)).toBe(MIN_ZOOM);
|
||||
expect(clampZoom(99)).toBe(MAX_ZOOM);
|
||||
expect(clampZoom(Number.NaN)).toBe(MIN_ZOOM);
|
||||
});
|
||||
});
|
||||
|
||||
describe("clampOffset", () => {
|
||||
it("never lets an empty gap slide into the viewport", () => {
|
||||
// 800x400 at scale 0.5 renders as 400x200, so x may travel from -200 to 0.
|
||||
expect(clampOffset(landscape, { x: 50, y: 0 })).toEqual({ x: 0, y: 0 });
|
||||
expect(clampOffset(landscape, { x: -900, y: 0 })).toEqual({ x: -200, y: 0 });
|
||||
});
|
||||
|
||||
it("pins the covered axis to 0 instead of inverting its range", () => {
|
||||
expect(clampOffset(landscape, { x: -10, y: -10 })).toEqual({ x: -10, y: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("centeredOffset", () => {
|
||||
it("centres the overhang of the long edge", () => {
|
||||
expect(centeredOffset(landscape)).toEqual({ x: -100, y: 0 });
|
||||
expect(centeredOffset(portrait)).toEqual({ x: 0, y: -100 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("offsetAfterZoom", () => {
|
||||
it("keeps the point in the middle of the viewport in the middle", () => {
|
||||
const start = centeredOffset(portrait);
|
||||
const zoomed = offsetAfterZoom(portrait, start, 2);
|
||||
|
||||
const before = cropRegion(portrait, start);
|
||||
const after = cropRegion({ ...portrait, zoom: 2 }, zoomed);
|
||||
|
||||
expect(after.sx + after.size / 2).toBeCloseTo(before.sx + before.size / 2, 5);
|
||||
expect(after.sy + after.size / 2).toBeCloseTo(before.sy + before.size / 2, 5);
|
||||
});
|
||||
|
||||
it("re-clamps so zooming out cannot expose a gap", () => {
|
||||
const panned = clampOffset({ ...portrait, zoom: 3 }, { x: -400, y: -1600 });
|
||||
const zoomedOut = offsetAfterZoom({ ...portrait, zoom: 3 }, panned, 1);
|
||||
|
||||
expect(zoomedOut).toEqual(clampOffset({ ...portrait, zoom: 1 }, zoomedOut));
|
||||
});
|
||||
});
|
||||
|
||||
describe("cropRegion", () => {
|
||||
it("maps the centred viewport onto the source pixels", () => {
|
||||
expect(cropRegion(landscape, centeredOffset(landscape))).toEqual({ sx: 200, sy: 0, size: 400 });
|
||||
});
|
||||
|
||||
it("shrinks the section as the user zooms in", () => {
|
||||
const geometry = { ...landscape, zoom: 2 };
|
||||
expect(cropRegion(geometry, centeredOffset(geometry)).size).toBe(200);
|
||||
});
|
||||
|
||||
it("follows a pan to the left edge", () => {
|
||||
expect(cropRegion(landscape, { x: 0, y: 0 })).toEqual({ sx: 0, sy: 0, size: 400 });
|
||||
});
|
||||
|
||||
it("stays inside the source image at every extreme", () => {
|
||||
const geometry = { ...landscape, zoom: 3 };
|
||||
const region = cropRegion(geometry, { x: -10_000, y: -10_000 });
|
||||
|
||||
expect(region.sx + region.size).toBeLessThanOrEqual(geometry.imageWidth);
|
||||
expect(region.sy + region.size).toBeLessThanOrEqual(geometry.imageHeight);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user