Fix hook and add keyboard navigation tests

Co-authored-by: cloud8421 <537608+cloud8421@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-11-09 17:31:30 +00:00
committed by Claudio Ortolina
parent df55065b32
commit 36d1ba1b48
2 changed files with 45 additions and 4 deletions
@@ -1,6 +1,5 @@
export default {
mounted() {
this.searchInput = document.getElementById("universal-search-input");
this.selectedIndex = -1; // -1 means search input is focused
this.keydownHandler = (event) => {
@@ -89,15 +88,16 @@ export default {
updateSelection() {
const results = this.getResultElements();
// Remove all aria-selected attributes and return focus to search input
// Remove all aria-selected attributes
results.forEach((result) => {
result.removeAttribute("aria-selected");
});
if (this.selectedIndex === -1) {
// Focus search input
if (this.searchInput) {
this.searchInput.focus();
const searchInput = document.getElementById("universal-search-input");
if (searchInput) {
searchInput.focus();
}
} else if (this.selectedIndex >= 0 && this.selectedIndex < results.length) {
// Mark selected result
@@ -107,4 +107,45 @@ defmodule MusicLibraryWeb.UniversalSearchLive.IndexTest do
|> assert_has("div", text: "1 result")
end
end
describe "Keyboard navigation" do
test "displays keyboard navigation hints when results are present", %{
conn: conn,
collection_record: collection_record
} do
conn
|> visit(~p"/collection")
|> click_button("Search (Cmd/Ctrl+K)")
|> fill_in("Universal Search", with: collection_record.title)
|> assert_has("kbd", text: "")
|> assert_has("kbd", text: "")
|> assert_has("span", text: "Navigate")
|> assert_has("kbd", text: "Enter")
|> assert_has("span", text: "Select")
end
test "search results have role=option for keyboard navigation", %{
conn: conn,
collection_record: collection_record
} do
conn
|> visit(~p"/collection")
|> click_button("Search (Cmd/Ctrl+K)")
|> fill_in("Universal Search", with: collection_record.title)
|> assert_has("[role='option']")
end
test "hook is attached to the universal search container", %{conn: conn} do
{:ok, view, _html} =
conn
|> visit(~p"/collection")
|> PhoenixTest.unwrap()
# Get the rendered HTML
html = Phoenix.LiveViewTest.render(view)
# Verify the hook is attached
assert html =~ ~s(phx-hook="UniversalSearchNavigation")
end
end
end