Improve records list tests

- Remove scaffolded tests (precursor to removing functionality)
- Add tests for paginated list
This commit is contained in:
Claudio Ortolina
2024-09-23 14:52:37 +01:00
parent e666b247e1
commit 7ebc12c4ac
2 changed files with 64 additions and 110 deletions
@@ -11,7 +11,10 @@ defmodule MusicLibraryWeb.Pagination do
assigns = assign(assigns, :page_links, page_links) assigns = assign(assigns, :page_links, page_links)
~H""" ~H"""
<div class="flex items-center justify-between border-t border-gray-200 bg-white px-4 py-3 sm:px-6"> <div
id="pagination"
class="flex items-center justify-between border-t border-gray-200 bg-white px-4 py-3 sm:px-6"
>
<%!-- Only on smallest viewport --%> <%!-- Only on smallest viewport --%>
<div class="flex flex-1 justify-between sm:hidden"> <div class="flex flex-1 justify-between sm:hidden">
<a <a
+60 -109
View File
@@ -4,131 +4,82 @@ defmodule MusicLibraryWeb.RecordLiveTest do
import Phoenix.LiveViewTest import Phoenix.LiveViewTest
import MusicLibrary.RecordsFixtures import MusicLibrary.RecordsFixtures
@create_attrs %{ defp create_records(_) do
type: :album, records = Enum.map(1..30, fn _ -> record_fixture() end)
title: "some title", %{records: records}
image_url: "some image url",
year: 42,
musicbrainz_id: "7488a646-e31f-11e4-aace-600308960662",
genres: ["option1", "option2"]
}
@update_attrs %{
type: :ep,
title: "some updated title",
image_url: "some updated image url",
year: 43,
musicbrainz_id: "7488a646-e31f-11e4-aace-600308960668",
genres: ["option1"]
}
@invalid_attrs %{
type: nil,
title: nil,
image_url: nil,
year: nil,
musicbrainz_id: nil,
genres: []
}
defp create_record(_) do
record = record_fixture()
%{record: record}
end end
describe "Index" do defp escape(string) do
setup [:create_record] string
|> Phoenix.HTML.html_escape()
|> Phoenix.HTML.safe_to_string()
end
test "lists all records", %{conn: conn, record: record} do describe "Paginated list of records" do
setup [:create_records]
test "lists all records within default pagination params", %{conn: conn, records: records} do
{:ok, _index_live, html} = live(conn, ~p"/records") {:ok, _index_live, html} = live(conn, ~p"/records")
assert html =~ "Listing Records" assert html =~ "Listing Records"
assert html =~ record.title
{present, absent} =
Enum.split_with(records, fn record ->
html =~ record.id
end)
assert length(present) == 20
assert length(absent) == 10
for record <- present do
assert html =~ escape(record.title)
assert html =~ to_string(record.year)
for artist <- record.artists do
assert html =~ escape(artist["name"])
end
end
end end
test "saves new record", %{conn: conn} do test "paginates records", %{conn: conn, records: records} do
{:ok, index_live, _html} = live(conn, ~p"/records") {:ok, page_2_live, page_2_html} = live(conn, ~p"/records?page=2&page_size=5")
assert index_live |> element("a", "New Record") |> render_click() =~ {page_2_present, page_2_absent} =
"New Record" Enum.split_with(records, fn record ->
page_2_html =~ record.id
end)
assert_patch(index_live, ~p"/records/new") assert length(page_2_present) == 5
assert length(page_2_absent) == 25
assert index_live page_2_pagination = page_2_live |> with_target("#pagination")
|> form("#record-form", record: @invalid_attrs) refute has_element?(page_2_pagination, "a", "2")
|> render_change() =~ "can&#39;t be blank" assert has_element?(page_2_pagination, "a", "1")
assert has_element?(page_2_pagination, "a", "3")
assert has_element?(page_2_pagination, "a", "4")
assert has_element?(page_2_pagination, "a", "5")
assert index_live {:ok, page_3_live, page_3_html} = live(conn, ~p"/records?page=3&page_size=5")
|> form("#record-form", record: @create_attrs)
|> render_submit()
assert_patch(index_live, ~p"/records") {page_3_present, page_3_absent} =
Enum.split_with(records, fn record ->
page_3_html =~ record.id
end)
html = render(index_live) assert length(page_3_present) == 5
assert html =~ "Record created successfully" assert length(page_3_absent) == 25
assert html =~ "some title"
end
test "updates record in listing", %{conn: conn, record: record} do page_3_pagination = page_3_live |> with_target("#pagination")
{:ok, index_live, _html} = live(conn, ~p"/records") refute has_element?(page_3_pagination, "a", "3")
assert has_element?(page_3_pagination, "a", "1")
assert has_element?(page_3_pagination, "a", "2")
assert has_element?(page_3_pagination, "a", "4")
assert has_element?(page_3_pagination, "a", "5")
assert index_live |> element("#records-#{record.id} a", "Edit") |> render_click() =~ # All records in page 3 were not present in page 2
"Edit Record" assert page_3_present -- page_2_absent == []
# All records in page 2 are not present in page 3
assert_patch(index_live, ~p"/records/#{record}/edit") assert page_2_present -- page_3_absent == []
assert index_live
|> form("#record-form", record: @invalid_attrs)
|> render_change() =~ "can&#39;t be blank"
assert index_live
|> form("#record-form", record: @update_attrs)
|> render_submit()
assert_patch(index_live, ~p"/records")
html = render(index_live)
assert html =~ "Record updated successfully"
assert html =~ "some updated title"
end
test "deletes record in listing", %{conn: conn, record: record} do
{:ok, index_live, _html} = live(conn, ~p"/records")
assert index_live |> element("#records-#{record.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#records-#{record.id}")
end
end
describe "Show" do
setup [:create_record]
test "displays record", %{conn: conn, record: record} do
{:ok, _show_live, html} = live(conn, ~p"/records/#{record}")
assert html =~ "Show Record"
assert html =~ record.title
end
test "updates record within modal", %{conn: conn, record: record} do
{:ok, show_live, _html} = live(conn, ~p"/records/#{record}")
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Record"
assert_patch(show_live, ~p"/records/#{record}/show/edit")
assert show_live
|> form("#record-form", record: @invalid_attrs)
|> render_change() =~ "can&#39;t be blank"
assert show_live
|> form("#record-form", record: @update_attrs)
|> render_submit()
assert_patch(show_live, ~p"/records/#{record}")
html = render(show_live)
assert html =~ "Record updated successfully"
assert html =~ "some updated title"
end end
end end
end end