2021-01-02 18:21:48 +00:00
|
|
|
defmodule Temple.Parser.ComponentsTest do
|
|
|
|
use ExUnit.Case, async: false
|
|
|
|
alias Temple.Parser.Components
|
|
|
|
use Temple.Support.Utils
|
|
|
|
|
|
|
|
describe "applicable?/1" do
|
|
|
|
test "runs when using the `c` ast with a block" do
|
|
|
|
ast =
|
|
|
|
quote do
|
|
|
|
c SomeModule, foo: :bar do
|
|
|
|
div do
|
|
|
|
"hello"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert Components.applicable?(ast)
|
|
|
|
end
|
|
|
|
|
2021-04-09 04:27:18 +00:00
|
|
|
test "runs when using the `c` ast with an inline block" do
|
|
|
|
ast =
|
|
|
|
quote do
|
|
|
|
c SomeModule, foo: :bar, do: "hello"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert Components.applicable?(ast)
|
|
|
|
end
|
|
|
|
|
2021-01-02 18:21:48 +00:00
|
|
|
test "runs when using the `c` ast without a block" do
|
|
|
|
ast =
|
|
|
|
quote do
|
|
|
|
c(SomeModule, foo: :bar)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert Components.applicable?(ast)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "run/2" do
|
2021-03-15 04:46:16 +00:00
|
|
|
test "adds a node to the buffer" do
|
|
|
|
raw_ast =
|
|
|
|
quote do
|
|
|
|
c SomeModule do
|
|
|
|
aside class: "foobar" do
|
|
|
|
"I'm a component!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-02 18:21:48 +00:00
|
|
|
|
2021-03-15 04:46:16 +00:00
|
|
|
ast = Components.run(raw_ast)
|
|
|
|
|
2021-04-09 03:04:26 +00:00
|
|
|
assert %Components{
|
2021-03-15 04:46:16 +00:00
|
|
|
content: SomeModule,
|
|
|
|
attrs: [],
|
|
|
|
children: _
|
|
|
|
} = ast
|
|
|
|
end
|
|
|
|
|
2021-04-09 04:27:18 +00:00
|
|
|
test "runs when using the `c` ast with an inline block" do
|
|
|
|
ast =
|
|
|
|
quote do
|
|
|
|
c SomeModule, foo: :bar, do: "hello"
|
|
|
|
end
|
|
|
|
|
|
|
|
ast = Components.run(ast)
|
|
|
|
|
|
|
|
assert %Components{
|
|
|
|
content: SomeModule,
|
|
|
|
attrs: [foo: :bar],
|
|
|
|
children: _
|
|
|
|
} = ast
|
|
|
|
end
|
|
|
|
|
2021-03-15 04:46:16 +00:00
|
|
|
test "adds a node to the buffer that takes args" do
|
|
|
|
raw_ast =
|
2021-01-02 18:21:48 +00:00
|
|
|
quote do
|
|
|
|
c SomeModule, foo: :bar do
|
|
|
|
aside class: "foobar" do
|
|
|
|
"I'm a component!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-15 04:46:16 +00:00
|
|
|
ast = Components.run(raw_ast)
|
2021-01-02 18:21:48 +00:00
|
|
|
|
2021-04-09 03:04:26 +00:00
|
|
|
assert %Components{
|
2021-03-15 04:46:16 +00:00
|
|
|
content: SomeModule,
|
|
|
|
attrs: [foo: :bar],
|
|
|
|
children: _
|
|
|
|
} = ast
|
2021-01-02 18:21:48 +00:00
|
|
|
end
|
2021-04-09 03:04:26 +00:00
|
|
|
|
|
|
|
test "adds a node to the buffer that without a block" do
|
|
|
|
raw_ast =
|
|
|
|
quote do
|
|
|
|
c SomeModule, foo: :bar
|
|
|
|
end
|
|
|
|
|
|
|
|
ast = Components.run(raw_ast)
|
|
|
|
|
|
|
|
assert %Components{
|
|
|
|
content: SomeModule,
|
|
|
|
attrs: [foo: :bar],
|
|
|
|
children: []
|
|
|
|
} = ast
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Temple.EEx.to_eex/1" do
|
|
|
|
test "emits eex for non void component" do
|
|
|
|
raw_ast =
|
|
|
|
quote do
|
|
|
|
c SomeModule, foo: :bar do
|
|
|
|
"I'm a component!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result =
|
|
|
|
raw_ast
|
|
|
|
|> Components.run()
|
|
|
|
|> Temple.EEx.to_eex()
|
|
|
|
|
|
|
|
assert result |> :erlang.iolist_to_binary() ==
|
|
|
|
~s|<%= Phoenix.View.render_layout SomeModule, :self, [foo: :bar] do %>\nI'm a component!\n<% end %>|
|
|
|
|
end
|
|
|
|
|
|
|
|
test "emits eex for void component" do
|
|
|
|
raw_ast =
|
|
|
|
quote do
|
|
|
|
c SomeModule, foo: :bar
|
|
|
|
end
|
|
|
|
|
|
|
|
result =
|
|
|
|
raw_ast
|
|
|
|
|> Components.run()
|
|
|
|
|> Temple.EEx.to_eex()
|
|
|
|
|
|
|
|
assert result |> :erlang.iolist_to_binary() ==
|
|
|
|
~s|<%= Phoenix.View.render SomeModule, :self, [foo: :bar] %>|
|
|
|
|
end
|
2021-01-02 18:21:48 +00:00
|
|
|
end
|
|
|
|
end
|