Sha256: 5ead019e4026fadde1cdea0022bd67997394eb5421240224c33cab7be96ecf4b

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

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

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

      def refute_component_rendered
        assert_no_selector("body")
      end
    rescue LoadError
      warn "WARNING in `ViewComponent::TestHelpers`: You must add `capybara` to your Gemfile to use Capybara assertions."
    end

    def render_inline(component, **args, &block)
      @raw = controller.view_context.render(component, args, &block)

      Nokogiri::HTML.fragment(@raw)
    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

4 entries across 4 versions & 1 rubygems

Version Path
view_component-2.6.0 lib/view_component/test_helpers.rb
view_component-2.5.1 lib/view_component/test_helpers.rb
view_component-2.5.0 lib/view_component/test_helpers.rb
view_component-2.4.0 lib/view_component/test_helpers.rb