Module: SolidusAdmin::TestingSupport::ComponentHelpers

Defined in:
lib/solidus_admin/testing_support/component_helpers.rb

Instance Method Summary collapse

Instance Method Details

#mock_component(&definition) ⇒ Object

Mocks a component class with the given definition.

Examples:

mock_component do
  def call
    "Rendered"
  end
end

Parameters:

  • definition (Proc)

    the component definition



15
16
17
18
19
20
21
22
23
24
# File 'lib/solidus_admin/testing_support/component_helpers.rb', line 15

def mock_component(&definition)
  location = caller(1, 1).first
  component_class = Class.new(SolidusAdmin::BaseComponent)
  # ViewComponent will complain if we don't fake a class name:
  # @see https://github.com/ViewComponent/view_component/blob/5decd07842c48cbad82527daefa3fe9c65a4226a/lib/view_component/base.rb#L371
  component_class.define_singleton_method(:name) { "Foo" }
  component_class.define_singleton_method(:to_s) { "#{name} (#{location})" }
  component_class.class_eval(&definition) if definition
  component_class
end