|
| 1 | +defmodule DemoWeb.DropdownSectionsTest do |
| 2 | + use Prima.WallabyCase, async: true |
| 3 | + |
| 4 | + @dropdown_button Query.css("#dropdown-sections [aria-haspopup=menu]") |
| 5 | + @dropdown_menu Query.css("#dropdown-sections [role=menu]") |
| 6 | + |
| 7 | + feature "renders sections with proper ARIA roles", %{session: session} do |
| 8 | + session |
| 9 | + |> visit_fixture("/fixtures/dropdown-sections", "#dropdown-sections") |
| 10 | + |> click(@dropdown_button) |
| 11 | + |> assert_has(@dropdown_menu |> Query.visible(true)) |
| 12 | + |> assert_has(Query.css("#dropdown-sections [role=group]", count: 2)) |
| 13 | + end |
| 14 | + |
| 15 | + feature "renders headings with proper ARIA presentation role", %{session: session} do |
| 16 | + session |
| 17 | + |> visit_fixture("/fixtures/dropdown-sections", "#dropdown-sections") |
| 18 | + |> click(@dropdown_button) |
| 19 | + |> assert_has(Query.css("#dropdown-sections [role=presentation]", count: 2)) |
| 20 | + end |
| 21 | + |
| 22 | + feature "renders separators with proper ARIA role", %{session: session} do |
| 23 | + session |
| 24 | + |> visit_fixture("/fixtures/dropdown-sections", "#dropdown-sections") |
| 25 | + |> click(@dropdown_button) |
| 26 | + |> assert_has( |
| 27 | + Query.css("#dropdown-sections [role=separator]", count: 2) |
| 28 | + |> Query.visible(false) |
| 29 | + ) |
| 30 | + end |
| 31 | + |
| 32 | + feature "sections are automatically labeled by headings via JS hook", %{session: session} do |
| 33 | + session |
| 34 | + |> visit_fixture("/fixtures/dropdown-sections", "#dropdown-sections") |
| 35 | + |> click(@dropdown_button) |
| 36 | + # Check that both sections have aria-labelledby attributes (auto-generated by JS) |
| 37 | + |> assert_has( |
| 38 | + Query.css( |
| 39 | + "#dropdown-sections [role=group][aria-labelledby^='dropdown-sections-section-']", |
| 40 | + count: 2 |
| 41 | + ) |
| 42 | + ) |
| 43 | + # Verify the first section's heading ID matches its aria-labelledby |
| 44 | + |> execute_script(""" |
| 45 | + const firstSection = document.querySelector('#dropdown-sections [role=group]'); |
| 46 | + const labelId = firstSection.getAttribute('aria-labelledby'); |
| 47 | + const heading = document.getElementById(labelId); |
| 48 | + return heading && heading.getAttribute('role') === 'presentation'; |
| 49 | + """) |
| 50 | + end |
| 51 | + |
| 52 | + feature "keyboard navigation skips headings and separators", %{session: session} do |
| 53 | + session |
| 54 | + |> visit_fixture("/fixtures/dropdown-sections", "#dropdown-sections") |
| 55 | + |> click(@dropdown_button) |
| 56 | + |> assert_has(@dropdown_menu |> Query.visible(true)) |
| 57 | + |> send_keys([:down_arrow]) |
| 58 | + |> assert_has(Query.css("#dropdown-sections-item-0[data-focus]")) |
| 59 | + |> send_keys([:down_arrow]) |
| 60 | + |> assert_has(Query.css("#dropdown-sections-item-1[data-focus]")) |
| 61 | + |> send_keys([:down_arrow]) |
| 62 | + |> assert_has(Query.css("#dropdown-sections-item-2[data-focus]")) |
| 63 | + |> send_keys([:down_arrow]) |
| 64 | + |> assert_has(Query.css("#dropdown-sections-item-3[data-focus]")) |
| 65 | + |> send_keys([:down_arrow]) |
| 66 | + |> assert_has(Query.css("#dropdown-sections-item-4[data-focus]")) |
| 67 | + end |
| 68 | + |
| 69 | + feature "Home key navigates to first menu item, skipping headings", %{session: session} do |
| 70 | + session |
| 71 | + |> visit_fixture("/fixtures/dropdown-sections", "#dropdown-sections") |
| 72 | + |> click(@dropdown_button) |
| 73 | + |> send_keys([:end]) |
| 74 | + |> assert_has(Query.css("#dropdown-sections [role=menuitem]:last-of-type[data-focus]")) |
| 75 | + |> send_keys([:home]) |
| 76 | + |> assert_has(Query.css("#dropdown-sections [role=menuitem]:first-of-type[data-focus]")) |
| 77 | + end |
| 78 | + |
| 79 | + feature "End key navigates to last menu item, skipping separators", %{session: session} do |
| 80 | + session |
| 81 | + |> visit_fixture("/fixtures/dropdown-sections", "#dropdown-sections") |
| 82 | + |> click(@dropdown_button) |
| 83 | + |> send_keys([:home]) |
| 84 | + |> assert_has(Query.css("#dropdown-sections [role=menuitem]:first-of-type[data-focus]")) |
| 85 | + |> send_keys([:end]) |
| 86 | + |> assert_has(Query.css("#dropdown-sections [role=menuitem]:last-of-type[data-focus]")) |
| 87 | + end |
| 88 | +end |
0 commit comments