This repository has been archived on 2023-08-07. You can view files and clone it, but cannot push or open issues or pull requests.
temple/test/temple_test.exs

321 lines
7.4 KiB
Elixir
Raw Normal View History

2019-07-02 02:48:51 +00:00
defmodule TempleTest do
use ExUnit.Case, async: true
2019-07-02 02:48:51 +00:00
use Temple
use Temple.Support.Utils
2020-06-16 19:28:21 +00:00
test "renders an attribute on a div passed as a variable" do
result =
temple do
div class: "hello" do
div class: "hi"
end
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result == ~s{<div class="hello"><div class="hi"></div></div>}
end
2020-06-16 19:28:21 +00:00
test "renders void element" do
result =
temple do
input name: "password"
end
2020-06-16 19:28:21 +00:00
assert result == ~s{<input name="password">}
end
2020-06-16 19:28:21 +00:00
test "renders a text node from the text keyword with siblings" do
result =
temple do
div class: "hello" do
"hi"
"foo"
end
end
2020-06-16 19:28:21 +00:00
assert result == ~s{<div class="hello">hifoo</div>}
end
2020-06-16 19:28:21 +00:00
test "renders a variable text node as eex" do
result =
temple do
div class: "hello" do
foo
end
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result == ~s{<div class="hello"><%= foo %></div>}
end
2020-06-16 19:28:21 +00:00
test "renders an assign text node as eex" do
result =
temple do
div class: "hello" do
@foo
end
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result == ~s{<div class="hello"><%= @foo %></div>}
end
2020-06-16 19:28:21 +00:00
test "renders a match expression" do
result =
temple do
x = 420
2020-06-16 19:28:21 +00:00
div do
"blaze it"
end
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result == ~s{<% x = 420 %><div>blaze it</div>}
end
2020-06-16 19:28:21 +00:00
test "renders a non-match expression" do
result =
temple do
IO.inspect(:foo)
2020-06-16 19:28:21 +00:00
div do
"bar"
end
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result == ~s{<%= IO.inspect(:foo) %><div>bar</div>}
end
2020-06-16 19:28:21 +00:00
test "renders an expression in attr as eex" do
result =
temple do
div class: foo <> " bar"
end
2020-06-16 19:28:21 +00:00
assert result == ~s{<div class="<%= foo <> " bar" %>"></div>}
end
2020-06-16 19:28:21 +00:00
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
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result ==
~s{<div class="<%= Enum.map([:one, :two], fn x -> x end) %>"><div class="hi"></div></div>}
end
2020-06-16 19:28:21 +00:00
test "renders a for comprehension as eex" do
result =
temple do
for x <- 1..5 do
div class: "hi"
end
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result == ~s{<%= for(x <- 1..5) do %><div class="hi"></div><% end %>}
end
2020-06-16 19:28:21 +00:00
test "renders an if expression as eex" do
result =
temple do
if true == false do
div class: "hi"
end
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result == ~s{<%= if(true == false) do %><div class="hi"></div><% end %>}
end
2020-06-16 19:28:21 +00:00
test "renders an if/else expression as eex" do
result =
temple do
if true == false do
div class: "hi"
else
div class: "haha"
end
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result ==
~s{<%= if(true == false) do %><div class="hi"></div><% else %><div class="haha"></div><% end %>}
end
2020-06-16 19:28:21 +00:00
test "renders an unless expression as eex" do
result =
temple do
unless true == false do
div class: "hi"
end
end
2020-06-16 19:28:21 +00:00
assert result == ~s{<%= unless(true == false) do %><div class="hi"></div><% end %>}
end
2020-11-05 00:57:03 +00:00
test "renders a case expression as eex" do
result =
temple do
2020-11-05 01:21:32 +00:00
case @foo do
2020-11-05 00:57:03 +00:00
:baz ->
2020-11-05 01:21:32 +00:00
some_component(form: @form)
2020-11-05 00:57:03 +00:00
end
end
expected =
~S"""
2020-11-05 01:21:32 +00:00
<%= case(@foo) do %>
2020-11-05 00:57:03 +00:00
<% :baz -> %>
2020-11-05 01:21:32 +00:00
<%= some_component(form: @form) %>
2020-11-05 00:57:03 +00:00
<% end %>
"""
|> String.trim()
assert result == expected
end
2020-06-16 19:28:21 +00:00
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
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result ==
~s{<%= form_for Routes.user_path(@conn, :create), fn f -> %>Name: <%= text_input(f, :name) %><% end %>}
end
2020-06-16 19:28:21 +00:00
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
2020-06-16 19:28:21 +00:00
assert result ==
~s{<%= form_for @changeset, Routes.user_path(@conn, :create), fn f -> %>Name: <%= text_input(f, :name) %><% end %>}
end
2020-06-16 19:28:21 +00:00
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
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result ==
~s{<%= form_for @changeset, Routes.user_path(@conn, :create), fn f -> %><div>Name: <%= text_input(f, :name) %></div><% end %>}
end
2020-06-16 19:28:21 +00:00
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
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result ==
~s{<%= form_for @changeset, Routes.user_path(@conn, :create), [foo: :bar], fn f -> %>Name: <%= text_input(f, :name) %><% end %>}
end
2020-06-16 19:28:21 +00:00
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
2020-06-16 19:28:21 +00:00
test "tags prefixed with Temple. should be interpreted as temple tags" do
result =
temple do
div do
Temple.span do
"bob"
end
end
2020-06-16 19:28:21 +00:00
end
2020-06-16 19:28:21 +00:00
assert result == ~s{<div><span>bob</span></div>}
end
2020-06-16 19:28:21 +00:00
test "can pass do as an arg instead of a block" do
result =
temple do
div class: "font-bold" do
"Hello, world"
end
2020-06-16 19:28:21 +00:00
div class: "font-bold", do: "Hello, world"
div do: "Hello, world"
end
2020-06-16 19:28:21 +00:00
assert result ==
~s{<div class="font-bold">Hello, world</div><div class="font-bold">Hello, world</div><div>Hello, world</div>}
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 %><div><%= x %></div><div><%= y %></div><% 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 ==
2021-04-09 04:12:19 +00:00
~s{<fieldset<%= Temple.Parser.Utils.runtime_attrs(if(true == false) do [disabled: true]else []end) %>><input type="text"></fieldset>}
end
test "can pass a variable as assigns" do
result =
temple do
fieldset foo_bar do
input type: "text"
end
end
assert result ==
2021-04-09 04:12:19 +00:00
~s{<fieldset<%= Temple.Parser.Utils.runtime_attrs(foo_bar) %>><input type="text"></fieldset>}
end
test "can pass a function as assigns" do
result =
temple do
fieldset Foo.foo_bar() do
input type: "text"
end
end
assert result ==
2021-04-09 04:12:19 +00:00
~s{<fieldset<%= Temple.Parser.Utils.runtime_attrs(Foo.foo_bar()) %>><input type="text"></fieldset>}
end
2019-04-15 01:44:39 +00:00
end