From 32b5060fd2420d5ea1456917e6202f78645dfce0 Mon Sep 17 00:00:00 2001 From: Arthur Clemens Date: Fri, 13 Sep 2024 19:27:00 +0200 Subject: [PATCH] Copy of Phoenix.Component.focus_wrap that ensures sorted attributes to aid testing --- lib/component.ex | 42 +++++++++++++------ lib/helpers/attribute_helpers.ex | 12 +++--- test/components/action_menu_test.exs | 18 ++++---- test/components/dialog_test.exs | 40 +++++++++--------- .../drawer_content_deprecated_test.exs | 26 ++++++------ test/components/drawer_test.exs | 28 ++++++------- test/components/dropdown_test.exs | 18 ++++---- test/components/select_menu_test.exs | 28 ++++++------- test/components/subnav_test.exs | 4 +- 9 files changed, 116 insertions(+), 100 deletions(-) diff --git a/lib/component.ex b/lib/component.ex index 9729ce74..1b19b604 100644 --- a/lib/component.ex +++ b/lib/component.ex @@ -7132,13 +7132,13 @@ defmodule PrimerLive.Component do |> assign(:focus_wrap_attrs, focus_wrap_attrs) ~H""" - <.focus_wrap {@focus_wrap_attrs}> + <.pl_focus_wrap {@focus_wrap_attrs}> - + """ end @@ -7824,7 +7824,7 @@ defmodule PrimerLive.Component do
- <.focus_wrap {@focus_wrap_attrs}> + <.pl_focus_wrap {@focus_wrap_attrs}> <%= if not is_nil(@menu_title) do %>

<%= @menu_title %>

@@ -7883,7 +7883,7 @@ defmodule PrimerLive.Component do
<% end %> <% end %> - +
@@ -8145,9 +8145,9 @@ defmodule PrimerLive.Component do
- <.focus_wrap {@focus_wrap_attrs}> + <.pl_focus_wrap {@focus_wrap_attrs}> <%= render_slot(@inner_block) %> - +
@@ -11496,7 +11496,7 @@ defmodule PrimerLive.Component do ``` - Dialog content is automatically wrapped inside a `Phoenix.Component.focus_wrap/1` so that navigating with Tab won't leave the dialog. + Dialog content is automatically wrapped inside a `Phoenix.Component.pl_focus_wrap/1` so that navigating with Tab won't leave the dialog. ``` <.dialog is_backdrop is_modal> @@ -11732,7 +11732,7 @@ defmodule PrimerLive.Component do
<% end %>
- <.focus_wrap {@focus_wrap_attrs}> + <.pl_focus_wrap {@focus_wrap_attrs}> <.box {@content_attrs}> <:header :if={@header_title && @header_title !== []} class={@classes.header}> <.button {@close_button_attrs}> @@ -11741,7 +11741,7 @@ defmodule PrimerLive.Component do <%= render_slot(@inner_block) %> - +
""" end @@ -12161,9 +12161,9 @@ defmodule PrimerLive.Component do <%= render_slot(@inner_block) %> <%= if @body && @body !== [] do %>
- <.focus_wrap {@focus_wrap_attrs}> + <.pl_focus_wrap {@focus_wrap_attrs}> <%= render_slot(@body) %> - +
<% end %> @@ -12254,9 +12254,9 @@ defmodule PrimerLive.Component do ~H"""
- <.focus_wrap {@focus_wrap_attrs}> + <.pl_focus_wrap {@focus_wrap_attrs}> <%= render_slot(@inner_block) %> - +
""" end @@ -13461,4 +13461,20 @@ defmodule PrimerLive.Component do <% end %> """ end + + # Copy of Phoenix.Component.focus_wrap that ensures sorted attributes to aid testing + + defp pl_focus_wrap(assigns) do + assigns = assigns |> assign(:attrs, AttributeHelpers.assigns_to_attributes_sorted(assigns, [ + id: assigns[:id], "phx-hook": "Phoenix.FocusWrap" + ])) + + ~H""" +
+ + <%= render_slot(@inner_block) %> + +
+ """ + end end diff --git a/lib/helpers/attribute_helpers.ex b/lib/helpers/attribute_helpers.ex index 99d1a050..178db8ba 100644 --- a/lib/helpers/attribute_helpers.ex +++ b/lib/helpers/attribute_helpers.ex @@ -845,10 +845,10 @@ defmodule PrimerLive.Helpers.AttributeHelpers do focus_wrap_attrs = append_attributes([ [ - id: focus_wrap_id, "data-focuswrap": "", - "phx-window-keydown": assigns.is_escapable && PromptHelpers.cancel_prompt(id), - "phx-key": "Escape" + id: focus_wrap_id, + "phx-key": "Escape", + "phx-window-keydown": assigns.is_escapable && PromptHelpers.cancel_prompt(id) ] ]) @@ -862,11 +862,11 @@ defmodule PrimerLive.Helpers.AttributeHelpers do toggle_attrs = append_attributes(toggle_rest, [ [ - class: toggle_class, "aria-haspopup": "true", "aria-owns": focus_wrap_id, - type: "button", - "phx-click": Keyword.get(toggle_rest, :"phx-click", PromptHelpers.toggle_prompt(id)) + class: toggle_class, + "phx-click": Keyword.get(toggle_rest, :"phx-click", PromptHelpers.toggle_prompt(id)), + type: "button" ] ]) diff --git a/test/components/action_menu_test.exs b/test/components/action_menu_test.exs index 2fc1921c..9ed1ebf3 100644 --- a/test/components/action_menu_test.exs +++ b/test/components/action_menu_test.exs @@ -27,7 +27,7 @@ defmodule PrimerLive.TestComponents.ActionMenuTest do """) |> format_html() == """ -
  • One
  • Two
  • Three
+
  • One
  • Two
  • Three
""" |> format_html() rescue @@ -63,7 +63,7 @@ defmodule PrimerLive.TestComponents.ActionMenuTest do """) |> format_html() == """ -
  • STRIPPED_SVG_PATHSText
  • STRIPPED_SVG_PATHSNumber
  • STRIPPED_SVG_PATHSCalendar
  • STRIPPED_SVG_PATHSIteration
+
  • STRIPPED_SVG_PATHSText
  • STRIPPED_SVG_PATHSNumber
  • STRIPPED_SVG_PATHSCalendar
  • STRIPPED_SVG_PATHSIteration
""" |> format_html() rescue @@ -86,7 +86,7 @@ defmodule PrimerLive.TestComponents.ActionMenuTest do """) |> format_html() == """ -
LIST
+
LIST
""" |> format_html() rescue @@ -109,7 +109,7 @@ defmodule PrimerLive.TestComponents.ActionMenuTest do """) |> format_html() == """ -
LIST
+
LIST
""" |> format_html() rescue @@ -132,7 +132,7 @@ defmodule PrimerLive.TestComponents.ActionMenuTest do """) |> format_html() == """ -
LIST
+
LIST
""" |> format_html() rescue @@ -155,7 +155,7 @@ defmodule PrimerLive.TestComponents.ActionMenuTest do """) |> format_html() == """ -
LIST
+
LIST
""" |> format_html() rescue @@ -184,7 +184,7 @@ defmodule PrimerLive.TestComponents.ActionMenuTest do """) |> format_html() == """ -
LIST
+
LIST
""" |> format_html() rescue @@ -210,7 +210,7 @@ defmodule PrimerLive.TestComponents.ActionMenuTest do """) |> format_html() == """ -
LIST
+
LIST
""" |> format_html() rescue @@ -247,7 +247,7 @@ defmodule PrimerLive.TestComponents.ActionMenuTest do """) |> format_html() == """ -
+
""" |> format_html() rescue diff --git a/test/components/dialog_test.exs b/test/components/dialog_test.exs index ce4f2f0a..f643498d 100644 --- a/test/components/dialog_test.exs +++ b/test/components/dialog_test.exs @@ -16,7 +16,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -36,7 +36,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -56,7 +56,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -76,7 +76,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -96,7 +96,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -116,7 +116,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -136,7 +136,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -156,7 +156,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -176,7 +176,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -196,7 +196,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -216,7 +216,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -236,7 +236,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -256,7 +256,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -276,7 +276,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -296,7 +296,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -316,7 +316,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -336,7 +336,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -365,7 +365,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -390,7 +390,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -

Dialog title

Message
Body message
Row 1
Row 2
+

Dialog title

Message
Body message
Row 1
Row 2
""" |> format_html() rescue @@ -432,7 +432,7 @@ defmodule PrimerLive.TestComponents.DialogTest do """) |> format_html() == """ -

Dialog title

Message
Body message
Row 1
Row 2
+

Dialog title

Message
Body message
Row 1
Row 2
""" |> format_html() rescue diff --git a/test/components/drawer_content_deprecated_test.exs b/test/components/drawer_content_deprecated_test.exs index 53a78fb8..1e7bf7d3 100644 --- a/test/components/drawer_content_deprecated_test.exs +++ b/test/components/drawer_content_deprecated_test.exs @@ -18,7 +18,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -40,7 +40,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -62,7 +62,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -84,7 +84,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -106,7 +106,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -128,7 +128,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -150,7 +150,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -172,7 +172,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -194,7 +194,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -216,7 +216,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -241,7 +241,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Page content
Content
+
Page content
Content
""" |> format_html() rescue @@ -266,7 +266,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Page content
Content
+
Page content
Content
""" |> format_html() rescue @@ -288,7 +288,7 @@ defmodule PrimerLive.TestComponents.DrawerContentDeprecatedTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue diff --git a/test/components/drawer_test.exs b/test/components/drawer_test.exs index 84096dca..2ea1a349 100644 --- a/test/components/drawer_test.exs +++ b/test/components/drawer_test.exs @@ -18,7 +18,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -40,7 +40,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -62,7 +62,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -84,7 +84,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -106,7 +106,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -128,7 +128,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -150,7 +150,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -172,7 +172,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -194,7 +194,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -216,7 +216,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -238,7 +238,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue @@ -263,7 +263,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Page content
Content
+
Page content
Content
""" |> format_html() rescue @@ -288,7 +288,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Page content
Content
+
Page content
Content
""" |> format_html() rescue @@ -310,7 +310,7 @@ defmodule PrimerLive.TestComponents.DrawerTest do """) |> format_html() == """ -
Content
+
Content
""" |> format_html() rescue diff --git a/test/components/dropdown_test.exs b/test/components/dropdown_test.exs index ea3dba18..9055be5e 100644 --- a/test/components/dropdown_test.exs +++ b/test/components/dropdown_test.exs @@ -19,7 +19,7 @@ defmodule PrimerLive.TestComponents.DropdownTest do """) |> format_html() == """ - + """ |> format_html() rescue @@ -42,7 +42,7 @@ defmodule PrimerLive.TestComponents.DropdownTest do """) |> format_html() == """ - + """ |> format_html() rescue @@ -71,7 +71,7 @@ defmodule PrimerLive.TestComponents.DropdownTest do """) |> format_html() == """ - + """ |> format_html() rescue @@ -98,7 +98,7 @@ defmodule PrimerLive.TestComponents.DropdownTest do """) |> format_html() == """ - + """ |> format_html() rescue @@ -125,7 +125,7 @@ defmodule PrimerLive.TestComponents.DropdownTest do """) |> format_html() == """ - + """ |> format_html() rescue @@ -151,7 +151,7 @@ defmodule PrimerLive.TestComponents.DropdownTest do """) |> format_html() == """ - + """ |> format_html() rescue @@ -182,7 +182,7 @@ defmodule PrimerLive.TestComponents.DropdownTest do """) |> format_html() == """ - + """ |> format_html() rescue @@ -227,7 +227,7 @@ defmodule PrimerLive.TestComponents.DropdownTest do """) |> format_html() == """ - + """ |> format_html() rescue @@ -253,7 +253,7 @@ defmodule PrimerLive.TestComponents.DropdownTest do """) |> format_html() == """ - + """ |> format_html() rescue diff --git a/test/components/select_menu_test.exs b/test/components/select_menu_test.exs index b1da36ac..aecda9de 100644 --- a/test/components/select_menu_test.exs +++ b/test/components/select_menu_test.exs @@ -35,7 +35,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
+
""" |> format_html() rescue @@ -61,7 +61,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
Link
+
Link
""" |> format_html() rescue @@ -94,7 +94,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
STRIPPED_SVG_PATHSLink
+
STRIPPED_SVG_PATHSLink
""" |> format_html() rescue @@ -121,7 +121,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -

Title

+

Title

""" |> format_html() rescue @@ -148,7 +148,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
+
""" |> format_html() rescue @@ -179,7 +179,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
+
""" |> format_html() rescue @@ -211,7 +211,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
+
""" |> format_html() rescue @@ -232,7 +232,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
STRIPPED_SVG_PATHS
+
STRIPPED_SVG_PATHS
""" |> format_html() rescue @@ -253,7 +253,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
Blankslate content
+
Blankslate content
""" |> format_html() rescue @@ -274,7 +274,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
Message
+
Message
""" |> format_html() rescue @@ -300,7 +300,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
+
""" |> format_html() rescue @@ -326,7 +326,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
+
""" |> format_html() rescue @@ -392,7 +392,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
+
""" |> format_html() rescue @@ -418,7 +418,7 @@ defmodule PrimerLive.TestComponents.SelectMenuTest do """) |> format_html() == """ -
+
""" |> format_html() rescue diff --git a/test/components/subnav_test.exs b/test/components/subnav_test.exs index 96471d54..82644901 100644 --- a/test/components/subnav_test.exs +++ b/test/components/subnav_test.exs @@ -110,7 +110,7 @@ defmodule PrimerLive.Components.SubnavTest do """) |> format_html() == """ - + """ |> format_html() rescue @@ -150,7 +150,7 @@ defmodule PrimerLive.Components.SubnavTest do """) |> format_html() == """ - + """ |> format_html() rescue