|
| 1 | +defmodule PrimaWeb.ComboboxAriaTest do |
| 2 | + use Prima.WallabyCase, async: true |
| 3 | + |
| 4 | + @combobox_input Query.css("#demo-combobox input[role=combobox]") |
| 5 | + @options_container Query.css("#demo-combobox [data-prima-ref=options]") |
| 6 | + |
| 7 | + feature "combobox input has role=combobox attribute", %{session: session} do |
| 8 | + session |
| 9 | + |> visit_fixture("/fixtures/simple-combobox", "#demo-combobox") |
| 10 | + |> assert_has(@combobox_input) |
| 11 | + end |
| 12 | + |
| 13 | + feature "aria-expanded toggles between true and false based on dropdown state", %{ |
| 14 | + session: session |
| 15 | + } do |
| 16 | + session |
| 17 | + |> visit_fixture("/fixtures/simple-combobox", "#demo-combobox") |
| 18 | + # Initially closed - aria-expanded should be false |
| 19 | + |> assert_has(Query.css("#demo-combobox input[aria-expanded=false]")) |
| 20 | + |> assert_has(@options_container |> Query.visible(false)) |
| 21 | + # Open options - aria-expanded should be true |
| 22 | + |> click(@combobox_input) |
| 23 | + |> assert_has(@options_container |> Query.visible(true)) |
| 24 | + |> assert_has(Query.css("#demo-combobox input[aria-expanded=true]")) |
| 25 | + # Close by clicking outside - aria-expanded should be false again |
| 26 | + |> click(Query.css("body")) |
| 27 | + |> assert_has(@options_container |> Query.visible(false)) |
| 28 | + |> assert_has(Query.css("#demo-combobox input[aria-expanded=false]")) |
| 29 | + end |
| 30 | + |
| 31 | + feature "aria-controls references the options container ID", %{session: session} do |
| 32 | + session |
| 33 | + |> visit_fixture("/fixtures/simple-combobox", "#demo-combobox") |
| 34 | + |> assert_has( |
| 35 | + Query.css("#demo-combobox input[role=combobox][aria-controls='demo-combobox-options']") |
| 36 | + ) |
| 37 | + end |
| 38 | + |
| 39 | + feature "options container has role=listbox attribute", %{session: session} do |
| 40 | + session |
| 41 | + |> visit_fixture("/fixtures/simple-combobox", "#demo-combobox") |
| 42 | + |> assert_has(Query.css("#demo-combobox-options[role=listbox]") |> Query.visible(false)) |
| 43 | + end |
| 44 | + |
| 45 | + feature "aria-activedescendant tracks the focused option", %{session: session} do |
| 46 | + session |
| 47 | + |> visit_fixture("/fixtures/simple-combobox", "#demo-combobox") |
| 48 | + # Initially no aria-activedescendant when closed |
| 49 | + |> assert_missing(Query.css("#demo-combobox input[aria-activedescendant]")) |
| 50 | + # Open options |
| 51 | + |> click(@combobox_input) |
| 52 | + |> assert_has(@options_container |> Query.visible(true)) |
| 53 | + # First option should be focused, input should have aria-activedescendant pointing to it |
| 54 | + |> execute_script( |
| 55 | + "const input = document.querySelector('#demo-combobox input[role=combobox]'); const firstOption = document.querySelector('#demo-combobox [role=option][data-focus=true]'); return {inputAria: input.getAttribute('aria-activedescendant'), optionId: firstOption ? firstOption.id : null}", |
| 56 | + fn result -> |
| 57 | + assert result["optionId"] != nil, "Expected first option to have an ID" |
| 58 | + |
| 59 | + assert result["inputAria"] == result["optionId"], |
| 60 | + "Expected aria-activedescendant to match focused option ID" |
| 61 | + end |
| 62 | + ) |
| 63 | + # Navigate down to second option |
| 64 | + |> send_keys([:down_arrow]) |
| 65 | + |> execute_script( |
| 66 | + "const input = document.querySelector('#demo-combobox input[role=combobox]'); const focusedOption = document.querySelector('#demo-combobox [role=option][data-focus=true]'); return {inputAria: input.getAttribute('aria-activedescendant'), optionId: focusedOption ? focusedOption.id : null}", |
| 67 | + fn result -> |
| 68 | + assert result["optionId"] != nil, "Expected second option to have an ID" |
| 69 | + |
| 70 | + assert result["inputAria"] == result["optionId"], |
| 71 | + "Expected aria-activedescendant to match focused option ID after navigation" |
| 72 | + end |
| 73 | + ) |
| 74 | + end |
| 75 | + |
| 76 | + feature "combobox input has aria-autocomplete=list attribute", %{session: session} do |
| 77 | + session |
| 78 | + |> visit_fixture("/fixtures/simple-combobox", "#demo-combobox") |
| 79 | + |> assert_has(Query.css("#demo-combobox input[role=combobox][aria-autocomplete=list]")) |
| 80 | + end |
| 81 | + |
| 82 | + feature "combobox input has aria-haspopup=listbox attribute", %{session: session} do |
| 83 | + session |
| 84 | + |> visit_fixture("/fixtures/simple-combobox", "#demo-combobox") |
| 85 | + |> assert_has(Query.css("#demo-combobox input[role=combobox][aria-haspopup=listbox]")) |
| 86 | + end |
| 87 | +end |
0 commit comments