import { describe, it, expectTypeOf } from "vitest"; import { observable } from "@legendapp/state"; import type { Observable } from "@legendapp/state"; import { useDraggable, type UseDraggableReturn, type UseDraggableOptions, type Position } from "."; import type { MaybeEventTarget } from "../../types"; // --------------------------------------------------------------------------- // useDraggable() — types // --------------------------------------------------------------------------- describe("useDraggable() — types", () => { describe("generic constraint", () => { it("target accepts HTMLElement wrapped as MaybeEventTarget", () => { expectTypeOf().parameter(0).toExtend(); }); it("target parameter accepts null", () => { expectTypeOf().toBeCallableWith(null); }); }); describe("return type", () => { it("UseDraggableReturn type includes x$", () => { expectTypeOf().toEqualTypeOf>(); }); it("UseDraggableReturn type includes y$", () => { expectTypeOf().toEqualTypeOf>(); }); it("UseDraggableReturn type includes isDragging$", () => { expectTypeOf().toEqualTypeOf>(); }); it("UseDraggableReturn type includes style$", () => { expectTypeOf().toEqualTypeOf>(); }); it("UseDraggableReturn type includes position$", () => { expectTypeOf().toEqualTypeOf>(); }); }); describe("option types", () => { it("accepts Observable for disabled option", () => { expectTypeOf().toBeCallableWith(null, { disabled: observable(false) }); }); it("accepts Observable for axis option", () => { expectTypeOf().toBeCallableWith(null, { axis: observable<"x" | "y" | "both">("both"), }); }); it("accepts plain boolean for disabled option", () => { expectTypeOf().toBeCallableWith(null, { disabled: true }); }); it("accepts plain axis string for axis option", () => { expectTypeOf().toBeCallableWith(null, { axis: "x" as const }); }); it("rejects invalid axis value", () => { // @ts-expect-error — "z" is not a valid axis value const _: UseDraggableOptions = { axis: "z" }; }); it("rejects number for disabled option", () => { // @ts-expect-error — number is not assignable to boolean | Observable const _: UseDraggableOptions = { disabled: 1 }; }); it("Position type has numeric x and y fields", () => { expectTypeOf().toEqualTypeOf(); expectTypeOf().toEqualTypeOf(); }); }); });