Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinBoers committed Sep 2, 2023
1 parent 9db20da commit 325146a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/petal_components/pagination.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule PetalComponents.Pagination do
alias PetalComponents.Link

attr :path, :string, default: "/:page", doc: "page path"
attr :class, :string, default: "", doc: "Parent div CSS class"
attr :class, :string, default: "", doc: "parent div CSS class"

attr :link_type, :string,
default: "a",
Expand All @@ -21,7 +21,7 @@ defmodule PetalComponents.Pagination do
doc:
"whether to use `phx-click` events instead of linking. Enabling this will disable `link_type` and `path`."

attr :target, :any, default: nil, doc: "the LiveView/LiveComponent to send the event to. Example: `@myself`."
attr :target, :any, default: nil, doc: "the LiveView/LiveComponent to send the event to. Example: `@myself`. Will be ignored if `event` is not enabled."
attr :total_pages, :integer, default: nil, doc: "sets a total page count"
attr :current_page, :integer, default: nil, doc: "sets the current page"
attr :sibling_count, :integer, default: 1, doc: "sets a sibling count"
Expand All @@ -42,7 +42,7 @@ defmodule PetalComponents.Pagination do
<div>
<Link.a
phx-click={if @event, do: "goto-page"}
phx-target={@target}
phx-target={if @event, do: @target}
phx-value-page={item.number}
link_type={if @event, do: "button", else: @link_type}
to={if not @event, do: get_path(@path, item.number, @current_page)}
Expand All @@ -60,7 +60,7 @@ defmodule PetalComponents.Pagination do
<% else %>
<Link.a
phx-click={if @event, do: "goto-page"}
phx-target={@target}
phx-target={if @event, do: @target}
phx-value-page={item.number}
link_type={if @event, do: "button", else: @link_type}
to={if not @event, do: get_path(@path, item.number, @current_page)}
Expand All @@ -84,7 +84,7 @@ defmodule PetalComponents.Pagination do
<div>
<Link.a
phx-click={if @event, do: "goto-page"}
phx-target={@target}
phx-target={if @event, do: @target}
phx-value-page={item.number}
link_type={if @event, do: "button", else: @link_type}
to={if not @event, do: get_path(@path, item.number, @current_page)}
Expand Down
23 changes: 23 additions & 0 deletions test/petal/pagination_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -495,5 +495,28 @@ defmodule PetalComponents.PaginationTest do
assert html =~ "goto-page"
assert html =~ "phx-value-page=\"2\""
assert html =~ "phx-value-page=\"3\""
refute html =~ "phx-target"
end

test "target option with event option will generate 'phx-target' attribute" do
assigns = %{}

html =
rendered_to_string(~H"""
<.pagination event={true} target={"some-component-id"} total_pages={3} current_page={1} />
""")

assert html =~ "phx-target"
end

test "target option without event option won't generate 'phx-target' attribute" do
assigns = %{}

html =
rendered_to_string(~H"""
<.pagination target={"some-component-id"} total_pages={3} current_page={1} />
""")

refute html =~ "phx-target"
end
end

0 comments on commit 325146a

Please sign in to comment.