Module: ViewComponent::TestHelpers
- Included in:
- TestCase
- Defined in:
- lib/view_component/test_helpers.rb
Instance Attribute Summary collapse
-
#rendered_component ⇒ Object
readonly
Returns the value of attribute rendered_component.
Instance Method Summary collapse
- #build_controller(klass) ⇒ Object
- #controller ⇒ Object
- #render_inline(component, **args, &block) ⇒ Object
- #request ⇒ Object
- #with_controller_class(klass) ⇒ Object
- #with_request_url(path) ⇒ Object
- #with_variant(variant) ⇒ Object
Instance Attribute Details
#rendered_component ⇒ Object (readonly)
Returns the value of attribute rendered_component.
30 31 32 |
# File 'lib/view_component/test_helpers.rb', line 30 def rendered_component @rendered_component end |
Instance Method Details
#build_controller(klass) ⇒ Object
85 86 87 |
# File 'lib/view_component/test_helpers.rb', line 85 def build_controller(klass) klass.new.tap { |c| c.request = request }.extend(Rails.application.routes.url_helpers) end |
#controller ⇒ Object
43 44 45 |
# File 'lib/view_component/test_helpers.rb', line 43 def controller @controller ||= build_controller(Base.test_controller.constantize) end |
#render_inline(component, **args, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/view_component/test_helpers.rb', line 32 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 |
#request ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/view_component/test_helpers.rb', line 47 def request @request ||= begin request = ActionDispatch::TestRequest.create request.session = ActionController::TestSession.new request end end |
#with_controller_class(klass) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/view_component/test_helpers.rb', line 65 def with_controller_class(klass) old_controller = defined?(@controller) && @controller @controller = build_controller(klass) yield ensure @controller = old_controller end |
#with_request_url(path) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/view_component/test_helpers.rb', line 74 def with_request_url(path) old_request_path_parameters = request.path_parameters old_controller = defined?(@controller) && @controller request.path_parameters = Rails.application.routes.recognize_path(path) yield ensure request.path_parameters = old_request_path_parameters @controller = old_controller end |
#with_variant(variant) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/view_component/test_helpers.rb', line 56 def with_variant(variant) old_variants = controller.view_context.lookup_context.variants controller.view_context.lookup_context.variants = variant yield ensure controller.view_context.lookup_context.variants = old_variants end |