Module: ViewComponent::TestHelpers

Includes:
Capybara::Minitest::Assertions
Included in:
TestCase
Defined in:
lib/view_component/test_helpers.rb

Instance Method Summary collapse

Instance Method Details

#controllerObject



19
20
21
# File 'lib/view_component/test_helpers.rb', line 19

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

#pageObject



9
10
11
# File 'lib/view_component/test_helpers.rb', line 9

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

#render_component(component, **args, &block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/view_component/test_helpers.rb', line 27

def render_component(component, **args, &block)
  ActiveSupport::Deprecation.warn(
    "`render_component` has been deprecated in favor of `render_inline`, and will be removed in v2.0.0."
  )

  render_inline(component, args, &block)
end

#render_inline(component, **args, &block) ⇒ Object



13
14
15
16
17
# File 'lib/view_component/test_helpers.rb', line 13

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

  Nokogiri::HTML.fragment(@raw)
end

#requestObject



23
24
25
# File 'lib/view_component/test_helpers.rb', line 23

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

#with_variant(variant) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/view_component/test_helpers.rb', line 35

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