require "rails_helper"

RSpec.describe "previews", type: :request do
  context "param default values" do
    it "renders previews with non-static default param values" do
      get lookbook_preview_path("params/dynamic_args")

      expect(html).to have_content "the multiline"
      expect(html).to have_content "default value"
    end
  end

  it "doesn't beautify HTML in the preview" do
    get lookbook_preview_path("whitespace_significant/default")

    expect(response.body.include?("<div>one</div><div>two</div><div>three</div>")).to be true
  end

  context "ViewComponents" do
    it "renders view component previews" do
      get lookbook_preview_path("view_component_example/default")

      expect(html).to have_content "viewcomponent component"
    end
  end

  if AppHelper.phlexible?
    context "Phlex components" do
      it "renders Phlex component previews" do
        get lookbook_preview_path("phlex_example/default")

        expect(html.has_css?("p", text: "phlex component")).to be true
      end

      it "supports helpers" do
        get lookbook_preview_path("phlex_example/helpers")

        expect(html).to have_selector "a[href='/']"
      end

      it "renders builder-style components" do
        get lookbook_preview_path("phlex_example/builder")

        expect(html.has_css?("li", text: "Hello")).to be true
        expect(html.has_css?("li", text: "World")).to be true
      end
    end
  end

  context "View partials" do
    context "with single-pass rendering" do
      before { Lookbook.config.preview_single_pass_rendering = true }
      after { Lookbook.config.preview_single_pass_rendering = false }

      it "supports content_for" do
        get lookbook_preview_path("partial_example/content_for")

        expect(html).to have_content "Custom title using content_for"
      end
    end

    it "does not support content_for by default" do
      get lookbook_preview_path("partial_example/content_for")

      expect(html).not_to have_content "Custom title using content_for"
    end

    it "renders view partial previews" do
      get lookbook_preview_path("partial_example/default")

      expect(html.has_css?("p", text: "view partial")).to be true
    end

    it "supports helpers" do
      get lookbook_preview_path("partial_example/helpers")

      expect(html).to have_selector "a[href='/']"
    end

    it "has access to host app url helpers" do
      get lookbook_preview_path("partial_example/url_helpers")

      expect(html).to have_selector "a[href='/blog']"
    end
  end

  context "after_render" do
    it "supports preview class after_render" do
      get lookbook_preview_path("after_render/default")

      expect(html.has_css?("em", text: "preview")).to be true
      expect(html).to have_content "default after render content"
    end

    it "supports scenario after_render" do
      get lookbook_preview_path("after_render/custom")

      expect(html.has_css?("strong", text: "scenario")).to be true
      expect(html).to have_content "custom after render content"
    end

    it "provides context information" do
      get lookbook_preview_path("after_render/with_context")

      expect(html.has_css?("strong", text: "with_context")).to be true
      expect(html).to have_content "after render using context info"
    end

    context "in ViewComponent::Preview" do
      it "supports preview class after_render" do
        get lookbook_preview_path("after_render_view_component_example/default")

        expect(html.has_css?("em", text: "preview")).to be true
        expect(html).to have_content "default after render content"
      end

      it "supports scenario after_render" do
        get lookbook_preview_path("after_render_view_component_example/custom")

        expect(html.has_css?("strong", text: "scenario")).to be true
        expect(html).to have_content "custom after render content"
      end
    end
  end
end
