Sha256: 346a4d02b4bc740a593bb9410ea494031b5f7ac035118ef52e9d80a81c583629

Contents?: true

Size: 1.52 KB

Versions: 15

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module ViewComponent
  module TestHelpers
    begin
      require "capybara/minitest"
      include Capybara::Minitest::Assertions

      def page
        Capybara::Node::Simple.new(@rendered_component)
      end

      def refute_component_rendered
        assert_no_selector("body")
      end
    rescue LoadError
      # We don't have a test case for running an application without capybara installed.
      # It's probably fine to leave this without coverage.
      # :nocov:
      warn "WARNING in `ViewComponent::TestHelpers`: You must add `capybara` to your Gemfile to use Capybara assertions." if ENV["DEBUG"]
      # :nocov:
    end

    attr_reader :rendered_component

    def render_inline(component, **args, &block)
      @rendered_component =
        if Rails.version.to_f >= 6.1
          controller.view_context.render(component, args, &block)
        else
          controller.view_context.render_component(component, &block)
        end

      Nokogiri::HTML.fragment(@rendered_component)
    end

    def controller
      @controller ||= Base.test_controller.constantize.new.tap { |c| c.request = request }.extend(Rails.application.routes.url_helpers)
    end

    def request
      @request ||= ActionDispatch::TestRequest.create
    end

    def with_variant(variant)
      old_variants = controller.view_context.lookup_context.variants

      controller.view_context.lookup_context.variants = variant
      yield
      controller.view_context.lookup_context.variants = old_variants
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
view_component-2.26.1 lib/view_component/test_helpers.rb
view_component-2.26.0 lib/view_component/test_helpers.rb
view_component-2.25.1 lib/view_component/test_helpers.rb
view_component-2.25.0 lib/view_component/test_helpers.rb
view_component-2.24.0 lib/view_component/test_helpers.rb
view_component-2.23.2 lib/view_component/test_helpers.rb
view_component-2.23.1 lib/view_component/test_helpers.rb
view_component-2.23.0 lib/view_component/test_helpers.rb
view_component-2.22.1 lib/view_component/test_helpers.rb
view_component-2.22.0 lib/view_component/test_helpers.rb
view_component-2.21.0 lib/view_component/test_helpers.rb
view_component-2.20.0 lib/view_component/test_helpers.rb
view_component-2.19.1 lib/view_component/test_helpers.rb
view_component-2.19.0 lib/view_component/test_helpers.rb
view_component-2.18.2 lib/view_component/test_helpers.rb