defmodule TempleTest do
use ExUnit.Case, async: true
use Temple
use Temple.Support.Utils
test "renders an attribute on a div passed as a variable" do
result =
temple do
div class: "hello" do
div class: "hi"
end
end
assert result == ~s{
}
end
test "renders void element" do
result =
temple do
input name: "password"
end
assert result == ~s{}
end
test "renders a text node from the text keyword with siblings" do
result =
temple do
div class: "hello" do
"hi"
"foo"
end
end
assert result == ~s{
hifoo
}
end
test "renders a variable text node as eex" do
result =
temple do
div class: "hello" do
foo
end
end
assert result == ~s{
<%= foo %>
}
end
test "renders an assign text node as eex" do
result =
temple do
div class: "hello" do
@foo
end
end
assert result == ~s{
<%= @foo %>
}
end
test "renders a match expression" do
result =
temple do
x = 420
div do
"blaze it"
end
end
assert result == ~s{<% x = 420 %>
blaze it
}
end
test "renders a non-match expression" do
result =
temple do
IO.inspect(:foo)
div do
"bar"
end
end
assert result == ~s{<%= IO.inspect(:foo) %>
bar
}
end
test "renders an expression in attr as eex" do
result =
temple do
div class: foo <> " bar"
end
assert result == ~s{
">
}
end
test "renders an attribute on a div passed as a variable as eex" do
result =
temple do
div class: Enum.map([:one, :two], fn x -> x end) do
div class: "hi"
end
end
assert result ==
~s{
}
end
test "renders a for comprehension as eex" do
result =
temple do
for x <- 1..5 do
div class: "hi"
end
end
assert result == ~s{<%= for(x <- 1..5) do %><% end %>}
end
test "renders an if expression as eex" do
result =
temple do
if true == false do
div class: "hi"
end
end
assert result == ~s{<%= if(true == false) do %><% end %>}
end
test "renders an if/else expression as eex" do
result =
temple do
if true == false do
div class: "hi"
else
div class: "haha"
end
end
assert result ==
~s{<%= if(true == false) do %><% else %><% end %>}
end
test "renders an unless expression as eex" do
result =
temple do
unless true == false do
div class: "hi"
end
end
assert result == ~s{<%= unless(true == false) do %><% end %>}
end
test "renders multiline anonymous function with 1 arg before the function" do
result =
temple do
form_for Routes.user_path(@conn, :create), fn f ->
"Name: "
text_input f, :name
end
end
assert result ==
~s{<%= form_for Routes.user_path(@conn, :create), fn f -> %>Name: <%= text_input(f, :name) %><% end %>}
end
test "renders multiline anonymous functions with 2 args before the function" do
result =
temple do
form_for @changeset, Routes.user_path(@conn, :create), fn f ->
"Name: "
text_input f, :name
end
end
assert result ==
~s{<%= form_for @changeset, Routes.user_path(@conn, :create), fn f -> %>Name: <%= text_input(f, :name) %><% end %>}
end
test "renders multiline anonymous functions with complex nested children" do
result =
temple do
form_for @changeset, Routes.user_path(@conn, :create), fn f ->
div do
"Name: "
text_input f, :name
end
end
end
assert result ==
~s{<%= form_for @changeset, Routes.user_path(@conn, :create), fn f -> %>
Name: <%= text_input(f, :name) %>
<% end %>}
end
test "renders multiline anonymous function with 3 arg before the function" do
result =
temple do
form_for @changeset, Routes.user_path(@conn, :create), [foo: :bar], fn f ->
"Name: "
text_input f, :name
end
end
assert result ==
~s{<%= form_for @changeset, Routes.user_path(@conn, :create), [foo: :bar], fn f -> %>Name: <%= text_input(f, :name) %><% end %>}
end
test "renders multiline anonymous function with 1 arg before the function and 1 arg after" do
result =
temple do
form_for @changeset,
fn f ->
"Name: "
text_input f, :name
end,
foo: :bar
end
assert result ==
~s{<%= form_for @changeset, fn f -> %>Name: <%= text_input(f, :name) %><% end, [foo: :bar] %>}
end
test "tags prefixed with Temple. should be interpreted as temple tags" do
result =
temple do
div do
Temple.span do
"bob"
end
end
end
assert result == ~s{
bob
}
end
test "can pass do as an arg instead of a block" do
result =
temple do
div class: "font-bold" do
"Hello, world"
end
div class: "font-bold", do: "Hello, world"
div do: "Hello, world"
end
assert result ==
~s{
Hello, world
Hello, world
Hello, world
}
end
test "passing 'compact: true' will not insert new lines" do
import Temple.Support.Utils, only: []
import Kernel
result =
temple do
p compact: true do
"Bob"
end
p compact: true do
foo
end
end
assert result == ~s{
Bob
\n
<%= foo %>
}
end
test "inlines function components" do
result =
temple do
div class: "font-bold" do
"Hello, world"
end
component do
"I'm a component!"
end
end
assert result ==
~s{
Hello, world
I'm a component!
}
end
test "function components can accept local assigns" do
result =
temple do
div class: "font-bold" do
"Hello, world"
end
component2 class: "bg-red" do
"I'm a component!"
end
end
assert result ==
~s{
Hello, world
I'm a component!
}
end
test "function components can accept local assigns that are variables" do
result =
temple do
div class: "font-bold" do
"Hello, world"
end
class = "bg-red"
component2 class: class do
"I'm a component!"
end
end
assert result ==
~s{
Hello, world
<% class = "bg-red" %>
I'm a component!
}
end
test "function components can use other components" do
result =
temple do
outer do
"outer!"
end
inner do
"inner!"
end
end
assert result ==
~s{
outer!
inner!
}
end
test "@temple should be available in any component" do
result =
temple do
has_temple class: "boom" do
"yay!"
end
end
assert result == ~s{
">yay!
}
end
test "normal functions with blocks should be treated like if expressions" do
result =
temple do
leenk to: "/route", class: "foo" do
div class: "hi"
end
end
assert result ==
~s{<%= leenk(to: "/route", class: "foo") do %><% end %>}
end
test "for with 2 generators" do
result =
temple do
for x <- 1..5, y <- 6..10 do
div do: x
div do: y
end
end
assert result ==
~s{<%= for(x <- 1..5, y <- 6..10) do %>
<%= x %>
<%= y %>
<% end %>}
end
test "can pass an expression as assigns" do
result =
temple do
fieldset if true == false, do: [disabled: true], else: [] do
input type: "text"
end
end
assert result ==
~s{}
end
test "can pass a variable as assigns" do
result =
temple do
fieldset foo_bar do
input type: "text"
end
end
assert result ==
~s{}
end
test "can pass a function as assigns" do
result =
temple do
fieldset Foo.foo_bar() do
input type: "text"
end
end
assert result ==
~s{}
end
test "can pass a function as assigns that has @temple" do
result =
temple do
has_temple_function_assign class: "justify-end", style: "color: pink" do
input type: "text"
end
end
expected =
~S"""
>
"""
|> String.trim()
assert result == expected
assert evaluate_template(result) == evaluate_template(expected)
end
defp evaluate_template(template) do
template
|> EEx.compile_string(engine: Phoenix.HTML.Engine)
|> Code.eval_quoted()
|> elem(0)
|> Phoenix.HTML.safe_to_string()
end
end