|
| 1 | +defmodule Volt.Compiler.Passes.InlineTest do |
| 2 | + use ExUnit.Case |
| 3 | + |
| 4 | + alias Volt.Compiler.Passes.Inline |
| 5 | + |
| 6 | + describe "props-only components" do |
| 7 | + test "inlines render expression with prop substitution" do |
| 8 | + expr = {:volt, [], [Volt.Test.Greeting, [name: "Zach"]]} |
| 9 | + {:ok, result} = Inline.run(expr, nil) |
| 10 | + |
| 11 | + # Should be "Hello, " <> "Zach" |
| 12 | + assert {:<>, _, ["Hello, ", "Zach"]} = result |
| 13 | + end |
| 14 | + |
| 15 | + test "uses default when prop not provided" do |
| 16 | + expr = {:volt, [], [Volt.Test.Greeting]} |
| 17 | + {:ok, result} = Inline.run(expr, nil) |
| 18 | + |
| 19 | + # Should be "Hello, " <> "World" |
| 20 | + assert {:<>, _, ["Hello, ", "World"]} = result |
| 21 | + end |
| 22 | + |
| 23 | + test "substitutes AST expressions for props" do |
| 24 | + expr = {:volt, [], [Volt.Test.Greeting, [name: {:@, [], [{:x, [], nil}]}]]} |
| 25 | + {:ok, result} = Inline.run(expr, nil) |
| 26 | + |
| 27 | + # Should be "Hello, " <> @x |
| 28 | + assert {:<>, _, ["Hello, ", {:@, _, [{:x, _, _}]}]} = result |
| 29 | + end |
| 30 | + |
| 31 | + test "inlines wrapper component" do |
| 32 | + expr = {:volt, [], [Volt.Test.Wrapper, [content: "inner"]]} |
| 33 | + {:ok, result} = Inline.run(expr, nil) |
| 34 | + |
| 35 | + # Should be %{wrapped: "inner"} |
| 36 | + assert {:%{}, _, [{:wrapped, "inner"}]} = result |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + describe "components with data are NOT inlined" do |
| 41 | + test "volt with state is left as-is" do |
| 42 | + expr = {:volt, [], [Volt.Test.WithState]} |
| 43 | + {:ok, result} = Inline.run(expr, nil) |
| 44 | + |
| 45 | + assert {:volt, _, [Volt.Test.WithState]} = result |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + describe "unresolvable modules are skipped" do |
| 50 | + test "unknown module is left as-is" do |
| 51 | + expr = {:volt, [], [{:__aliases__, [], [:NoSuchModule]}, [name: "hi"]]} |
| 52 | + {:ok, result} = Inline.run(expr, nil) |
| 53 | + |
| 54 | + assert {:volt, _, _} = result |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + describe "nested inlining" do |
| 59 | + test "recursively inlines nested props-only volts" do |
| 60 | + # volt(Wrapper, content: volt(Greeting, name: "Zach")) |
| 61 | + inner = {:volt, [], [Volt.Test.Greeting, [name: "Zach"]]} |
| 62 | + expr = {:volt, [], [Volt.Test.Wrapper, [content: inner]]} |
| 63 | + {:ok, result} = Inline.run(expr, nil) |
| 64 | + |
| 65 | + # Wrapper inlines to %{wrapped: @content} |
| 66 | + # @content was volt(Greeting, name: "Zach") which inlines to "Hello, " <> "Zach" |
| 67 | + assert {:%{}, _, [{:wrapped, {:<>, _, ["Hello, ", "Zach"]}}]} = result |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + describe "mixed with non-inlineable content" do |
| 72 | + test "inlines only props-only volts, leaves others" do |
| 73 | + # [volt(Greeting, name: "A"), volt(WithState)] |
| 74 | + expr = [ |
| 75 | + {:volt, [], [Volt.Test.Greeting, [name: "A"]]}, |
| 76 | + {:volt, [], [Volt.Test.WithState]} |
| 77 | + ] |
| 78 | + |
| 79 | + {:ok, result} = Inline.run(expr, nil) |
| 80 | + |
| 81 | + assert [{:<>, _, ["Hello, ", "A"]}, {:volt, _, [Volt.Test.WithState]}] = result |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments