Improve InputPicker tests - include AlpineJS assertions

pull/2859/head
Adam Rutkowski 4 years ago
parent 4c8600b28d
commit b3dca59cf6

@ -24,47 +24,44 @@ defmodule PlausibleWeb.Live.FunnelSettings.InputPicker do
def render(assigns) do
~H"""
<div>
<div
class="mb-3"
x-data={"window.suggestionsDropdown('#{@id}')"}
x-on:keydown.arrow-up="focusPrev"
x-on:keydown.arrow-down="focusNext"
x-on:keydown.enter="select()"
>
<div class="relative w-full">
<div
@click.away="close"
class="pl-2 pr-8 py-1 w-full dark:bg-gray-900 dark:text-gray-300 rounded-md shadow-sm border border-gray-300 dark:border-gray-700 focus-within:border-indigo-500 focus-within:ring-1 focus-within:ring-indigo-500"
>
<input
type="text"
autocomplete="off"
id={@id}
name={"display-#{@id}"}
placeholder={@placeholder}
x-on:focus="open"
phx-change="search"
phx-target={@myself}
value={@display_value}
class="border-none py-1 px-1 p-0 w-full inline-block rounded-md focus:outline-none focus:ring-0 text-sm"
style="background-color: inherit;"
/>
<.dropdown_anchor id={@id} />
<input
type="hidden"
name={@submit_name}
value={@submit_value}
phx-target={@myself}
id={"submit-#{@id}"}
/>
</div>
<div
id={"input-picker-main-#{@id}"}
class="mb-3"
x-data={"window.suggestionsDropdown('#{@id}')"}
x-on:keydown.arrow-up="focusPrev"
x-on:keydown.arrow-down="focusNext"
x-on:keydown.enter="select()"
@click.away="close"
>
<div class="relative w-full">
<div class="pl-2 pr-8 py-1 w-full dark:bg-gray-900 dark:text-gray-300 rounded-md shadow-sm border border-gray-300 dark:border-gray-700 focus-within:border-indigo-500 focus-within:ring-1 focus-within:ring-indigo-500">
<input
type="text"
autocomplete="off"
id={@id}
name={"display-#{@id}"}
placeholder={@placeholder}
x-on:focus="open"
phx-change="search"
phx-target={@myself}
value={@display_value}
class="border-none py-1 px-1 p-0 w-full inline-block rounded-md focus:outline-none focus:ring-0 text-sm"
style="background-color: inherit;"
/>
<.dropdown_anchor id={@id} />
<input
type="hidden"
name={@submit_name}
value={@submit_value}
phx-target={@myself}
id={"submit-#{@id}"}
/>
</div>
<.dropdown ref={@id} options={@options} suggestions={@suggestions} target={@myself} />
</div>
<.dropdown ref={@id} options={@options} suggestions={@suggestions} target={@myself} />
</div>
"""
end

@ -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"])

@ -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

Loading…
Cancel
Save