|
1 | 1 | import assert from "node:assert/strict"; |
2 | | -import { readFile } from "node:fs/promises"; |
3 | | -import { test } from "node:test"; |
4 | 2 |
|
5 | | -import { getButtonKeyboardAction } from "./button-keyboard.ts"; |
| 3 | +import { test } from "vite-plus/test"; |
6 | 4 |
|
7 | | -const source = await readFile(new URL("./ActionButton.vue", import.meta.url), "utf8"); |
| 5 | +import ActionButton from "./ActionButton.vue"; |
| 6 | +import { getButtonKeyboardAction } from "./button-keyboard.ts"; |
| 7 | +import { mountInteraction } from "./testing/mount.ts"; |
8 | 8 |
|
9 | | -void test("matches native keyboard activation timing", () => { |
| 9 | +test("matches native keyboard activation timing", () => { |
10 | 10 | assert.equal(getButtonKeyboardAction("Enter", "keydown"), "activate"); |
11 | 11 | assert.equal(getButtonKeyboardAction("Enter", "keyup"), "ignore"); |
12 | 12 | assert.equal(getButtonKeyboardAction(" ", "keydown"), "prevent"); |
13 | 13 | assert.equal(getButtonKeyboardAction(" ", "keyup"), "activate"); |
14 | 14 | assert.equal(getButtonKeyboardAction("Escape", "keydown"), "ignore"); |
15 | 15 | }); |
16 | 16 |
|
17 | | -void test("ships typed disabled and loading semantics in an explicit SFC", () => { |
18 | | - assert.match(source, /defineEmits<\{[\s\S]*press: \[event: MouseEvent\]/); |
19 | | - assert.match(source, /:disabled="isNativeButton \? disabled : undefined"/); |
20 | | - assert.match(source, /:aria-disabled=/); |
21 | | - assert.match(source, /:aria-busy=/); |
22 | | - assert.match(source, /:tabindex="tabIndex"/); |
23 | | - assert.doesNotMatch(source, /\bh\s*\(/); |
24 | | - assert.doesNotMatch(source, /defineOptions|withDefaults|interface (?:Props|Emits)/); |
| 17 | +test("renders a native button with an accessible name", () => { |
| 18 | + const handle = mountInteraction(ActionButton, { slots: { default: "Save" } }); |
| 19 | + const button = handle.getByRole("button", { name: "Save" }); |
| 20 | + |
| 21 | + assert.equal(button.tagName, "BUTTON"); |
| 22 | + assert.equal(button.getAttribute("type"), "button"); |
| 23 | + assert.equal(button.getAttribute("data-vize-ui"), "button"); |
| 24 | + assert.equal(button.getAttribute("data-state"), "idle"); |
| 25 | + assert.equal(button.getAttribute("aria-disabled"), null); |
| 26 | + assert.equal(button.getAttribute("aria-busy"), null); |
| 27 | + handle.unmount(); |
| 28 | +}); |
| 29 | + |
| 30 | +test("joins the tab order and focuses programmatically", async () => { |
| 31 | + const handle = mountInteraction(ActionButton, { slots: { default: "Save" } }); |
| 32 | + const button = handle.getByRole("button"); |
| 33 | + |
| 34 | + assert.ok((await handle.tab()) === button, "Tab must move focus to the button"); |
| 35 | + assert.ok(handle.activeElement() === button); |
| 36 | + |
| 37 | + button.blur(); |
| 38 | + handle.exposes<{ focus: (options?: FocusOptions) => void }>().focus(); |
| 39 | + assert.ok(handle.activeElement() === button, "exposed focus() must focus the control"); |
| 40 | + handle.unmount(); |
| 41 | +}); |
| 42 | + |
| 43 | +test("pointer click fires exactly one press", async () => { |
| 44 | + const handle = mountInteraction(ActionButton, { slots: { default: "Save" } }); |
| 45 | + |
| 46 | + await handle.click(handle.getByRole("button")); |
| 47 | + |
| 48 | + const presses = handle.wrapper.emitted("press"); |
| 49 | + assert.equal(presses?.length, 1); |
| 50 | + assert.ok(presses?.[0]?.[0] instanceof MouseEvent); |
| 51 | + handle.unmount(); |
| 52 | +}); |
| 53 | + |
| 54 | +test("Enter and Space each fire exactly one press on a native button", async () => { |
| 55 | + const handle = mountInteraction(ActionButton, { slots: { default: "Save" } }); |
| 56 | + const button = handle.getByRole("button"); |
| 57 | + button.focus(); |
| 58 | + |
| 59 | + const enter = await handle.press(button, "Enter"); |
| 60 | + assert.equal(enter.activated, true); |
| 61 | + assert.equal(handle.wrapper.emitted("press")?.length, 1); |
| 62 | + |
| 63 | + const space = await handle.press(button, " "); |
| 64 | + assert.equal(space.activated, true); |
| 65 | + assert.equal(handle.wrapper.emitted("press")?.length, 2); |
| 66 | + handle.unmount(); |
| 67 | +}); |
| 68 | + |
| 69 | +test("Enter and Space activate a non-native button through its own handlers", async () => { |
| 70 | + const handle = mountInteraction(ActionButton, { |
| 71 | + props: { as: "div" }, |
| 72 | + slots: { default: "Save" }, |
| 73 | + }); |
| 74 | + const button = handle.getByRole("button", { name: "Save" }); |
| 75 | + |
| 76 | + assert.equal(button.tagName, "DIV"); |
| 77 | + assert.equal(button.getAttribute("role"), "button"); |
| 78 | + assert.equal(button.getAttribute("tabindex"), "0"); |
| 79 | + |
| 80 | + const enter = await handle.press(button, "Enter"); |
| 81 | + assert.equal(enter.activated, false, "the component, not the harness, must synthesize clicks"); |
| 82 | + assert.equal(handle.wrapper.emitted("press")?.length, 1); |
| 83 | + |
| 84 | + const space = await handle.press(button, " "); |
| 85 | + assert.equal(space.keydownPrevented, true, "Space keydown must be canceled to prevent scrolling"); |
| 86 | + assert.equal(handle.wrapper.emitted("press")?.length, 2); |
| 87 | + |
| 88 | + const escape = await handle.press(button, "Escape"); |
| 89 | + assert.equal(escape.keydownPrevented, false); |
| 90 | + assert.equal(handle.wrapper.emitted("press")?.length, 2); |
| 91 | + handle.unmount(); |
| 92 | +}); |
| 93 | + |
| 94 | +test("keyboard and pointer presses are indistinguishable MouseEvents in order", async () => { |
| 95 | + const handle = mountInteraction(ActionButton, { |
| 96 | + slots: { default: "Save" }, |
| 97 | + record: ["press"], |
| 98 | + }); |
| 99 | + const button = handle.getByRole("button"); |
| 100 | + button.focus(); |
| 101 | + |
| 102 | + await handle.click(button); |
| 103 | + await handle.press(button, "Enter"); |
| 104 | + await handle.press(button, " "); |
| 105 | + |
| 106 | + const recorded = handle.recorded(); |
| 107 | + assert.equal(recorded.length, 3); |
| 108 | + for (const emit of recorded) { |
| 109 | + assert.equal(emit.event, "press"); |
| 110 | + assert.ok(emit.payload[0] instanceof MouseEvent); |
| 111 | + } |
| 112 | + handle.unmount(); |
| 113 | +}); |
| 114 | + |
| 115 | +test("disabled native button removes activation and keeps native semantics", async () => { |
| 116 | + const handle = mountInteraction(ActionButton, { |
| 117 | + props: { disabled: true }, |
| 118 | + slots: { default: "Save" }, |
| 119 | + }); |
| 120 | + const button = handle.getByRole("button"); |
| 121 | + |
| 122 | + assert.ok(button.hasAttribute("disabled"), "native disabled must be forwarded"); |
| 123 | + assert.equal(button.getAttribute("aria-disabled"), null, "native disabled needs no aria mirror"); |
| 124 | + assert.equal(button.getAttribute("data-state"), "disabled"); |
| 125 | + |
| 126 | + await handle.click(button); |
| 127 | + await handle.press(button, "Enter"); |
| 128 | + await handle.press(button, " "); |
| 129 | + assert.equal(handle.wrapper.emitted("press"), undefined); |
| 130 | + |
| 131 | + assert.ok((await handle.tab()) === null, "a disabled button must leave the tab order"); |
| 132 | + handle.unmount(); |
| 133 | +}); |
| 134 | + |
| 135 | +test("disabled non-native button leaves the tab order and announces aria-disabled", async () => { |
| 136 | + const handle = mountInteraction(ActionButton, { |
| 137 | + props: { as: "div", disabled: true }, |
| 138 | + slots: { default: "Save" }, |
| 139 | + }); |
| 140 | + const button = handle.getByRole("button"); |
| 141 | + |
| 142 | + assert.equal(button.getAttribute("tabindex"), "-1"); |
| 143 | + assert.equal(button.getAttribute("aria-disabled"), "true"); |
| 144 | + |
| 145 | + await handle.click(button); |
| 146 | + await handle.press(button, "Enter"); |
| 147 | + await handle.press(button, " "); |
| 148 | + assert.equal(handle.wrapper.emitted("press"), undefined); |
| 149 | + assert.ok((await handle.tab()) === null, "tabindex -1 must remove it from the tab order"); |
| 150 | + handle.unmount(); |
| 151 | +}); |
| 152 | + |
| 153 | +test("loading button announces busy, stays focusable, and suppresses press", async () => { |
| 154 | + const handle = mountInteraction(ActionButton, { |
| 155 | + props: { loading: true }, |
| 156 | + slots: { default: "Save" }, |
| 157 | + }); |
| 158 | + const button = handle.getByRole("button"); |
| 159 | + |
| 160 | + assert.equal(button.getAttribute("aria-busy"), "true"); |
| 161 | + assert.equal(button.getAttribute("aria-disabled"), "true"); |
| 162 | + assert.equal(button.getAttribute("data-state"), "loading"); |
| 163 | + assert.equal(button.hasAttribute("disabled"), false, "loading must not steal focus"); |
| 164 | + |
| 165 | + button.focus(); |
| 166 | + assert.ok(handle.activeElement() === button, "a busy button must keep accepting focus"); |
| 167 | + |
| 168 | + await handle.click(button); |
| 169 | + await handle.press(button, "Enter"); |
| 170 | + await handle.press(button, " "); |
| 171 | + assert.equal(handle.wrapper.emitted("press"), undefined); |
| 172 | + handle.unmount(); |
25 | 173 | }); |
26 | 174 |
|
27 | | -void test("exposes focus deliberately and provides programmable slot state", () => { |
28 | | - assert.match(source, /defineExpose\(\{ element, focus \}\)/); |
29 | | - assert.match(source, /<slot :disabled :loading :unavailable \/>/); |
30 | | - assert.match(source, /data-vize-ui="button"/); |
31 | | - assert.match(source, /:data-state=/); |
| 175 | +test("exposes disabled, loading, and unavailable to the default slot", async () => { |
| 176 | + const handle = mountInteraction(ActionButton, { |
| 177 | + slots: { |
| 178 | + default: (state: { disabled: boolean; loading: boolean; unavailable: boolean }) => |
| 179 | + `disabled:${state.disabled} loading:${state.loading} unavailable:${state.unavailable}`, |
| 180 | + }, |
| 181 | + }); |
| 182 | + |
| 183 | + assert.equal(handle.root().textContent, "disabled:false loading:false unavailable:false"); |
| 184 | + await handle.wrapper.setProps({ loading: true }); |
| 185 | + assert.equal(handle.root().textContent, "disabled:false loading:true unavailable:true"); |
| 186 | + handle.unmount(); |
32 | 187 | }); |
0 commit comments