109 lines
4.0 KiB
TypeScript
109 lines
4.0 KiB
TypeScript
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<typeof mount>[] = [];
|
|
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('<Button :tabindex="-1">Excluded</Button>');
|
|
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(
|
|
'<div><Button as-child><button tabindex="-1">Button child</button></Button><ToggleButton as-child><button tabindex="-1">Toggle child</button></ToggleButton></div>',
|
|
);
|
|
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(
|
|
'<Toolbar><Button>A</Button><Teleport to="body" :disabled="inline"><Button data-outside tabindex="3">Moving</Button></Teleport></Toolbar>',
|
|
() => ({ 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(
|
|
'<Toolbar><Button>A</Button><Teleport to="body" :disabled="inline"><ToggleButtonGroup data-moving><ToggleButton value="x">X</ToggleButton><ToggleButton value="y">Y</ToggleButton></ToggleButtonGroup></Teleport></Toolbar>',
|
|
() => ({ inline }),
|
|
);
|
|
await nextTick();
|
|
inline.value = false;
|
|
await nextTick();
|
|
const buttons = document.querySelectorAll<HTMLButtonElement>(
|
|
"[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(
|
|
'<Toolbar @keydown="handler"><Button>A</Button><Button>B</Button></Toolbar>',
|
|
() => ({ 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(
|
|
'<Toolbar><Button>A</Button><Button>B</Button><Teleport to="body"><Button>Outside</Button></Teleport></Toolbar>',
|
|
);
|
|
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);
|
|
});
|