import { describe, it, expectTypeOf } from "vitest"; import { observable } from "@legendapp/state"; import type { ReadonlyObservable } from "@usels/core"; import { useIntersectionObserver, type UseIntersectionObserverOptions, type UseIntersectionObserverReturn, } from "."; describe("useIntersectionObserver() — types", () => { describe("option types", () => { it("options accepts plain boolean for immediate", () => { const _: UseIntersectionObserverOptions = { immediate: false }; expectTypeOf(_).toMatchTypeOf(); }); it("options accepts plain string for rootMargin", () => { const _: UseIntersectionObserverOptions = { rootMargin: "10px" }; expectTypeOf(_).toMatchTypeOf(); }); it("options accepts plain number for threshold", () => { const _: UseIntersectionObserverOptions = { threshold: 0.5 }; expectTypeOf(_).toMatchTypeOf(); }); it("options accepts number array for threshold", () => { const _: UseIntersectionObserverOptions = { threshold: [0, 0.5, 1] }; expectTypeOf(_).toMatchTypeOf(); }); it("accepts Observable for rootMargin option", () => { expectTypeOf().toBeCallableWith( null, (_entries: IntersectionObserverEntry[]) => {}, { rootMargin: observable("10px") } ); }); it("accepts null as target", () => { expectTypeOf().toBeCallableWith( null, (_entries: IntersectionObserverEntry[]) => {} ); }); it("rejects invalid type for rootMargin", () => { // @ts-expect-error — rootMargin must be string | Observable, not number const _: UseIntersectionObserverOptions = { rootMargin: 42 }; }); it("rejects invalid type for threshold", () => { // @ts-expect-error — threshold must be number | number[], not string const _: UseIntersectionObserverOptions = { threshold: "high" }; }); }); describe("return type", () => { it("returns UseIntersectionObserverReturn with correct shape", () => { expectTypeOf().toHaveProperty("isSupported$"); expectTypeOf().toHaveProperty("isActive$"); expectTypeOf().toHaveProperty("pause"); expectTypeOf().toHaveProperty("resume"); expectTypeOf().toHaveProperty("stop"); }); it("isSupported$ is ReadonlyObservable", () => { expectTypeOf().toEqualTypeOf< ReadonlyObservable >(); }); it("isActive$ is ReadonlyObservable", () => { expectTypeOf().toEqualTypeOf< ReadonlyObservable >(); }); it("pause is a function returning void", () => { expectTypeOf().toEqualTypeOf<() => void>(); }); it("resume is a function returning void", () => { expectTypeOf().toEqualTypeOf<() => void>(); }); it("stop is a function returning void", () => { expectTypeOf().toEqualTypeOf<() => void>(); }); }); });