module ViewComponent::TestHelpers

def __vc_test_helpers_build_controller(klass)

def __vc_test_helpers_build_controller(klass)
  klass.new.tap { |c| c.request = vc_test_request }.extend(Rails.application.routes.url_helpers)
end

def __vc_test_helpers_preview_class

def __vc_test_helpers_preview_class
  result = if respond_to?(:described_class)
    raise "`render_preview` expected a described_class, but it is nil." if described_class.nil?
    "#{described_class}Preview"
  else
    self.class.name.gsub("Test", "Preview")
  end
  result = result.constantize
rescue NameError
  raise NameError, "`render_preview` expected to find #{result}, but it does not exist."
end

def page

def page
  @page ||= Capybara::Node::Simple.new(rendered_content)
end

def refute_component_rendered

def refute_component_rendered
  assert_no_selector("body")
end

def render_in_view_context(*args, &block)

```
assert_text("Hello, World!")

end
render(MyComponent.new(arg1, arg2))
render_in_view_context(arg1, arg2: nil) do |arg1, arg2:|
```ruby

Capybara assertions to be used. All arguments are forwarded to the block.
Internally sets `page` to be a `Capybara::Node::Simple`, allowing for
Execute the given block in the view context (using `instance_exec`).
def render_in_view_context(*args, &block)
  @page = nil
  @rendered_content = vc_test_controller.view_context.instance_exec(*args, &block)
  Nokogiri::HTML.fragment(@rendered_content)
end

def render_inline(component, **args, &block)

Returns:
  • (Nokogiri::HTML) -

Parameters:
  • component (ViewComponent::Base, ViewComponent::Collection) -- The instance of the component to be rendered.
def render_inline(component, **args, &block)
  @page = nil
  @rendered_content =
    if Rails.version.to_f >= 6.1
      vc_test_controller.view_context.render(component, args, &block)
    else
      vc_test_controller.view_context.render_component(component, &block)
    end
  Nokogiri::HTML.fragment(@rendered_content)
end

def render_preview(name, from: __vc_test_helpers_preview_class, params: {})

Returns:
  • (Nokogiri::HTML) -

Parameters:
  • params (Hash) -- Parameters to be passed to the preview.
  • from (ViewComponent::Preview) -- The class of the preview to be rendered.
  • name (String) -- The name of the preview to be rendered.
def render_preview(name, from: __vc_test_helpers_preview_class, params: {})
  previews_controller = __vc_test_helpers_build_controller(Rails.application.config.view_component.preview_controller.constantize)
  # From what I can tell, it's not possible to overwrite all request parameters
  # at once, so we set them individually here.
  params.each do |k, v|
    previews_controller.request.params[k] = v
  end
  previews_controller.request.params[:path] = "#{from.preview_name}/#{name}"
  previews_controller.set_response!(ActionDispatch::Response.new)
  result = previews_controller.previews
  @rendered_content = result
  Nokogiri::HTML.fragment(@rendered_content)
end

def vc_test_controller

Returns:
  • (ActionController::Base) -
def vc_test_controller
  @vc_test_controller ||= __vc_test_helpers_build_controller(Base.test_controller.constantize)
end

def vc_test_request

Returns:
  • (ActionDispatch::TestRequest) -
def vc_test_request
  @vc_test_request ||=
    begin
      out = ActionDispatch::TestRequest.create
      out.session = ActionController::TestSession.new
      out
    end
end

def with_controller_class(klass)

Parameters:
  • klass (ActionController::Base) -- The controller to be used.
def with_controller_class(klass)
  old_controller = defined?(@vc_test_controller) && @vc_test_controller
  @vc_test_controller = __vc_test_helpers_build_controller(klass)
  yield
ensure
  @vc_test_controller = old_controller
end

def with_request_url(path)

Parameters:
  • path (String) -- The path to set for the current request.
def with_request_url(path)
  old_request_path_info = vc_test_request.path_info
  old_request_path_parameters = vc_test_request.path_parameters
  old_request_query_parameters = vc_test_request.query_parameters
  old_request_query_string = vc_test_request.query_string
  old_controller = defined?(@vc_test_controller) && @vc_test_controller
  path, query = path.split("?", 2)
  vc_test_request.path_info = path
  vc_test_request.path_parameters = Rails.application.routes.recognize_path_with_request(vc_test_request, path, {})
  vc_test_request.set_header("action_dispatch.request.query_parameters", Rack::Utils.parse_nested_query(query))
  vc_test_request.set_header(Rack::QUERY_STRING, query)
  yield
ensure
  vc_test_request.path_info = old_request_path_info
  vc_test_request.path_parameters = old_request_path_parameters
  vc_test_request.set_header("action_dispatch.request.query_parameters", old_request_query_parameters)
  vc_test_request.set_header(Rack::QUERY_STRING, old_request_query_string)
  @vc_test_controller = old_controller
end

def with_variant(variant)

Parameters:
  • variant (Symbol) -- The variant to be set for the provided block.
def with_variant(variant)
  old_variants = vc_test_controller.view_context.lookup_context.variants
  vc_test_controller.view_context.lookup_context.variants = variant
  yield
ensure
  vc_test_controller.view_context.lookup_context.variants = old_variants
end