# frozen_string_literal: true
require_relative "../../test_helper"
require_relative "../../../lib/herb/engine/slots/subtree"
module Engine
module Slots
class SubtreeTest < Minitest::Spec
class View
def initialize(**assigns)
assigns.each { |name, value| instance_variable_set(:"@#{name}", value) }
end
end
CONDITIONAL = "
<% if @admin %>yes<% else %>no<% end %>
"
COLLECTION = "<% @items.each do |item| %>- <%= item %>
<% end %>
"
def subtree(source)
Herb::Engine::Slots::Subtree.new(source, filename: "app/views/posts/index.html.erb")
end
def render(source, index, **assigns)
View.new(**assigns).instance_eval(subtree(source).source_for(index))
end
test "renders the branch the page is showing" do
assert_equal "no", render(CONDITIONAL, 0, admin: false)
end
test "renders a branch the page never had" do
assert_equal "yes", render(CONDITIONAL, 0, admin: true)
end
test "renders every item a collection has" do
assert_equal "ab", render(COLLECTION, 0, items: ["a", "b"])
end
test "renders the first item a collection has ever had" do
assert_equal "only", render(COLLECTION, 0, items: ["only"])
end
test "renders nothing for a collection that lost every item" do
assert_equal "", render(COLLECTION, 0, items: [])
end
test "refuses an attribute, which the path cannot name on its own" do
source = %(x
)
refute subtree(source).addressable?(0)
assert_nil subtree(source).source_for(0)
end
test "refuses a slot the template does not have" do
assert_nil subtree(CONDITIONAL).source_for(99)
refute subtree(CONDITIONAL).addressable?(99)
end
test "names each slot by the path the visitor recorded" do
visitor = Herb::Engine::Slots::Visitor.new
Herb::Engine.new(COLLECTION, visitors: [visitor], filename: "app/views/posts/index.html.erb")
subject = subtree(COLLECTION)
visitor.slots.each do |slot|
assert_equal slot.node_path, subject.node_path_for(slot.index)
end
end
test "carries the version the markers were written with" do
visitor = Herb::Engine::Slots::Visitor.new
Herb::Engine.new(COLLECTION, visitors: [visitor], filename: "app/views/posts/index.html.erb")
assert_equal visitor.schema[:version], subtree(COLLECTION).version
end
end
end
end