import { afterEach, expect, it, vi } from "vitest"; import { mount } from "@vue/test-utils"; import { defineComponent, nextTick, ref } from "vue"; import { Button, Toolbar, ToggleButton, ToggleButtonGroup, } from "../packages/vue/src"; const wrappers: ReturnType[] = []; afterEach(() => { wrappers.splice(0).forEach((wrapper) => wrapper.unmount()); document.body.innerHTML = ""; }); function fixture(template: string, setup = () => ({})) { const wrapper = mount( defineComponent({ components: { Button, Toolbar, ToggleButton, ToggleButtonGroup }, template, setup, }), { attachTo: document.body }, ); wrappers.push(wrapper); return wrapper; } it("standalone Button preserves the caller tabindex after Toolbar integration", async () => { const wrapper = fixture(''); await nextTick(); expect(wrapper.get("button").attributes("tabindex")).toBe("-1"); }); it("standalone asChild buttons preserve tabindex supplied by their native child", async () => { const wrapper = fixture( '
', ); await nextTick(); expect( wrapper.findAll("button").map((button) => button.attributes("tabindex")), ).toEqual(["-1", "-1"]); }); it("moving an existing Button through Teleport restores its own tabindex", async () => { const inline = ref(true); const wrapper = fixture( '', () => ({ inline }), ); await nextTick(); const moving = document.querySelector("[data-outside]") as HTMLButtonElement; expect(moving.getAttribute("tabindex")).toBe("-1"); inline.value = false; await nextTick(); expect(wrapper.element.contains(moving)).toBe(false); expect(moving.getAttribute("tabindex")).toBe("3"); }); it("moving an existing group outside Toolbar restores its arrow navigation", async () => { const inline = ref(true); fixture( 'XY', () => ({ inline }), ); await nextTick(); inline.value = false; await nextTick(); const buttons = document.querySelectorAll( "[data-moving] button", ); buttons[0].focus(); buttons[0].dispatchEvent( new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true }), ); await nextTick(); expect(document.activeElement).toBe(buttons[1]); }); it("Toolbar root keydown handler is preserved and can cancel navigation", async () => { const handler = vi.fn((event: KeyboardEvent) => event.preventDefault()); const wrapper = fixture( '', () => ({ handler }), ); await nextTick(); const buttons = wrapper.findAll("button"); (buttons[0].element as HTMLElement).focus(); await buttons[0].trigger("keydown", { key: "ArrowRight" }); expect(handler).toHaveBeenCalledTimes(1); expect(document.activeElement).toBe(buttons[0].element); }); it("Toolbar navigation excludes controls teleported outside its DOM", async () => { const wrapper = fixture( '', ); await nextTick(); const buttons = wrapper.findAll("button"); expect(buttons).toHaveLength(2); (buttons[1].element as HTMLElement).focus(); await buttons[1].trigger("keydown", { key: "ArrowRight" }); expect(document.activeElement).toBe(buttons[0].element); const outside = [...document.querySelectorAll("button")].find( (button) => button.textContent === "Outside", ); expect(outside?.hasAttribute("tabindex")).toBe(false); });