# frozen_string_literal: true
require_relative "../../test_helper"
require_relative "../../snapshot_utils"
require_relative "../../../lib/herb/engine"
require_relative "../../../lib/herb/engine/slots/visitor"
require_relative "../../../lib/herb/engine/slots/dynamics_compiler"
module Engine
module Slots
class StaticsTest < Minitest::Spec
include SnapshotUtils
def options
{ visitors: [Herb::Engine::Slots::Visitor.new(mode: :client)], filename: "app/views/test.html.erb" }
end
def render(template, locals = {})
evaluate_herb_source(Herb::Engine.new(template, **options).src, locals)
end
def parked(template, locals = {})
render(template, locals)[%r{(.*)}m, 1]
end
test "parks both branches when only one of them rendered" do
assert_evaluated_snapshot(
"
<% if @admin %>Hi <%= @name %><% else %>guest<% end %>
",
{ "@admin" => false, "@name" => "Marco" },
options
)
end
test "parks every arm of a case, including the one that ran" do
assert_evaluated_snapshot(
"
<% case @s %><% when 1 %>one<% when 2 %>two<% else %>other<% end %>
",
{ "@s" => 3 },
options
)
end
test "parks both branches whichever one ran first" do
assert_evaluated_snapshot(
"
<% if @admin %>Hi <%= @name %><% else %>guest<% end %>
",
{ "@admin" => true, "@name" => "Marco" },
options
)
end
test "parks a conditional nested in a branch on its own rather than inside its parent" do
assert_evaluated_snapshot(
"
<% if @a %><% if @b %>deep<% end %>outer<% end %>
",
{ "@a" => false, "@b" => false },
options
)
end
test "keeps the markers of the slots inside a branch, so the client can fill them" do
assert_evaluated_snapshot(
%(
<% if @a %><%= @name %><% end %>
),
{ "@a" => false, "@c" => "item", "@name" => "Marco" },
options
)
end
test "parks nothing for a template that has no conditionals" do
assert_evaluated_snapshot("
Hi <%= @name %>
", { "@name" => "Marco" }, options)
end
test "names the template it belongs to, so it does not depend on where it sits" do
output = render("
<% if @a %>x<% end %>
", { "@a" => false })
assert_match(%r{}, output)
end
test "names the version the region marker names" do
output = render("
<% if @a %>x<% end %>
", { "@a" => false })
assert_snapshot_matches(output, "statics_test-0")
end
test "sits outside the region it belongs to" do
output = render("
", { "@a" => false })
end
test "orders branches by the slot they belong to" do
markup = parked("<% if @a %>a<% if @b %>b<% end %><% end %><% if @c %>c<% end %>", { "@a" => false, "@b" => false, "@c" => false })
assert_equal ["0:0", "1:0", "2:0"], markup.scan(/herb-branch:(\d+:\d+)/).flatten
end
test "parks a conditional's only branch even once it has rendered" do
assert_evaluated_snapshot("
<% if @a %>x<% end %>
", { "@a" => true }, options)
end
test "parks every branch a collection's rows might need, whatever they covered between them" do
template = "
<% @items.each do |i| %>
<% if i.odd? %>odd<% else %>even<% end %>
<% end %>
"
assert_evaluated_snapshot(template, { "@items" => [1, 2] }, options)
assert_evaluated_snapshot(template, { "@items" => [1, 3] }, options)
end
test "parks a keyed collection's item template only when no row rendered" do
template = %(
<% @items.each do |item| %>
<%= item %>
<% end %>
)
assert_evaluated_snapshot(template, { "@items" => ["a", "b"] }, options)
end
test "parks the item template, with its values blanked, when the collection rendered nothing" do
assert_evaluated_snapshot(
%(
<% @items.each do |item| %>
<%= item %>
<% end %>
),
{ "@items" => [] },
options
)
end
test "parks an item template for an unkeyed collection, which is addressed by position" do
assert_evaluated_snapshot("
<% @items.each do |item| %>
<%= item %>
<% end %>
", { "@items" => ["a", "b"] }, options)
end
test "parks the branches of every conditional, taken or not" do
markup = parked("<% if @a %>a<% end %><% if @b %>b<% end %>", { "@a" => true, "@b" => false })
assert_equal ["0:0", "1:0"], markup.scan(/herb-branch:(\d+:\d+)/).flatten
end
test "keeps what it counts to itself" do
assert_compiled_snapshot("
<% if @a %>x<% end %>
", options)
end
test "parks a branch once for a partial rendered many times in one response" do
source = Herb::Engine.new("
<% if @a %>x<% else %>y<% end %>
", **options).src
view = Object.new
view.instance_variable_set(:@_herb_covered, {})
view.define_singleton_method(:render) do |a|
@a = a
instance_eval(source, __FILE__, __LINE__)
end
rendered = Array.new(3) { view.render(true) }.join
assert_equal 1, rendered.scan("<% if @a %>x<% else %>y<% end %>",
visitors: [Herb::Engine::Slots::Visitor.new(mode: :server)],
filename: "app/views/test.html.erb"
).src
assert_snapshot_matches(compiled, "compiled in server mode")
assert_snapshot_matches(evaluate_herb_source(compiled, { "@a" => false }), "rendered in server mode")
end
test "parks nothing for an interpolated attribute, whose stretches the manifest carries" do
assert_evaluated_snapshot(
%(
x
),
{ "@id" => 7, "@kind" => "a", "@size" => "b" },
options
)
end
test "carries an interpolated attribute's static segments in the manifest" do
visitor = Herb::Engine::Slots::Visitor.new(mode: :client)
Herb::Engine.new(%(
x
), visitors: [visitor], filename: "app/views/test.html.erb")
assert_equal({ "0" => ["message_", ""], "1" => ["row-", "-of-", ""] }, visitor.manifest["parts"])
end
test "the values payload carries an interpolated attribute as its dynamic parts" do
template = %(
x
)
source = Herb::Engine::Slots::DynamicsCompiler.new(template, filename: "app/views/test.html.erb").src
payload = evaluate_herb_source(source, { "@kind" => "a", "@size" => "b" })
assert_equal ["a", "b"], payload[:slots][0]
end
test "parks no segments for an attribute holding more than outputs" do
assert_evaluated_snapshot(%(
c
), { "@a" => true }, options)
end
test "parks nothing at all unless the mode asks for it" do
source = Herb::Engine.new(
"
<% if @a %>x<% end %>
",
visitors: [Herb::Engine::Slots::Visitor.new],
filename: "app/views/test.html.erb"
).src
assert_snapshot_matches(evaluate_herb_source(source, { "@a" => false }), "rendered with no delivery asked for")
end
test "wraps a branch inside an svg in a foreign root, so the client parses it as SVG" do
assert_evaluated_snapshot(%(), { "@a" => false }, options)
end
test "wraps a keyed collection's item template inside an svg in a foreign root" do
assert_evaluated_snapshot(
%(),
{ "@items" => [] },
options
)
end
test "wraps a keyed element inside an svg in a foreign root" do
assert_evaluated_snapshot(%(<%# herb:slots %>\n), { "@id" => "a", "@name" => "Marco" }, options)
end
test "wraps a branch inside a math element in a foreign root" do
assert_evaluated_snapshot(%(), { "@a" => false }, options)
end
test "does not wrap a branch inside a foreignObject, which holds HTML" do
assert_evaluated_snapshot(%(), { "@a" => false }, options)
end
test "does not wrap a branch inside an HTML integration point of MathML" do
assert_evaluated_snapshot(%(), { "@a" => false }, options)
end
end
end
end