fix: disable link navigation and align breadcrumb actions and date focus

This commit is contained in:
maoyingjie
2026-10-29 09:03:52 +08:00
parent 680a592e7b
commit 68d45c66f9
27 changed files with 983 additions and 44 deletions
+116 -3
View File
@@ -1,6 +1,6 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi } from "vitest";
import { mount } from "@vue/test-utils";
import { h, inject, defineComponent, nextTick } from "vue";
import { h, inject, defineComponent, nextTick, ref } from "vue";
import {
Surface,
surfaceContextKey,
@@ -96,7 +96,7 @@ describe("Link", () => {
});
const a = w.get("a");
expect(a.attributes("aria-disabled")).toBe("true");
expect(a.attributes("href")).toBe("#");
expect(a.attributes("href")).toBeUndefined();
await a.trigger("click");
expect(clicks).toBe(0);
expect(captureClicks).toBe(0);
@@ -559,3 +559,116 @@ it("omits the implicit same-window target so host routers can intercept navigati
expect(explicit.attributes("target")).toBe("_self");
explicit.unmount();
});
it.each([false, true])(
"removes disabled navigation attributes and restores them dynamically (asChild=%s)",
async (asChild) => {
const click = vi.fn();
const childCapture = vi.fn();
const childRef = ref<HTMLElement>();
const w = mount(Link, {
props: {
asChild,
href: "#parent",
target: "_blank",
download: "parent.txt",
isDisabled: true,
},
attrs: { class: "consumer", onClick: click, "data-test": "kept" },
slots: {
default: () =>
asChild
? h(
"a",
{
href: "#child",
target: "_self",
download: "child.txt",
ref: childRef,
tabIndex: 0,
onClickCapture: childCapture,
},
"link",
)
: "link",
},
attachTo: document.body,
});
try {
const root = w.get("a");
expect(root.attributes("href")).toBeUndefined();
expect(root.attributes("target")).toBeUndefined();
expect(root.attributes("download")).toBeUndefined();
expect(root.attributes("aria-disabled")).toBe("true");
expect(root.attributes("tabindex")).toBe("-1");
expect(root.classes()).toContain("consumer");
expect(root.attributes("data-test")).toBe("kept");
if (asChild) expect(childRef.value).toBe(root.element);
const event = new MouseEvent("click", {
bubbles: true,
cancelable: true,
});
root.element.dispatchEvent(event);
expect(event.defaultPrevented).toBe(true);
const middle = new MouseEvent("auxclick", {
bubbles: true,
cancelable: true,
button: 1,
});
root.element.dispatchEvent(middle);
expect(middle.defaultPrevented).toBe(true);
expect(click).not.toHaveBeenCalled();
expect(childCapture).not.toHaveBeenCalled();
await w.setProps({ isDisabled: false });
expect(root.attributes("href")).toBe(asChild ? "#child" : "#parent");
expect(root.attributes("download")).toBe(
asChild ? "child.txt" : "parent.txt",
);
await root.trigger("click");
expect(click).toHaveBeenCalledTimes(1);
if (asChild) expect(childCapture).toHaveBeenCalledTimes(1);
await w.setProps({ isDisabled: true });
expect(root.attributes("href")).toBeUndefined();
} finally {
w.unmount();
}
},
);
it("preserves forwarding child refs and handlers while respecting cancellation", async () => {
const child = vi.fn(),
parent = vi.fn();
const forwardedRef = ref();
const Forwarded = defineComponent({
inheritAttrs: false,
setup(_, { attrs }) {
return () => h("a", attrs, "forwarded");
},
});
const w = mount(Link, {
props: { asChild: true, isDisabled: true },
attrs: {
onClick: parent,
onClickCapture: (e: Event) => e.preventDefault(),
},
slots: {
default: () =>
h(Forwarded, { href: "#forwarded", ref: forwardedRef, onClick: child }),
},
attachTo: document.body,
});
try {
expect(w.get("a").attributes("href")).toBeUndefined();
await w.get("a").trigger("click");
expect(child).not.toHaveBeenCalled();
await w.setProps({ isDisabled: false });
expect(forwardedRef.value.$el).toBe(w.get("a").element);
const event = new MouseEvent("click", { bubbles: true, cancelable: true });
w.get("a").element.dispatchEvent(event);
expect(event.defaultPrevented).toBe(true);
expect(parent).toHaveBeenCalledTimes(1);
expect(child).toHaveBeenCalledTimes(1);
} finally {
w.unmount();
}
});