import { afterEach, expect, it, vi } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { defineComponent, ref, nextTick } from "vue"; import { Button, ButtonGroup, ToggleButton, ToggleButtonGroup, } from "../../../packages/vue/src"; const wrappers: ReturnType[] = []; afterEach(async () => { wrappers.splice(0).forEach((w) => w.unmount()); await flushPromises(); document.body.innerHTML = ""; }); function fixture(template: string, setup = () => ({})) { const w = mount( defineComponent({ components: { Button, ButtonGroup, ToggleButton, ToggleButtonGroup }, template, setup, }), { attachTo: document.body }, ); wrappers.push(w); return w; } it("caller preventDefault cancels standalone and grouped activation", async () => { const change = vi.fn(); const w = fixture( '
onetwo
', () => ({ change }), ); for (const b of w.findAll("button")) await b.trigger("click"); expect(change).not.toHaveBeenCalled(); }); it("dynamic group size updates existing members", async () => { const size = ref("sm"); const w = fixture( 'A', () => ({ size }), ); expect(w.get("button").classes()).toContain("hv-toggle-button--sm"); size.value = "lg"; await nextTick(); expect(w.get("button").classes()).toContain("hv-toggle-button--lg"); }); it("multiple to single reconciles to at most one checked radio", async () => { const mode = ref("multiple"); const w = fixture( 'AB', () => ({ mode }), ); mode.value = "single"; await nextTick(); expect(w.findAll('[aria-checked="true"]').length).toBeLessThanOrEqual(1); }); it("a disabled first member leaves an enabled keyboard entry point", async () => { const w = fixture( 'AB', ); await flushPromises(); expect(w.findAll("button")[1].attributes("tabindex")).toBe("0"); }); it("numeric and string group keys do not produce duplicate DOM ids", () => { const w = fixture( 'numberstring', ); const ids = w .findAll("button") .map((b) => b.attributes("id")) .filter((id) => id !== undefined); expect(new Set(ids).size).toBe(ids.length); }); it("cancelled arrow key is not processed", async () => { const w = fixture( 'AB', ); await flushPromises(); const buttons = w.findAll("button"); (buttons[0].element as HTMLElement).focus(); await buttons[0].trigger("keydown", { key: "ArrowRight" }); expect(document.activeElement).toBe(buttons[0].element); }); it("changing member keys updates keyboard registration", async () => { const key = ref("a"); const w = fixture( 'firstsecond', () => ({ key }), ); await flushPromises(); key.value = "c"; await nextTick(); const buttons = w.findAll("button"); (buttons[0].element as HTMLElement).focus(); await buttons[0].trigger("keydown", { key: "ArrowRight" }); expect(document.activeElement).toBe(buttons[1].element); }); it("Tab exits the group using either a single tab stop or focus-to-end strategy", async () => { const w = fixture( 'ABC', ); await flushPromises(); const buttons = w.findAll("button"); (buttons[0].element as HTMLElement).focus(); if (w.findAll('button[tabindex="0"]').length > 1) { await buttons[0].trigger("keydown", { key: "Tab" }); expect(document.activeElement).toBe(buttons[2].element); } else { expect(w.findAll('button[tabindex="0"]')).toHaveLength(1); } }); it("vertical orientation does not consume perpendicular arrows", async () => { const w = fixture( 'AB', ); await flushPromises(); const first = w.findAll("button")[0]; (first.element as HTMLElement).focus(); await first.trigger("keydown", { key: "ArrowRight" }); expect(document.activeElement).toBe(first.element); }); it("explicit child isDisabled=false overrides only ButtonGroup default as in pinned source", () => { const w = fixture( '', ); expect((w.get("button").element as HTMLButtonElement).disabled).toBe(false); }); it("arrow navigation follows keyed DOM order after a reorder", async () => { const keys = ref(["a", "b", "c"]); const w = fixture( '{{ key }}', () => ({ keys }), ); await flushPromises(); keys.value = ["c", "b", "a"]; await nextTick(); const buttons = w.findAll("button"); expect(buttons.map((b) => b.text())).toEqual(["c", "b", "a"]); (buttons[1].element as HTMLElement).focus(); await buttons[1].trigger("keydown", { key: "ArrowRight" }); expect(document.activeElement).toBe(buttons[2].element); }); it("focusing a member updates the single Tab entry to that member", async () => { const w = fixture( 'ABC', ); await flushPromises(); const buttons = w.findAll("button"); (buttons[2].element as HTMLElement).focus(); await nextTick(); expect(buttons[2].attributes("tabindex")).toBe("0"); expect(buttons[0].attributes("tabindex")).toBe("-1"); });