Wrap Phoenix.HTML.Link module

This commit is contained in:
Mitchell Hanberg 2019-06-29 18:14:05 -04:00
parent 6b55fc7665
commit 1cb522d440
3 changed files with 156 additions and 0 deletions

View file

@ -8,6 +8,7 @@ defmodule Dsl do
import unquote(__MODULE__)
import Dsl.Tags
import Dsl.Form
import Dsl.Link
end
end

30
lib/dsl/link.ex Normal file
View file

@ -0,0 +1,30 @@
defmodule Dsl.Link do
alias Phoenix.HTML
alias Dsl.Utils
@moduledoc """
This modules wraps all of the functions from the `Phoenix.HTML.Link` module to make them compatible with with Dsl.
"""
@doc """
Please see `Phoenix.HTML.Link.link/2` for details.
"""
defmacro phx_link(content_or_opts, opts_or_block) do
quote do
{:safe, link} = HTML.Link.link(unquote_splicing([content_or_opts, opts_or_block]))
Utils.put_buffer(var!(buff, Dsl.Tags), link)
end
end
@doc """
Please see `Phoenix.HTML.Link.button/2` for details.
"""
defmacro phx_button(content_or_opts, opts_or_block) do
quote do
{:safe, link} = HTML.Link.button(unquote_splicing([content_or_opts, opts_or_block]))
Utils.put_buffer(var!(buff, Dsl.Tags), link)
end
end
end

125
test/dsl/link_test.exs Normal file
View file

@ -0,0 +1,125 @@
defmodule Dsl.LinkTest do
use ExUnit.Case, async: true
use Dsl
describe "phx_link" do
test "emits a link" do
{:safe, actual} =
htm do
phx_link("hi", to: "/hello")
end
assert actual =~ ~s{<a}
assert actual =~ ~s{href="/hello"}
assert actual =~ ~s{hi}
end
test "emits a link when passed block" do
{:safe, actual} =
htm do
phx_link to: "/hello" do
"hi"
end
end
assert actual =~ ~s{<a}
assert actual =~ ~s{href="/hello"}
assert actual =~ ~s{hi}
end
test "emits a link with additional html attributes" do
{:safe, actual} =
htm do
phx_link("hi",
to: "/hello",
class: "phoenix",
id: "legendary",
data: [confirm: "Really?"],
method: :delete
)
end
assert actual =~ ~s{<a}
assert actual =~ ~s{href="/hello"}
assert actual =~ ~s{class="phoenix"}
assert actual =~ ~s{id="legendary"}
assert actual =~ ~s{data-confirm="Really?"}
assert actual =~ ~s{hi}
end
test "emits a link with a non GET method" do
{:safe, actual} =
htm do
phx_link("hi",
to: "/hello",
method: :delete
)
end
assert actual =~ ~s{<a}
assert actual =~ ~s{data-csrf="}
assert actual =~ ~s{data-method="delete"}
assert actual =~ ~s{data-to="/hello"}
assert actual =~ ~s{hi}
end
end
describe "phx_button" do
test "emits a button" do
{:safe, actual} =
htm do
phx_button("hi", to: "/hello")
end
assert actual =~ ~s{<button}
assert actual =~ ~s{hi}
end
test "emits a button when passed block" do
{:safe, actual} =
htm do
phx_button to: "/hello" do
"hi"
end
end
assert actual =~ ~s{<button}
assert actual =~ ~s{hi}
end
test "emits a button with additional html attributes" do
{:safe, actual} =
htm do
phx_button("hi",
to: "/hello",
class: "phoenix",
id: "legendary",
data: [confirm: "Really?"],
method: :delete
)
end
assert actual =~ ~s{<button}
assert actual =~ ~s{class="phoenix"}
assert actual =~ ~s{id="legendary"}
assert actual =~ ~s{data-confirm="Really?"}
assert actual =~ ~s{hi}
end
test "emits a button with a non GET method" do
{:safe, actual} =
htm do
phx_button("hi",
to: "/hello",
method: :delete
)
end
assert actual =~ ~s{<button}
assert actual =~ ~s{data-csrf="}
assert actual =~ ~s{data-method="delete"}
assert actual =~ ~s{data-to="/hello"}
assert actual =~ ~s{hi}
end
end
end