|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import * as nodeModule from "node:module"; |
| 3 | +import test from "node:test"; |
| 4 | +import { JSDOM } from "jsdom"; |
| 5 | +import * as componentTestLoader from "./component-test-loader.mjs"; |
| 6 | +import { loadProductDocsContent } from "../lib/repository-content.mjs"; |
| 7 | + |
| 8 | +if (typeof nodeModule.registerHooks === "function") { |
| 9 | + nodeModule.registerHooks(componentTestLoader); |
| 10 | +} else { |
| 11 | + nodeModule.register("./component-test-loader.mjs", import.meta.url); |
| 12 | +} |
| 13 | + |
| 14 | +const { groups } = loadProductDocsContent().commands; |
| 15 | + |
| 16 | +async function renderDirectory({ width = 1280 } = {}) { |
| 17 | + const dom = new JSDOM('<div id="root"></div>', { |
| 18 | + pretendToBeVisual: true, |
| 19 | + url: "https://headless.test/docs/commands", |
| 20 | + }); |
| 21 | + Object.defineProperty(dom.window, "innerWidth", { |
| 22 | + configurable: true, |
| 23 | + value: width, |
| 24 | + }); |
| 25 | + |
| 26 | + const previousGlobals = new Map(); |
| 27 | + for (const name of [ |
| 28 | + "document", |
| 29 | + "Event", |
| 30 | + "HTMLElement", |
| 31 | + "HTMLInputElement", |
| 32 | + "InputEvent", |
| 33 | + "KeyboardEvent", |
| 34 | + "Node", |
| 35 | + "navigator", |
| 36 | + "window", |
| 37 | + ]) { |
| 38 | + previousGlobals.set(name, Object.getOwnPropertyDescriptor(globalThis, name)); |
| 39 | + Object.defineProperty(globalThis, name, { |
| 40 | + configurable: true, |
| 41 | + value: dom.window[name], |
| 42 | + writable: true, |
| 43 | + }); |
| 44 | + } |
| 45 | + previousGlobals.set( |
| 46 | + "IS_REACT_ACT_ENVIRONMENT", |
| 47 | + Object.getOwnPropertyDescriptor(globalThis, "IS_REACT_ACT_ENVIRONMENT"), |
| 48 | + ); |
| 49 | + Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", { |
| 50 | + configurable: true, |
| 51 | + value: true, |
| 52 | + writable: true, |
| 53 | + }); |
| 54 | + |
| 55 | + const [{ act, createElement }, { createRoot }, { CommandDirectory }] = |
| 56 | + await Promise.all([ |
| 57 | + import("react"), |
| 58 | + import("react-dom/client"), |
| 59 | + import("@/components/command-directory"), |
| 60 | + ]); |
| 61 | + const container = dom.window.document.querySelector("#root"); |
| 62 | + const root = createRoot(container); |
| 63 | + await act(() => root.render(createElement(CommandDirectory, { groups }))); |
| 64 | + |
| 65 | + return { |
| 66 | + act, |
| 67 | + document: dom.window.document, |
| 68 | + input: dom.window.document.querySelector("input[type=search]"), |
| 69 | + async cleanup() { |
| 70 | + await act(() => root.unmount()); |
| 71 | + dom.window.close(); |
| 72 | + for (const [name, descriptor] of previousGlobals) { |
| 73 | + if (descriptor === undefined) delete globalThis[name]; |
| 74 | + else Object.defineProperty(globalThis, name, descriptor); |
| 75 | + } |
| 76 | + }, |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +async function typeWithKeyboard(view, value) { |
| 81 | + const valueSetter = Object.getOwnPropertyDescriptor( |
| 82 | + view.input.ownerDocument.defaultView.HTMLInputElement.prototype, |
| 83 | + "value", |
| 84 | + ).set; |
| 85 | + |
| 86 | + view.input.focus(); |
| 87 | + await view.act(() => { |
| 88 | + for (let index = 0; index < value.length; index += 1) { |
| 89 | + const key = value[index]; |
| 90 | + view.input.dispatchEvent( |
| 91 | + new KeyboardEvent("keydown", { bubbles: true, key }), |
| 92 | + ); |
| 93 | + valueSetter.call(view.input, value.slice(0, index + 1)); |
| 94 | + view.input.dispatchEvent( |
| 95 | + new InputEvent("input", { |
| 96 | + bubbles: true, |
| 97 | + data: key, |
| 98 | + inputType: "insertText", |
| 99 | + }), |
| 100 | + ); |
| 101 | + view.input.dispatchEvent( |
| 102 | + new KeyboardEvent("keyup", { bubbles: true, key }), |
| 103 | + ); |
| 104 | + } |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +async function replaceSearch(view, value) { |
| 109 | + const valueSetter = Object.getOwnPropertyDescriptor( |
| 110 | + view.input.ownerDocument.defaultView.HTMLInputElement.prototype, |
| 111 | + "value", |
| 112 | + ).set; |
| 113 | + await view.act(() => { |
| 114 | + valueSetter.call(view.input, value); |
| 115 | + view.input.dispatchEvent( |
| 116 | + new InputEvent("input", { |
| 117 | + bubbles: true, |
| 118 | + data: value, |
| 119 | + inputType: "insertReplacementText", |
| 120 | + }), |
| 121 | + ); |
| 122 | + }); |
| 123 | +} |
| 124 | + |
| 125 | +function assertTocTargetsExist(document) { |
| 126 | + for (const link of document.querySelectorAll("nav[aria-label='On this page'] a")) { |
| 127 | + assert.ok( |
| 128 | + document.querySelector(link.hash), |
| 129 | + `table-of-contents target ${link.hash} must exist`, |
| 130 | + ); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +test("filters real command groups from keyboard input and updates accessible status", async () => { |
| 135 | + const view = await renderDirectory(); |
| 136 | + try { |
| 137 | + const label = view.document.querySelector("label[for=command-filter]"); |
| 138 | + const status = view.document.querySelector("[role=status]"); |
| 139 | + |
| 140 | + assert.equal(label.control, view.input); |
| 141 | + assert.equal(view.input.tabIndex, 0); |
| 142 | + assert.equal(status.getAttribute("aria-live"), "polite"); |
| 143 | + assert.equal(status.textContent, `${groups.length} of ${groups.length} groups`); |
| 144 | + assertTocTargetsExist(view.document); |
| 145 | + |
| 146 | + await typeWithKeyboard(view, "auth login"); |
| 147 | + |
| 148 | + assert.equal(view.document.activeElement, view.input); |
| 149 | + assert.equal(status.textContent, `1 of ${groups.length} groups`); |
| 150 | + assert.deepEqual( |
| 151 | + [...view.document.querySelectorAll("section h2")].map( |
| 152 | + (heading) => heading.textContent, |
| 153 | + ), |
| 154 | + ["Credential vault"], |
| 155 | + ); |
| 156 | + assert.deepEqual( |
| 157 | + [...view.document.querySelectorAll("nav[aria-label='On this page'] a")].map( |
| 158 | + (link) => link.textContent, |
| 159 | + ), |
| 160 | + ["Credential vault"], |
| 161 | + ); |
| 162 | + assertTocTargetsExist(view.document); |
| 163 | + } finally { |
| 164 | + await view.cleanup(); |
| 165 | + } |
| 166 | +}); |
| 167 | + |
| 168 | +test("handles normalized queries, no matches, and clearing without stale links", async () => { |
| 169 | + const view = await renderDirectory(); |
| 170 | + try { |
| 171 | + const status = view.document.querySelector("[role=status]"); |
| 172 | + |
| 173 | + await replaceSearch(view, " SCREENSHOT "); |
| 174 | + assert.equal(status.textContent, `1 of ${groups.length} groups`); |
| 175 | + assert.equal( |
| 176 | + view.document.querySelector("section")?.id, |
| 177 | + "capture-and-evidence", |
| 178 | + ); |
| 179 | + assertTocTargetsExist(view.document); |
| 180 | + |
| 181 | + await replaceSearch(view, "not-a-command"); |
| 182 | + assert.equal(status.textContent, "No commands match “not-a-command”."); |
| 183 | + assert.equal(view.document.querySelector("section"), null); |
| 184 | + assert.equal(view.document.querySelector("nav[aria-label='On this page']"), null); |
| 185 | + |
| 186 | + await replaceSearch(view, ""); |
| 187 | + assert.equal(status.textContent, `${groups.length} of ${groups.length} groups`); |
| 188 | + assert.equal(view.document.querySelectorAll("section").length, groups.length); |
| 189 | + assertTocTargetsExist(view.document); |
| 190 | + } finally { |
| 191 | + await view.cleanup(); |
| 192 | + } |
| 193 | +}); |
| 194 | + |
| 195 | +test("keeps command discovery functional in a mobile viewport", async () => { |
| 196 | + const view = await renderDirectory({ width: 375 }); |
| 197 | + try { |
| 198 | + assert.equal(view.document.defaultView.innerWidth, 375); |
| 199 | + assert.ok(view.input); |
| 200 | + assert.equal(view.document.querySelectorAll("section").length, groups.length); |
| 201 | + assertTocTargetsExist(view.document); |
| 202 | + |
| 203 | + await typeWithKeyboard(view, "visit"); |
| 204 | + assert.equal( |
| 205 | + view.document.querySelector("[role=status]").textContent, |
| 206 | + `1 of ${groups.length} groups`, |
| 207 | + ); |
| 208 | + assert.equal( |
| 209 | + view.document.querySelector("nav[aria-label='On this page'] a").hash, |
| 210 | + "#navigation-and-interaction", |
| 211 | + ); |
| 212 | + assert.equal( |
| 213 | + view.document.querySelector("section")?.id, |
| 214 | + "navigation-and-interaction", |
| 215 | + ); |
| 216 | + } finally { |
| 217 | + await view.cleanup(); |
| 218 | + } |
| 219 | +}); |
0 commit comments