import { afterEach, describe, expect, it, vi } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { Teleport, defineComponent, h, nextTick, ref } from "vue"; import { readFileSync } from "node:fs"; import { Button, ButtonGroup, ButtonGroupSeparator, Separator, ToggleButton, ToggleButtonGroup, ToggleButtonGroupSeparator, Toolbar, } from "../packages/vue/src"; afterEach(() => { document.body.innerHTML = ""; }); function mountTo( comp: T, props: Record = {}, setup?: () => Record, ) { return mount( defineComponent({ components: { Button, ButtonGroup, ButtonGroupSeparator, Separator, ToggleButton, ToggleButtonGroup, ToggleButtonGroupSeparator, Toolbar, }, setup: setup ?? (() => ({})), ...comp, }), { attachTo: document.body }, ); } describe("Toolbar basics", () => { it("renders role, data-slot, aria-orientation and classes", () => { const w = mount( defineComponent({ render: () => h(Toolbar, { "aria-label": "文本格式" }, () => [ h(Button, () => "One"), ]), }), { attachTo: document.body }, ); const root = w.get('[role="toolbar"]'); expect(root.attributes("data-slot")).toBe("toolbar"); expect(root.attributes("aria-label")).toBe("文本格式"); expect(root.attributes("aria-orientation")).toBe("horizontal"); expect(root.classes().join(" ")).toContain("hv-toolbar"); expect(root.classes().join(" ")).toContain("hv-toolbar--horizontal"); w.unmount(); }); it("exposes vertical and attached modifiers", () => { const w = mount( defineComponent({ render: () => h( Toolbar, { orientation: "vertical", isAttached: true, "aria-label": "工具" }, () => [h(Button, () => "One")], ), }), { attachTo: document.body }, ); const classes = w.get('[role="toolbar"]').classes().join(" "); expect(classes).toContain("hv-toolbar--vertical"); expect(classes).toContain("hv-toolbar--attached"); expect(w.get('[role="toolbar"]').attributes("aria-orientation")).toBe( "vertical", ); w.unmount(); }); it("passes orientation to the scoped slot and merges attrs/class", async () => { const seen: string[] = []; const w = mount( defineComponent({ render: () => h( Toolbar, { orientation: "vertical", class: "extra", "data-testid": "bar" }, { default: (scope: { orientation: string }) => { seen.push(scope.orientation); return [h(Button, () => "One")]; }, }, ), }), { attachTo: document.body }, ); expect(seen).toEqual(["vertical"]); const root = w.get('[data-testid="bar"]'); expect(root.classes().join(" ")).toContain("extra"); expect(root.classes().join(" ")).toContain("hv-toolbar--vertical"); w.unmount(); }); it("updates class and aria when orientation and isAttached change", async () => { const vertical = ref(false); const attached = ref(false); const w = mount( defineComponent({ setup: () => () => h( Toolbar, { orientation: vertical.value ? "vertical" : "horizontal", isAttached: attached.value, "aria-label": "bar", }, () => [h(Button, () => "One")], ), }), { attachTo: document.body }, ); vertical.value = true; attached.value = true; await nextTick(); const root = w.get('[role="toolbar"]'); expect(root.attributes("aria-orientation")).toBe("vertical"); expect(root.classes().join(" ")).toContain("hv-toolbar--vertical"); expect(root.classes().join(" ")).toContain("hv-toolbar--attached"); w.unmount(); }); }); describe("Toolbar keyboard ownership", () => { const withGroups = () => h(Toolbar, { "aria-label": "编辑器" }, () => [ h(ButtonGroup, { variant: "tertiary" }, () => [ h(Button, () => "Undo"), h(Button, () => [h(ButtonGroupSeparator), "Redo"]), ]), h(Separator), h( ToggleButtonGroup, { selectionMode: "multiple", "aria-label": "styles" }, () => [ h(ToggleButton, { id: "bold" }, () => "Bold"), h(ToggleButton, { id: "italic" }, () => [ h(ToggleButtonGroupSeparator), "Italic", ]), ], ), ]); it("moves once per arrow key across ButtonGroup and ToggleButtonGroup", async () => { const w = mount(defineComponent({ render: withGroups }), { attachTo: document.body, }); await nextTick(); await flushPromises(); const buttons = w.findAll("button"); expect(buttons).toHaveLength(4); (buttons[0]!.element as HTMLElement).focus(); await buttons[0]!.trigger("keydown", { key: "ArrowRight" }); expect(document.activeElement).toBe(buttons[1]!.element); await buttons[1]!.trigger("keydown", { key: "ArrowRight" }); expect(document.activeElement).toBe(buttons[2]!.element); await buttons[2]!.trigger("keydown", { key: "ArrowRight" }); expect(document.activeElement).toBe(buttons[3]!.element); w.unmount(); }); it("keeps a single Tab entry across nested groups", async () => { const w = mount(defineComponent({ render: withGroups }), { attachTo: document.body, }); await nextTick(); await flushPromises(); const stops = w .findAll("button") .filter((b) => b.attributes("tabindex") === "0"); expect(stops).toHaveLength(1); w.unmount(); }); it("real focus moves the toolbar Tab entry", async () => { const w = mount(defineComponent({ render: withGroups }), { attachTo: document.body, }); await nextTick(); await flushPromises(); const buttons = w.findAll("button"); (buttons[3]!.element as HTMLElement).focus(); await nextTick(); expect(buttons[3]!.attributes("tabindex")).toBe("0"); expect( buttons.filter((b) => b.attributes("tabindex") === "0"), ).toHaveLength(1); w.unmount(); }); it("skips disabled members and promotes the next entry", async () => { const w = mount( defineComponent({ render: () => h(Toolbar, { "aria-label": "bar" }, () => [ h(Button, { isDisabled: true }, () => "No"), h(Button, () => "Yes"), ]), }), { attachTo: document.body }, ); await nextTick(); await flushPromises(); const buttons = w.findAll("button"); expect(buttons[1]!.attributes("tabindex")).toBe("0"); (buttons[1]!.element as HTMLElement).focus(); await buttons[1]!.trigger("keydown", { key: "ArrowRight" }); expect(document.activeElement).toBe(buttons[1]!.element); w.unmount(); }); it("reverses horizontal arrows in RTL across three members", async () => { const w = mount( defineComponent({ render: () => h(Toolbar, { "aria-label": "bar", dir: "rtl" }, () => [ h(Button, () => "A"), h(Button, () => "B"), h(Button, () => "C"), ]), }), { attachTo: document.body }, ); await nextTick(); await flushPromises(); const buttons = w.findAll("button"); (buttons[0]!.element as HTMLElement).focus(); await buttons[0]!.trigger("keydown", { key: "ArrowLeft" }); expect(document.activeElement).toBe(buttons[1]!.element); await buttons[1]!.trigger("keydown", { key: "ArrowLeft" }); expect(document.activeElement).toBe(buttons[2]!.element); await buttons[2]!.trigger("keydown", { key: "ArrowRight" }); expect(document.activeElement).toBe(buttons[1]!.element); w.unmount(); }); it("vertical toolbar uses up/down and ignores horizontal arrows", async () => { const w = mount( defineComponent({ render: () => h(Toolbar, { "aria-label": "bar", orientation: "vertical" }, () => [ h(Button, () => "A"), h(Button, () => "B"), ]), }), { attachTo: document.body }, ); await nextTick(); 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); await buttons[0]!.trigger("keydown", { key: "ArrowDown" }); expect(document.activeElement).toBe(buttons[1]!.element); w.unmount(); }); it("respects preventDefault on arrow keys", async () => { const w = mount( defineComponent({ render: () => h(Toolbar, { "aria-label": "bar" }, () => [ h( Button, { onKeydown: (e: KeyboardEvent) => e.preventDefault() }, () => "A", ), h(Button, () => "B"), ]), }), { attachTo: document.body }, ); await nextTick(); 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); w.unmount(); }); it("does not toggle selection when navigating with arrows", async () => { const change = vi.fn(); const w = mount( defineComponent({ render: () => h(Toolbar, { "aria-label": "bar" }, () => [ h( ToggleButtonGroup, { selectionMode: "multiple", onSelectionChange: change, }, () => [ h(ToggleButton, { id: "a" }, () => "A"), h(ToggleButton, { id: "b" }, () => "B"), ], ), ]), }), { attachTo: document.body }, ); await nextTick(); 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[1]!.element); expect(change).not.toHaveBeenCalled(); w.unmount(); }); it("Home and End move to toolbar edges", async () => { const w = mount(defineComponent({ render: withGroups }), { attachTo: document.body, }); await nextTick(); await flushPromises(); const buttons = w.findAll("button"); (buttons[1]!.element as HTMLElement).focus(); await buttons[1]!.trigger("keydown", { key: "End" }); expect(document.activeElement).toBe(buttons[3]!.element); await buttons[3]!.trigger("keydown", { key: "Home" }); expect(document.activeElement).toBe(buttons[0]!.element); w.unmount(); }); it("nested toolbars isolate arrow navigation", async () => { const w = mount( defineComponent({ render: () => h(Toolbar, { "aria-label": "outer" }, () => [ h(Button, () => "Outer"), h(Toolbar, { "aria-label": "inner" }, () => [ h(Button, () => "Inner1"), h(Button, () => "Inner2"), ]), h(Button, () => "Outer2"), ]), }), { attachTo: document.body }, ); await nextTick(); await flushPromises(); const buttons = w.findAll("button"); // DOM order: Outer, Inner1, Inner2, Outer2 (buttons[1]!.element as HTMLElement).focus(); await buttons[1]!.trigger("keydown", { key: "ArrowRight" }); expect(document.activeElement).toBe(buttons[2]!.element); await buttons[2]!.trigger("keydown", { key: "ArrowRight" }); // stays within inner when looping inner list? Inner has 2 items; next wraps to Inner1 expect(document.activeElement).toBe(buttons[1]!.element); w.unmount(); }); it("keyed reorder changes the next arrow target", async () => { const keys = ref(["a", "b", "c"]); const w = mount( defineComponent({ setup: () => () => h(Toolbar, { "aria-label": "bar" }, () => keys.value.map((key) => h(Button, { key }, () => key)), ), }), { attachTo: document.body }, ); await nextTick(); 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); w.unmount(); }); it("Enter activates a toggle once and does not double-fire", async () => { const change = vi.fn(); const w = mount( defineComponent({ render: () => h(Toolbar, { "aria-label": "bar" }, () => [ h( ToggleButtonGroup, { selectionMode: "multiple", onSelectionChange: change }, () => [h(ToggleButton, { id: "a" }, () => "A")], ), ]), }), { attachTo: document.body }, ); await nextTick(); await flushPromises(); const button = w.get("button"); (button.element as HTMLElement).focus(); await button.trigger("keydown", { key: "Enter" }); await button.trigger("click"); expect(change).toHaveBeenCalledTimes(1); w.unmount(); }); }); describe("Toolbar orientation inheritance", () => { it("ToggleButtonGroup inherits toolbar orientation and restores after override", async () => { const override = ref(undefined); const w = mount( defineComponent({ setup: () => () => h(Toolbar, { orientation: "vertical", "aria-label": "bar" }, () => [ h( ToggleButtonGroup, { selectionMode: "multiple", orientation: override.value, }, () => [h(ToggleButton, { id: "a" }, () => "A")], ), ]), }), { attachTo: document.body }, ); await nextTick(); let group = w.get('[data-slot="toggle-button-group"]'); expect(group.attributes("aria-orientation")).toBe("vertical"); override.value = "horizontal"; await nextTick(); group = w.get('[data-slot="toggle-button-group"]'); expect(group.attributes("aria-orientation")).toBe("horizontal"); override.value = undefined; await nextTick(); group = w.get('[data-slot="toggle-button-group"]'); expect(group.attributes("aria-orientation")).toBe("vertical"); w.unmount(); }); it("Separator defaults to the perpendicular toolbar direction", async () => { const w = mount( defineComponent({ render: () => h(Toolbar, { orientation: "horizontal", "aria-label": "bar" }, () => [ h(Button, () => "A"), h(Separator), h(Button, () => "B"), ]), }), { attachTo: document.body }, ); await nextTick(); const sep = w.get('[data-slot="separator"]'); expect(sep.classes().join(" ")).toContain("hv-separator--vertical"); w.unmount(); }); it("explicit Separator orientation overrides toolbar default", async () => { const w = mount( defineComponent({ render: () => h(Toolbar, { orientation: "horizontal", "aria-label": "bar" }, () => [ h(Button, () => "A"), h(Separator, { orientation: "horizontal" }), h(Button, () => "B"), ]), }), { attachTo: document.body }, ); await nextTick(); const sep = w.get('[data-slot="separator"]'); expect(sep.classes().join(" ")).toContain("hv-separator--horizontal"); w.unmount(); }); }); describe("Toolbar demos", () => { it("mounts all 5 baseline demos without Vue warnings", async () => { const warnings: string[] = []; const spy = vi.spyOn(console, "warn").mockImplementation((...args) => { warnings.push(args.map(String).join(" ")); }); const names = [ "basic", "vertical", "attached", "with-button-group", "custom-styles", ]; for (const name of names) { const mod = await import(`../apps/docs/demos/toolbar-${name}.vue`); const w = mount(mod.default, { attachTo: document.body }); await nextTick(); expect(w.html().length).toBeGreaterThan(0); w.unmount(); } spy.mockRestore(); expect(warnings).toEqual([]); }); it("includes every preview in the pinned upstream Toolbar document", () => { const document = readFileSync("apps/docs/components/toolbar.md", "utf8"); const names = [ ...document.matchAll(/ m[1]); expect(names).toEqual([ "basic", "vertical", "attached", "with-button-group", "custom-styles", ]); }); }); describe("M29 review boundaries", () => { it("keeps caller tabindex through add/remove/re-add on standalone Button", async () => { const tab = ref(-1); const w = mount( defineComponent({ setup: () => () => h(Button, { tabindex: tab.value }, () => "Solo"), }), { attachTo: document.body }, ); await nextTick(); expect(w.get("button").attributes("tabindex")).toBe("-1"); tab.value = undefined; await nextTick(); expect(w.get("button").attributes("tabindex")).toBeUndefined(); tab.value = 0; await nextTick(); expect(w.get("button").attributes("tabindex")).toBe("0"); w.unmount(); }); it("runs root keydown handler arrays in order and honors stopImmediatePropagation", async () => { const calls: string[] = []; const w = mount( defineComponent({ render: () => h( Toolbar, { "aria-label": "bar", onKeydown: [ (e: KeyboardEvent) => { calls.push("first"); e.stopImmediatePropagation(); }, () => { calls.push("second"); }, ], }, () => [h(Button, () => "A"), h(Button, () => "B")], ), }), { attachTo: document.body }, ); await nextTick(); await flushPromises(); const buttons = w.findAll("button"); (buttons[0]!.element as HTMLElement).focus(); await buttons[0]!.trigger("keydown", { key: "ArrowRight" }); expect(calls).toEqual(["first"]); expect(document.activeElement).toBe(buttons[0]!.element); w.unmount(); }); it("supports removing and re-adding the root keydown handler", async () => { const enable = ref(true); const handler = vi.fn((event: KeyboardEvent) => event.preventDefault()); const w = mount( defineComponent({ setup: () => () => h( Toolbar, { "aria-label": "bar", onKeydown: enable.value ? handler : undefined, }, () => [h(Button, () => "A"), h(Button, () => "B")], ), }), { attachTo: document.body }, ); await nextTick(); await flushPromises(); const buttons = w.findAll("button"); (buttons[0]!.element as HTMLElement).focus(); enable.value = false; await nextTick(); await buttons[0]!.trigger("keydown", { key: "ArrowRight" }); expect(handler).not.toHaveBeenCalled(); expect(document.activeElement).toBe(buttons[1]!.element); enable.value = true; await nextTick(); (buttons[1]!.element as HTMLElement).focus(); await buttons[1]!.trigger("keydown", { key: "ArrowLeft" }); expect(handler).toHaveBeenCalled(); expect(document.activeElement).toBe(buttons[1]!.element); w.unmount(); }); it("restores ToggleButtonGroup navigation after teleporting outside the toolbar", async () => { const w = mount( defineComponent({ render: () => h(Toolbar, { "aria-label": "bar" }, () => [ h(ToggleButtonGroup, { selectionMode: "multiple" }, () => [ h(ToggleButton, { value: "a" }, () => "A"), h(ToggleButton, { value: "b" }, () => "B"), ]), h(Teleport, { to: "body" }, () => [ h( ToggleButtonGroup, { selectionMode: "multiple", "data-testid": "outside" }, () => [ h(ToggleButton, { value: "x" }, () => "X"), h(ToggleButton, { value: "y" }, () => "Y"), ], ), ]), ]), }), { attachTo: document.body }, ); await nextTick(); await flushPromises(); const outside = document.querySelector('[data-testid="outside"]'); expect(outside).toBeTruthy(); const outButtons = [ ...(outside!.querySelectorAll("button") as NodeListOf), ]; expect(outButtons).toHaveLength(2); expect(outButtons[0]!.getAttribute("tabindex")).toBe("0"); expect(outButtons[1]!.getAttribute("tabindex")).toBe("-1"); outButtons[0]!.focus(); await flushPromises(); outButtons[0]!.dispatchEvent( new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true }), ); await nextTick(); expect(document.activeElement).toBe(outButtons[1]); w.unmount(); }); }); describe("M29 dynamic Teleport ownership", () => { it("restores toolbar ownership when the same node moves back inside", async () => { const inline = ref(false); const w = mount( defineComponent({ setup: () => () => h(Toolbar, { "aria-label": "bar" }, () => [ h(Button, () => "A"), h(Teleport, { to: "body", disabled: inline.value }, () => [ h(Button, { tabindex: "3", "data-moving": "" }, () => "Moving"), ]), ]), }), { attachTo: document.body }, ); await nextTick(); const moving = document.querySelector("[data-moving]") as HTMLButtonElement; expect(moving.getAttribute("tabindex")).toBe("3"); inline.value = true; await nextTick(); expect(moving.getAttribute("tabindex")).toBe("-1"); inline.value = false; await nextTick(); expect(moving.getAttribute("tabindex")).toBe("3"); w.unmount(); }); it("keeps single Tab entry after a toolbar-owned node leaves", async () => { const inline = ref(true); const w = mount( defineComponent({ setup: () => () => h(Toolbar, { "aria-label": "bar" }, () => [ h(Button, () => "A"), h(Teleport, { to: "body", disabled: inline.value }, () => [ h(Button, () => "Moving"), ]), ]), }), { attachTo: document.body }, ); await nextTick(); inline.value = false; await nextTick(); const root = w.get('[role="toolbar"]'); const stops = root .findAll("button") .filter((b) => b.attributes("tabindex") === "0"); expect(stops).toHaveLength(1); w.unmount(); }); });