|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import React, { act } from "react"; |
| 4 | +import { createRoot } from "react-dom/client"; |
| 5 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import { PreviewGuides } from "./PreviewGuides"; |
| 7 | +import { usePreviewGuidesStore } from "./previewGuidesStore"; |
| 8 | + |
| 9 | +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 10 | + |
| 11 | +const compRect = vi.hoisted(() => ({ |
| 12 | + current: { left: 20, top: 20, width: 400, height: 225, scaleX: 1, scaleY: 1 }, |
| 13 | +})); |
| 14 | +vi.mock("./useDomEditCompositionRect", () => ({ |
| 15 | + useDomEditCompositionRect: () => compRect.current, |
| 16 | +})); |
| 17 | + |
| 18 | +afterEach(() => { |
| 19 | + compRect.current = { left: 20, top: 20, width: 400, height: 225, scaleX: 1, scaleY: 1 }; |
| 20 | + document.body.innerHTML = ""; |
| 21 | + window.localStorage.clear(); |
| 22 | +}); |
| 23 | + |
| 24 | +function render(rulerVisible: boolean, safeMarginsVisible: boolean) { |
| 25 | + usePreviewGuidesStore.setState({ rulerVisible, safeMarginsVisible }); |
| 26 | + const host = document.createElement("div"); |
| 27 | + document.body.append(host); |
| 28 | + act(() => { |
| 29 | + createRoot(host).render(<PreviewGuides iframeRef={{ current: null }} />); |
| 30 | + }); |
| 31 | + return host; |
| 32 | +} |
| 33 | + |
| 34 | +describe("PreviewGuides", () => { |
| 35 | + it("draws nothing when both toggles are off", () => { |
| 36 | + const host = render(false, false); |
| 37 | + expect(host.querySelector("[data-testid]")).toBeNull(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("draws the ruler with ticks 0 to 100 every 10 on both edges", () => { |
| 41 | + const host = render(true, false); |
| 42 | + const top = host.querySelector('[data-testid="preview-ruler-top"]'); |
| 43 | + expect(Array.from(top?.children ?? []).map((t) => t.textContent)).toEqual( |
| 44 | + Array.from({ length: 11 }, (_, i) => String(i * 10)), |
| 45 | + ); |
| 46 | + expect(host.querySelector('[data-testid="preview-ruler-left"]')).not.toBeNull(); |
| 47 | + expect(host.querySelector('[data-testid="preview-safe-90"]')).toBeNull(); |
| 48 | + expect(host.querySelector('[data-testid="preview-safe-80"]')).toBeNull(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("draws the 90% and 80% boxes with edge-midpoint ticks and no labels or caption band", () => { |
| 52 | + const host = render(false, true); |
| 53 | + const box = (pct: number) => |
| 54 | + host.querySelector<HTMLElement>(`[data-testid="preview-safe-${pct}"]`); |
| 55 | + expect([box(90)?.style.left, box(90)?.style.bottom]).toEqual(["5%", "5%"]); |
| 56 | + expect([box(80)?.style.top, box(80)?.style.right]).toEqual(["10%", "10%"]); |
| 57 | + expect(box(90)?.children.length).toBe(4); |
| 58 | + expect(box(80)?.children.length).toBe(4); |
| 59 | + expect(host.textContent).toBe(""); |
| 60 | + expect(host.querySelector('[data-testid="preview-ruler-top"]')).toBeNull(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("places the ruler in the gutter outside the frame", () => { |
| 64 | + const host = render(true, false); |
| 65 | + const top = host.querySelector<HTMLElement>('[data-testid="preview-ruler-top"]'); |
| 66 | + const left = host.querySelector<HTMLElement>('[data-testid="preview-ruler-left"]'); |
| 67 | + expect([top?.style.left, top?.style.top, top?.style.width]).toEqual(["20px", "4px", "400px"]); |
| 68 | + expect([left?.style.left, left?.style.top, left?.style.height]).toEqual([ |
| 69 | + "4px", |
| 70 | + "20px", |
| 71 | + "225px", |
| 72 | + ]); |
| 73 | + }); |
| 74 | + |
| 75 | + it("draws nothing until the composition has a size", () => { |
| 76 | + compRect.current = { left: 0, top: 0, width: 0, height: 0, scaleX: 1, scaleY: 1 }; |
| 77 | + const host = render(true, true); |
| 78 | + expect(host.querySelector("[data-testid]")).toBeNull(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("draws the same boxes on a portrait composition", () => { |
| 82 | + compRect.current = { left: 20, top: 20, width: 225, height: 400, scaleX: 1, scaleY: 1 }; |
| 83 | + const host = render(false, true); |
| 84 | + const box = host.querySelector<HTMLElement>('[data-testid="preview-safe-90"]'); |
| 85 | + expect([box?.style.left, box?.style.top]).toEqual(["5%", "5%"]); |
| 86 | + expect(host.querySelector('[data-testid="preview-safe-80"]')).not.toBeNull(); |
| 87 | + }); |
| 88 | +}); |
0 commit comments