|
| 1 | +defmodule DemoWeb.ModalWithoutPortalTest do |
| 2 | + use Prima.WallabyCase, async: true |
| 3 | + |
| 4 | + @modal_panel Query.css("#no-portal-modal [data-prima-ref=modal-panel]") |
| 5 | + @modal_overlay Query.css("#no-portal-modal [data-prima-ref=modal-overlay]") |
| 6 | + @modal_container Query.css("#no-portal-modal") |
| 7 | + |
| 8 | + feature "modal without portal shows when button is clicked", %{session: session} do |
| 9 | + session |
| 10 | + |> visit_fixture("/fixtures/modal-without-portal", "#no-portal-modal") |
| 11 | + |> assert_has(@modal_container |> Query.visible(false)) |
| 12 | + |> assert_has(@modal_overlay |> Query.visible(false)) |
| 13 | + |> assert_has(@modal_panel |> Query.visible(false)) |
| 14 | + |> click(Query.css("#modal-without-portal button")) |
| 15 | + |> assert_has(@modal_container |> Query.visible(true)) |
| 16 | + |> assert_has(@modal_overlay |> Query.visible(true)) |
| 17 | + |> assert_has(@modal_panel |> Query.visible(true)) |
| 18 | + end |
| 19 | + |
| 20 | + feature "modal without portal closes when user clicks close button", %{session: session} do |
| 21 | + session |
| 22 | + |> visit_fixture("/fixtures/modal-without-portal", "#no-portal-modal") |
| 23 | + |> click(Query.css("#modal-without-portal button")) |
| 24 | + |> assert_has(@modal_container |> Query.visible(true)) |
| 25 | + |> click(Query.css("#no-portal-modal [testing-ref=close-button]")) |
| 26 | + |> assert_has(@modal_container |> Query.visible(false)) |
| 27 | + |> assert_has(@modal_overlay |> Query.visible(false)) |
| 28 | + |> assert_has(@modal_panel |> Query.visible(false)) |
| 29 | + end |
| 30 | + |
| 31 | + feature "modal without portal closes on escape key", %{session: session} do |
| 32 | + session |
| 33 | + |> visit_fixture("/fixtures/modal-without-portal", "#no-portal-modal") |
| 34 | + |> click(Query.css("#modal-without-portal button")) |
| 35 | + |> assert_has(@modal_container |> Query.visible(true)) |
| 36 | + |> send_keys([:escape]) |
| 37 | + |> assert_has(@modal_container |> Query.visible(false)) |
| 38 | + |> assert_has(@modal_overlay |> Query.visible(false)) |
| 39 | + |> assert_has(@modal_panel |> Query.visible(false)) |
| 40 | + end |
| 41 | + |
| 42 | + feature "modal without portal prevents body scroll", %{session: session} do |
| 43 | + session |
| 44 | + |> visit_fixture("/fixtures/modal-without-portal", "#no-portal-modal") |
| 45 | + |> execute_script("return document.body.style.overflow", fn overflow -> |
| 46 | + assert overflow == "" |
| 47 | + end) |
| 48 | + |> click(Query.css("#modal-without-portal button")) |
| 49 | + |> assert_has(@modal_container |> Query.visible(true)) |
| 50 | + |> execute_script("return document.body.style.overflow", fn overflow -> |
| 51 | + assert overflow == "hidden" |
| 52 | + end) |
| 53 | + |> click(Query.css("#no-portal-modal [testing-ref=close-button]")) |
| 54 | + |> assert_has(@modal_container |> Query.visible(false)) |
| 55 | + |> execute_script("return document.body.style.overflow", fn overflow -> |
| 56 | + assert overflow == "" |
| 57 | + end) |
| 58 | + end |
| 59 | + |
| 60 | + feature "modal without portal has proper ARIA attributes", %{session: session} do |
| 61 | + session |
| 62 | + |> visit("/fixtures/modal-without-portal") |
| 63 | + |> assert_has( |
| 64 | + Query.css("#no-portal-modal[role=dialog][aria-modal=true]") |
| 65 | + |> Query.visible(false) |
| 66 | + ) |
| 67 | + end |
| 68 | + |
| 69 | + feature "modal without portal manages aria-hidden state", %{session: session} do |
| 70 | + session |
| 71 | + |> visit_fixture("/fixtures/modal-without-portal", "#no-portal-modal") |
| 72 | + |> assert_has( |
| 73 | + Query.css("#no-portal-modal[aria-hidden=true]") |
| 74 | + |> Query.visible(false) |
| 75 | + ) |
| 76 | + |> click(Query.css("#modal-without-portal button")) |
| 77 | + |> assert_has(@modal_container |> Query.visible(true)) |
| 78 | + |> assert_has(Query.css("#no-portal-modal:not([aria-hidden])")) |
| 79 | + |> send_keys([:escape]) |
| 80 | + |> assert_has(@modal_container |> Query.visible(false)) |
| 81 | + |> assert_has( |
| 82 | + Query.css("#no-portal-modal[aria-hidden=true]") |
| 83 | + |> Query.visible(false) |
| 84 | + ) |
| 85 | + end |
| 86 | + |
| 87 | + feature "modal without portal auto-generates ARIA label relationships", %{session: session} do |
| 88 | + session |
| 89 | + |> visit_fixture("/fixtures/modal-without-portal", "#no-portal-modal") |
| 90 | + |> click(Query.css("#modal-without-portal button")) |
| 91 | + |> assert_has(@modal_container |> Query.visible(true)) |
| 92 | + |> assert_has(Query.css("#no-portal-modal[aria-labelledby='no-portal-modal-title']")) |
| 93 | + |> assert_has(Query.css("#no-portal-modal-title")) |
| 94 | + end |
| 95 | + |
| 96 | + feature "modal without portal manages focus correctly", %{session: session} do |
| 97 | + session |
| 98 | + |> visit_fixture("/fixtures/modal-without-portal", "#no-portal-modal") |
| 99 | + |> click(Query.css("#modal-without-portal button")) |
| 100 | + |> assert_has(@modal_container |> Query.visible(true)) |
| 101 | + |> assert_has(Query.css("#no-portal-modal [testing-ref=close-button]:focus")) |
| 102 | + end |
| 103 | +end |
0 commit comments