Files
image-studio/commands/viewport.test.ts
2026-07-03 09:50:03 +02:00

45 lines
1.7 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { createInitialAppState } from "@editor/initial-state";
import {
viewportPanCommand,
viewportResetCommand,
viewportSetSizeCommand,
viewportSetZoomCommand,
viewportZoomAroundPointCommand,
} from "./viewport";
const context = () => ({ state: createInitialAppState("Test") });
describe("viewport commands", () => {
test("pans viewport center", () => {
const next = viewportPanCommand.execute(context(), { delta: { x: 10, y: -5 } });
expect(next.editor.viewport.center).toEqual({ x: 10, y: -5 });
});
test("sets zoom with minimum clamp", () => {
const next = viewportSetZoomCommand.execute(context(), { zoom: -1 });
expect(next.editor.viewport.zoom).toBe(0.01);
});
test("sets non-negative viewport size", () => {
const next = viewportSetSizeCommand.execute(context(), { w: 800, h: -1 });
expect(next.editor.viewport.size).toEqual({ w: 800, h: 0 });
});
test("zooms around a viewport point", () => {
const state = viewportSetSizeCommand.execute(context(), { w: 100, h: 100 });
const next = viewportZoomAroundPointCommand.execute({ state }, { zoom: 2, point: { x: 100, y: 50 } });
expect(next.editor.viewport.zoom).toBe(2);
expect(next.editor.viewport.center).toEqual({ x: 25, y: 0 });
});
test("resets viewport but preserves size", () => {
const sized = viewportSetSizeCommand.execute(context(), { w: 640, h: 480 });
const panned = viewportPanCommand.execute({ state: sized }, { delta: { x: 4, y: 8 } });
const next = viewportResetCommand.execute({ state: panned }, undefined);
expect(next.editor.viewport).toEqual({ center: { x: 0, y: 0 }, zoom: 1, rotation: 0, size: { w: 640, h: 480 } });
});
});