2017-11-17 18:36:37 +00:00
|
|
|
defmodule AutoLinker.BuilderTest do
|
2019-02-08 10:56:05 +00:00
|
|
|
use ExUnit.Case, async: true
|
2017-11-17 18:36:37 +00:00
|
|
|
doctest AutoLinker.Builder
|
|
|
|
|
|
|
|
import AutoLinker.Builder
|
|
|
|
|
2019-02-27 05:57:39 +00:00
|
|
|
test "create_link/2" do
|
|
|
|
expected =
|
|
|
|
"<a href=\"text\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">text</a>"
|
|
|
|
|
|
|
|
assert create_link("text", %{href: "url"}) == expected
|
|
|
|
|
|
|
|
expected = "<a href=\"text\" class=\"auto-linker\" target=\"_blank\">text</a>"
|
|
|
|
assert create_link("text", %{href: "url", rel: nil}) == expected
|
|
|
|
|
|
|
|
expected = "<a href=\"text\" class=\"auto-linker\" target=\"_blank\" rel=\"me\">text</a>"
|
|
|
|
assert create_link("text", %{href: "url", rel: "me"}) == expected
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create_markdown_links/2" do
|
|
|
|
expected =
|
|
|
|
"<a href='url' class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">text</a>"
|
|
|
|
|
|
|
|
assert create_markdown_links("[text](url)", %{}) == expected
|
|
|
|
end
|
|
|
|
|
|
|
|
test "format_hashtag/3" do
|
|
|
|
expected = "<a href=\"/t/girls\">#girls</a>"
|
|
|
|
assert format_hashtag(%{href: "/t/girls"}, "girls", nil) == expected
|
|
|
|
end
|
|
|
|
|
|
|
|
test "format_email/3" do
|
|
|
|
expected = "<a href=\"mailto:user@example.org\">mailto:user@example.org</a>"
|
|
|
|
|
|
|
|
assert format_email(%{href: "mailto:user@example.org"}, "mailto:user@example.org", nil) ==
|
|
|
|
expected
|
|
|
|
end
|
|
|
|
|
|
|
|
test "format_mention/3" do
|
|
|
|
expected = "<a href=\"url\">@user@host</a>"
|
|
|
|
format_mention(%{href: "url"}, "user@host", nil) == expected
|
|
|
|
end
|
|
|
|
|
2017-11-17 18:36:37 +00:00
|
|
|
describe "create_phone_link" do
|
|
|
|
test "finishes" do
|
|
|
|
assert create_phone_link([], "", []) == ""
|
|
|
|
end
|
2019-02-05 11:22:51 +00:00
|
|
|
|
2017-11-17 18:36:37 +00:00
|
|
|
test "handles one link" do
|
|
|
|
phrase = "my exten is x888. Call me."
|
2019-02-05 11:22:51 +00:00
|
|
|
|
|
|
|
expected =
|
|
|
|
~s'my exten is <a href="#" class="phone-number" data-phone="888">x888</a>. Call me.'
|
|
|
|
|
2017-11-17 18:36:37 +00:00
|
|
|
assert create_phone_link([["x888", ""]], phrase, []) == expected
|
|
|
|
end
|
2019-02-05 11:22:51 +00:00
|
|
|
|
2017-11-17 18:36:37 +00:00
|
|
|
test "handles multiple links" do
|
|
|
|
phrase = "555.555.5555 or (555) 888-8888"
|
2019-02-05 11:22:51 +00:00
|
|
|
|
|
|
|
expected =
|
|
|
|
~s'<a href="#" class="phone-number" data-phone="5555555555">555.555.5555</a> or ' <>
|
|
|
|
~s'<a href="#" class="phone-number" data-phone="5558888888">(555) 888-8888</a>'
|
|
|
|
|
2017-11-17 18:36:37 +00:00
|
|
|
assert create_phone_link([["555.555.5555", ""], ["(555) 888-8888"]], phrase, []) == expected
|
|
|
|
end
|
|
|
|
end
|
2019-02-27 05:57:39 +00:00
|
|
|
|
|
|
|
test "create_mention_link/3" do
|
|
|
|
expected =
|
|
|
|
"<a href=\"/u/navi\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">@navi</a>"
|
|
|
|
|
|
|
|
assert create_mention_link("@navi", "hello @navi", %{mention_prefix: "/u/"}) == expected
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create_email_link/3" do
|
|
|
|
expected = "<a href=\"mailto:user@example.org\" class=\"auto-linker\">user@example.org</a>"
|
|
|
|
assert create_email_link("user@example.org", %{}) == expected
|
|
|
|
assert create_email_link("user@example.org", %{href: "mailto:user@example.org"}) == expected
|
|
|
|
end
|
2017-11-17 18:36:37 +00:00
|
|
|
end
|