import { describe, it, expect, vi, afterEach } from "vitest"; import { render } from "@solidjs/web"; import { focus } from "../src/index.js"; // Regression for #1143: focus() must work as a real JSX ref, where onCleanup is forbidden. let disposers: Array<() => void> = []; function mount(App: () => any) { const container = document.createElement("div"); document.body.appendChild(container); const dispose = render(App, container); const unmount = () => { dispose(); container.remove(); }; disposers.push(unmount); return { container, unmount }; } afterEach(() => { disposers.forEach(d => d()); disposers = []; }); const fire = (el: Element, type: "focus" | "blur") => el.dispatchEvent(new FocusEvent(type)); describe("focus as a JSX ref callback", () => { it("mounts a static ref without throwing and reports focus changes", () => { const cb = vi.fn(); let container!: HTMLElement; expect(() => { ({ container } = mount(() => )); }).not.toThrow(); const input = container.querySelector("input")!; expect(cb).toHaveBeenLastCalledWith(false); fire(input, "focus"); expect(cb).toHaveBeenLastCalledWith(true); fire(input, "blur"); expect(cb).toHaveBeenLastCalledWith(false); }); it("works inside an array of refs", () => { const cb = vi.fn(); const other = vi.fn(); const { container } = mount(() => ); const input = container.querySelector("input")!; expect(other).toHaveBeenCalledWith(input); fire(input, "focus"); expect(cb).toHaveBeenLastCalledWith(true); }); it("keeps per-element state when one focus() value is attached to two elements", () => { const cb = vi.fn(); const { container, unmount } = mount(() => { const f = focus(cb); return (