import { act, renderHook } from "@testing-library/react"; import { describe, expect, it } from "vitest"; import { useZoom } from "../useZoom"; describe("useZoom hook", () => { it("should initialize with default values", () => { const { result } = renderHook(() => useZoom()); expect(result.current.zoom).toBe(1); expect(result.current.pan).toEqual({ x: 0, y: 0 }); expect(result.current.isDefaultZoom).toBe(true); expect(result.current.isDragging).toBe(false); }); it("should increase zoom when zoomIn is called", () => { const { result } = renderHook(() => useZoom()); act(() => { result.current.zoomIn(); }); expect(result.current.zoom).toBe(1.5); expect(result.current.isDefaultZoom).toBe(false); }); it("should decrease zoom when zoomOut is called", () => { const { result } = renderHook(() => useZoom()); // First zoom in to have room to zoom out act(() => { result.current.zoomIn(); result.current.zoomIn(); }); expect(result.current.zoom).toBe(2); act(() => { result.current.zoomOut(); }); expect(result.current.zoom).toBe(1.5); }); it("should not go below minZoom", () => { const { result } = renderHook(() => useZoom({ minZoom: 1 })); act(() => { result.current.zoomOut(); result.current.zoomOut(); result.current.zoomOut(); }); expect(result.current.zoom).toBe(1); }); it("should not go above maxZoom", () => { const { result } = renderHook(() => useZoom({ maxZoom: 2 })); act(() => { result.current.zoomIn(); result.current.zoomIn(); result.current.zoomIn(); result.current.zoomIn(); result.current.zoomIn(); }); expect(result.current.zoom).toBe(2); }); it("should reset zoom and pan when resetZoom is called", () => { const { result } = renderHook(() => useZoom()); act(() => { result.current.zoomIn(); result.current.zoomIn(); }); expect(result.current.zoom).toBe(2); act(() => { result.current.resetZoom(); }); expect(result.current.zoom).toBe(1); expect(result.current.pan).toEqual({ x: 0, y: 0 }); expect(result.current.isDefaultZoom).toBe(true); }); it("should use custom zoomStep", () => { const { result } = renderHook(() => useZoom({ zoomStep: 0.5 })); act(() => { result.current.zoomIn(); }); expect(result.current.zoom).toBe(1.5); }); it("should return isDefaultZoom as false when zoomed", () => { const { result } = renderHook(() => useZoom()); expect(result.current.isDefaultZoom).toBe(true); act(() => { result.current.zoomIn(); }); expect(result.current.isDefaultZoom).toBe(false); }); describe("wheelZoom option", () => { it("should accept wheelZoom option with default value true", () => { const { result } = renderHook(() => useZoom()); // Hook should work normally with default wheelZoom=true expect(result.current.zoom).toBe(1); expect(result.current.containerRef).toBeDefined(); }); it("should accept wheelZoom option set to false", () => { const { result } = renderHook(() => useZoom({ wheelZoom: false })); // Hook should work normally with wheelZoom=false expect(result.current.zoom).toBe(1); expect(result.current.containerRef).toBeDefined(); }); it("should still allow button zoom when wheelZoom is disabled", () => { const { result } = renderHook(() => useZoom({ wheelZoom: false })); act(() => { result.current.zoomIn(); }); expect(result.current.zoom).toBe(1.5); act(() => { result.current.zoomOut(); }); expect(result.current.zoom).toBe(1); }); }); });