# frozen_string_literal: true

require "helper"

class TestComponents < BridgetownUnitTest
  def setup
    @site = fixture_site({ level: "Level" })
    @site.process
    @erb_page = @site.collections.pages.resources.find { |page| page.data.title == "I'm an ERB Page" }
  end

  context "basic Ruby components" do
    should "should render" do
      assert_includes @erb_page.output, "Here's the page title! <strong>I'm an ERB Page</strong>"
    end

    should "allow source components to override plugin components" do
      assert_includes @erb_page.output, "Yay, it got overridden!"
    end
  end

  context "Bridgetown::Component" do
    should "should render with captured block content" do
      # lots of funky whitespace from all the erb captures!
      spaces = "  "
      morespaces = "      "
      assert_includes @erb_page.output, <<~HTML
        <app-card>
          <header>I&#39;M A CARD</header>
          <app-card-inner>
          #{spaces}
          <p>I'm the body of the card</p>

          #{morespaces}<img src="test.jpg" />

          </app-card-inner>
          <footer>I&#39;m a footer</footer>
        </app-card>
      HTML
    end

    should "not render if render? is false" do
      refute_includes @erb_page.output, "NOPE"
      refute_includes @erb_page.output, "Canceled!"
    end

    should "handle nested renders" do
      levels = []
      4.times do |i|
        levels << "#{i + 1}: Level #{i + 1}"
      end

      assert_includes @erb_page.output, levels.join("\n")
      assert_includes @erb_page.output, "4: Level 4\n\n  <p><strong>Level 4!!</strong></p>"
    end
  end
end
