Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pattern matching instead of elem(1) #276

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 30 additions & 34 deletions test/dpul_collections_web/live/search_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
test "GET /search", %{conn: conn} do
conn = get(conn, ~p"/search")

document =
{:ok, document} =
html_response(conn, 200)
|> Floki.parse_document()
|> elem(1)

assert document
|> Floki.find(~s{a[href="/i/document1/item/1"]})
Expand All @@ -31,8 +30,8 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
test "GET /search with blank q parameter", %{conn: conn} do
conn = get(conn, ~p"/search?q=")

document =
html_response(conn, 200) |> Floki.parse_document() |> elem(1)
{:ok, document} =
html_response(conn, 200) |> Floki.parse_document()

assert document
|> Floki.find(~s{a[href="/i/document1/item/1"]})
Expand Down Expand Up @@ -70,12 +69,11 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
test "searching filters results", %{conn: conn} do
{:ok, view, _html} = live(conn, "/search?")

document =
{:ok, document} =
view
|> element("#search-form")
|> render_submit(%{"q" => "Document-2"})
|> Floki.parse_document()
|> elem(1)

assert document
|> Floki.find(~s{a[href="/i/document2/item/2"]})
Expand All @@ -89,11 +87,10 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
test "items can be sorted by date, ascending and descending", %{conn: conn} do
{:ok, view, _html} = live(conn, "/search")

document =
{:ok, document} =
view
|> render_click("sort", %{"sort-by" => "date_asc"})
|> Floki.parse_document()
|> elem(1)

assert document
|> Floki.find(~s{a[href="/i/document100/item/100"]})
Expand All @@ -103,11 +100,10 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
|> Floki.find(~s{a[href="/i/document1/item/1"]})
|> Enum.empty?()

document =
{:ok, document} =
view
|> render_click("sort", %{"sort-by" => "date_desc"})
|> Floki.parse_document()
|> elem(1)

assert document
|> Floki.find(~s{a[href="/i/document1/item/1"]})
Expand All @@ -121,12 +117,11 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
test "items can be filtered by date range", %{conn: conn} do
{:ok, view, _html} = live(conn, "/search")

document =
{:ok, document} =
view
|> element("#date-filter")
|> render_submit(%{"date-from" => "1925", "date-to" => "1926"})
|> Floki.parse_document()
|> elem(1)

assert document
|> Floki.find(~s{a[href="/i/document99/item/99"]})
Expand All @@ -151,25 +146,29 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
{:ok, view, _html} = live(conn, ~p"/search?page=5")
assert(view |> element(".paginator > a.active", ~r(5)) |> has_element?())

assert view
|> element("#paginator-previous")
|> render_click()
|> follow_redirect(conn)
|> elem(2)
|> Floki.parse_document()
|> elem(1)
{:ok, document} =
view
|> element("#paginator-previous")
|> render_click()
|> follow_redirect(conn)
|> elem(2)
|> Floki.parse_document()

assert document
|> Floki.find(~s{a[href="/i/document40/item/40"]})
|> Enum.any?()

{:ok, view, _html} = live(conn, ~p"/search?page=4")

assert view
|> element("#paginator-next")
|> render_click()
|> follow_redirect(conn)
|> elem(2)
|> Floki.parse_document()
|> elem(1)
{:ok, document} =
view
|> element("#paginator-next")
|> render_click()
|> follow_redirect(conn)
|> elem(2)
|> Floki.parse_document()

assert document
|> Floki.find(~s{a[href="/i/document50/item/50"]})
|> Enum.any?()

Expand All @@ -184,8 +183,8 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
# Check that changing the sort order resets the paginator
{:ok, view, _html} = live(conn, ~p"/search?page=10")

document =
view |> render() |> Floki.parse_document() |> elem(1)
{:ok, document} =
view |> render() |> Floki.parse_document()

assert document
|> Floki.find("a[phx-value-page=9]")
Expand All @@ -195,11 +194,10 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
|> Floki.find("a[phx-value-page=2]")
|> Enum.empty?()

document =
{:ok, document} =
view
|> render_click("sort", %{"sort-by" => "date_asc"})
|> Floki.parse_document()
|> elem(1)

assert document
|> Floki.find("a[phx-value-page=2]")
Expand All @@ -212,12 +210,11 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
# Check that changing search query resets the paginator
{:ok, view, _html} = live(conn, ~p"/search?page=10")

document =
{:ok, document} =
view
|> element("#search-form")
|> render_submit(%{"q" => "Document*"})
|> Floki.parse_document()
|> elem(1)

assert document
|> Floki.find("a[phx-value-page=2]")
Expand All @@ -230,12 +227,11 @@ defmodule DpulCollectionsWeb.SearchLiveTest do
# Check that updating the date query resets the paginator
{:ok, view, _html} = live(conn, ~p"/search?page=10")

document =
{:ok, document} =
view
|> element("#search-form")
|> render_submit(%{"date-from" => "1900", "date-to" => "2025"})
|> Floki.parse_document()
|> elem(1)

assert document
|> Floki.find("a[phx-value-page=2]")
Expand Down