|
| 1 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | +import type { Locator } from "vite-plus/test/browser"; |
| 3 | + |
| 4 | +import { render } from "../../../test/render"; |
| 5 | +import { FileExplorerPane } from "./FileExplorerPane"; |
| 6 | + |
| 7 | +const STORAGE_KEY = "t3code.fileExplorerWidth"; |
| 8 | + |
| 9 | +function PreviewRow({ width = 1000, resizable = true }: { width?: number; resizable?: boolean }) { |
| 10 | + return ( |
| 11 | + <div style={{ display: "flex", width, height: 300 }}> |
| 12 | + <div style={{ flex: 1, minWidth: 0 }}>File contents</div> |
| 13 | + <FileExplorerPane resizable={resizable}>Files</FileExplorerPane> |
| 14 | + </div> |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +function drag(handle: HTMLElement, delta: number, finish = true) { |
| 19 | + const init = { bubbles: true, button: 0, pointerId: 1, clientX: 600 }; |
| 20 | + handle.dispatchEvent(new PointerEvent("pointerdown", init)); |
| 21 | + handle.dispatchEvent(new PointerEvent("pointermove", { ...init, clientX: 600 + delta })); |
| 22 | + if (finish) { |
| 23 | + handle.dispatchEvent(new PointerEvent("pointerup", { ...init, clientX: 600 + delta })); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +async function expectWidth(explorer: Locator, width: number) { |
| 28 | + await vi.waitFor(() => expect(explorer.element().getBoundingClientRect().width).toBe(width)); |
| 29 | +} |
| 30 | + |
| 31 | +function storedWidth(width: number) { |
| 32 | + const previous = localStorage.getItem(STORAGE_KEY); |
| 33 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(width)); |
| 34 | + return { |
| 35 | + [Symbol.dispose]() { |
| 36 | + if (previous === null) localStorage.removeItem(STORAGE_KEY); |
| 37 | + else localStorage.setItem(STORAGE_KEY, previous); |
| 38 | + }, |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +describe("FileExplorerPane", () => { |
| 43 | + it("restores the saved width after narrowing and widening the preview row", async () => { |
| 44 | + using _storage = storedWidth(480); |
| 45 | + await using screen = await render(<PreviewRow />); |
| 46 | + const explorer = screen.getByRole("complementary", { name: "File explorer" }); |
| 47 | + await expectWidth(explorer, 480); |
| 48 | + |
| 49 | + await screen.rerender(<PreviewRow width={600} />); |
| 50 | + await expectWidth(explorer, 344); |
| 51 | + expect(localStorage.getItem(STORAGE_KEY)).toBe("480"); |
| 52 | + |
| 53 | + await screen.rerender(<PreviewRow width={360} />); |
| 54 | + await expectWidth(explorer, 160); |
| 55 | + |
| 56 | + await screen.rerender(<PreviewRow />); |
| 57 | + await expectWidth(explorer, 480); |
| 58 | + }); |
| 59 | + |
| 60 | + it("clamps dragging to the row and persists the visible width on release", async () => { |
| 61 | + using _storage = storedWidth(256); |
| 62 | + await using screen = await render(<PreviewRow width={600} />); |
| 63 | + // Synthetic pointer ids are not active browser pointers. |
| 64 | + using _capture = vi |
| 65 | + .spyOn(HTMLElement.prototype, "setPointerCapture") |
| 66 | + .mockImplementation(() => {}); |
| 67 | + const handle = screen |
| 68 | + .getByRole("separator", { name: "Resize file explorer" }) |
| 69 | + .element() as HTMLElement; |
| 70 | + const explorer = screen.getByRole("complementary", { name: "File explorer" }); |
| 71 | + |
| 72 | + drag(handle, -1000); |
| 73 | + await expectWidth(explorer, 344); |
| 74 | + expect(localStorage.getItem(STORAGE_KEY)).toBe("344"); |
| 75 | + |
| 76 | + drag(handle, 40); |
| 77 | + await expectWidth(explorer, 304); |
| 78 | + expect(localStorage.getItem(STORAGE_KEY)).toBe("304"); |
| 79 | + |
| 80 | + drag(handle, 1000); |
| 81 | + await expectWidth(explorer, 160); |
| 82 | + expect(localStorage.getItem(STORAGE_KEY)).toBe("160"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("cleans up an active drag when the explorer is hidden", async () => { |
| 86 | + using _storage = storedWidth(256); |
| 87 | + await using screen = await render(<PreviewRow />); |
| 88 | + using _capture = vi |
| 89 | + .spyOn(HTMLElement.prototype, "setPointerCapture") |
| 90 | + .mockImplementation(() => {}); |
| 91 | + const handle = screen |
| 92 | + .getByRole("separator", { name: "Resize file explorer" }) |
| 93 | + .element() as HTMLElement; |
| 94 | + drag(handle, -80, false); |
| 95 | + expect(document.body.style.cursor).toBe("col-resize"); |
| 96 | + |
| 97 | + await screen.unmount(); |
| 98 | + expect(document.body.style.cursor).toBe(""); |
| 99 | + expect(document.body.style.userSelect).toBe(""); |
| 100 | + expect(localStorage.getItem(STORAGE_KEY)).toBe("256"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("does not constrain the explorer or show a splitter when no file is open", async () => { |
| 104 | + using _storage = storedWidth(480); |
| 105 | + await using screen = await render(<PreviewRow resizable={false} />); |
| 106 | + await expect.element(screen.getByRole("separator")).not.toBeInTheDocument(); |
| 107 | + expect((screen.getByRole("complementary").element() as HTMLElement).style.width).toBe(""); |
| 108 | + }); |
| 109 | +}); |
0 commit comments