|
| 1 | +import { DndContext, PointerSensor, useSensor, useSensors } from "@dnd-kit/core"; |
| 2 | +import { SortableContext, useSortable, type SortingStrategy } from "@dnd-kit/sortable"; |
| 3 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 4 | +import { render } from "../../../test/render"; |
| 5 | +import { sidebarMarkerId } from "../Sidebar.logic"; |
| 6 | +import { SidebarSectionPlaceholder } from "./SidebarListMarkers"; |
| 7 | + |
| 8 | +// The sidebar strategy gives every item a transform once a drag starts. |
| 9 | +const stationary: SortingStrategy = () => ({ x: 0, y: 0, scaleX: 1, scaleY: 1 }); |
| 10 | + |
| 11 | +function Row() { |
| 12 | + const { setNodeRef, listeners } = useSortable({ id: "row" }); |
| 13 | + return ( |
| 14 | + <li ref={setNodeRef} {...listeners} data-testid="row" style={{ height: 40 }}> |
| 15 | + Row |
| 16 | + </li> |
| 17 | + ); |
| 18 | +} |
| 19 | + |
| 20 | +function Fixture() { |
| 21 | + const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 1 } })); |
| 22 | + return ( |
| 23 | + <DndContext sensors={sensors} autoScroll={false}> |
| 24 | + <SortableContext |
| 25 | + items={["row", sidebarMarkerId("settled-placeholder")]} |
| 26 | + strategy={stationary} |
| 27 | + > |
| 28 | + <div data-testid="scroller" style={{ height: 160, overflow: "auto" }}> |
| 29 | + <ul> |
| 30 | + <Row /> |
| 31 | + <li style={{ height: 2000 }} /> |
| 32 | + <SidebarSectionPlaceholder |
| 33 | + marker="settled-placeholder" |
| 34 | + label="Settled" |
| 35 | + showHint |
| 36 | + isDropTarget={false} |
| 37 | + /> |
| 38 | + </ul> |
| 39 | + </div> |
| 40 | + </SortableContext> |
| 41 | + </DndContext> |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +function pointer(type: string, target: EventTarget, y: number) { |
| 46 | + target.dispatchEvent( |
| 47 | + new PointerEvent(type, { |
| 48 | + bubbles: true, |
| 49 | + cancelable: true, |
| 50 | + isPrimary: true, |
| 51 | + pointerId: 1, |
| 52 | + button: 0, |
| 53 | + buttons: type === "pointerup" ? 0 : 1, |
| 54 | + clientX: 10, |
| 55 | + clientY: y, |
| 56 | + }), |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +describe("SidebarSectionPlaceholder", () => { |
| 61 | + it("leaves the list scroll alone when a drag starts with the drop hint far out of view", async () => { |
| 62 | + await using screen = await render(<Fixture />, { router: false }); |
| 63 | + const scroller = screen.getByTestId("scroller").element(); |
| 64 | + const row = screen.getByTestId("row").element(); |
| 65 | + const { top } = row.getBoundingClientRect(); |
| 66 | + |
| 67 | + pointer("pointerdown", row, top + 5); |
| 68 | + pointer("pointermove", document, top + 15); |
| 69 | + const hint = screen.getByTestId("sidebar-settled-placeholder").element(); |
| 70 | + // The reveal effect runs in the same commit that applies the drag transform. |
| 71 | + await vi.waitFor(() => |
| 72 | + expect(getComputedStyle(hint).transform !== "none" || scroller.scrollTop !== 0).toBe(true), |
| 73 | + ); |
| 74 | + |
| 75 | + expect(scroller.scrollTop).toBe(0); |
| 76 | + pointer("pointerup", document, top + 15); |
| 77 | + }); |
| 78 | +}); |