diff --git a/lib/plausible_web/live/funnel_settings/input_picker.ex b/lib/plausible_web/live/funnel_settings/input_picker.ex index 7a97b219b..413deb77b 100644 --- a/lib/plausible_web/live/funnel_settings/input_picker.ex +++ b/lib/plausible_web/live/funnel_settings/input_picker.ex @@ -24,47 +24,44 @@ defmodule PlausibleWeb.Live.FunnelSettings.InputPicker do def render(assigns) do ~H""" -
-
-
-
- - - <.dropdown_anchor id={@id} /> - - -
+
+
+
+ + + <.dropdown_anchor id={@id} /> + +
- - <.dropdown ref={@id} options={@options} suggestions={@suggestions} target={@myself} />
+ + <.dropdown ref={@id} options={@options} suggestions={@suggestions} target={@myself} />
""" end diff --git a/test/plausible_web/live/funnel_settings/funnel_settings/input_picker_test.exs b/test/plausible_web/live/funnel_settings/funnel_settings/input_picker_test.exs index fe0763de9..4a62fad36 100644 --- a/test/plausible_web/live/funnel_settings/funnel_settings/input_picker_test.exs +++ b/test/plausible_web/live/funnel_settings/funnel_settings/input_picker_test.exs @@ -11,7 +11,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.InputPickerTest do ~s/#{@ul} li#dropdown-test-component-option-#{idx - 1}/ end - describe "static" do + describe "static rendering" do test "renders suggestions" do assert doc = render_sample_component(new_options(10)) @@ -35,10 +35,73 @@ defmodule PlausibleWeb.Live.FunnelSettings.InputPickerTest do refute element_exists?(doc, suggestion_li(16)) refute element_exists?(doc, suggestion_li(17)) + + assert Floki.text(doc) =~ "Max results reached" + end + + test "Alpine.js: renders attrs focusing suggestion elements" do + assert doc = render_sample_component(new_options(10)) + li1 = doc |> find(suggestion_li(1)) |> List.first() + li2 = doc |> find(suggestion_li(2)) |> List.first() + + assert text_of_attr(li1, "@mouseenter") == "setFocus(0)" + assert text_of_attr(li2, "@mouseenter") == "setFocus(1)" + + assert text_of_attr(li1, "x-bind:class") =~ "focus === 0" + assert text_of_attr(li2, "x-bind:class") =~ "focus === 1" + end + + test "Alpine.js: component refers to window.suggestionsDropdown" do + assert new_options(2) + |> render_sample_component() + |> find("div#input-picker-main-test-component") + |> text_of_attr("x-data") =~ "window.suggestionsDropdown('test-component')" + end + + test "Alpine.js: component sets up keyboard navigation" do + main = + new_options(2) + |> render_sample_component() + |> find("div#input-picker-main-test-component") + + assert text_of_attr(main, "x-on:keydown.arrow-up") == "focusPrev" + assert text_of_attr(main, "x-on:keydown.arrow-down") == "focusNext" + assert text_of_attr(main, "x-on:keydown.enter") == "select()" + end + + test "Alpine.js: component sets up close on click-away" do + assert new_options(2) + |> render_sample_component() + |> find("div#input-picker-main-test-component") + |> text_of_attr("@click.away") == "close" + end + + test "Alpine.js: component sets up open on focusing the display input" do + assert new_options(2) + |> render_sample_component() + |> find("input#test-component") + |> text_of_attr("x-on:focus") == "open" + end + + test "Alpine.js: dropdown is annotated and shows when isOpen is true" do + dropdown = + new_options(2) + |> render_sample_component() + |> find("#dropdown-test-component") + + assert text_of_attr(dropdown, "x-show") == "isOpen" + assert text_of_attr(dropdown, "x-ref") == "suggestions" + end + + test "Dropdown shows a notice when no suggestions exist" do + doc = render_sample_component([]) + + assert text_of_element(doc, "#dropdown-test-component") == + "No matches found. Try searching for something different." end end - describe "autosuggest" do + describe "autosuggest algorithm" do test "favours exact match" do options = fake_options(["yellow", "hello", "cruel hello world"]) diff --git a/test/support/html.ex b/test/support/html.ex index 6116a5245..f9be38c7c 100644 --- a/test/support/html.ex +++ b/test/support/html.ex @@ -26,4 +26,11 @@ defmodule Plausible.Test.Support.HTML do |> Floki.text() |> String.trim() end + + def text_of_attr(element, attr) do + element + |> Floki.attribute(attr) + |> Floki.text() + |> String.trim() + end end