Module: ViewComponent::TestHelpers
- Included in:
- TestCase
- Defined in:
- lib/view_component/test_helpers.rb
Instance Attribute Summary collapse
- #rendered_content ⇒ Object readonly
Instance Method Summary collapse
- #build_controller(klass) ⇒ Object
- #controller ⇒ Object
-
#render_in_view_context(&block) ⇒ Object
Execute the given block in the view context.
-
#render_inline(component, **args, &block) ⇒ Nokogiri::HTML
Render a component inline.
-
#rendered_component ⇒ String
Returns the result of a render_inline call.
- #request ⇒ Object
-
#with_controller_class(klass) ⇒ Object
Set the controller to be used while executing the given block, allowing access to controller-specific methods:.
-
#with_request_url(path) ⇒ Object
Set the URL of the current request (such as when using request-dependent path helpers):.
-
#with_variant(variant) ⇒ Object
Set the Action Pack request variant for the given block:.
Instance Attribute Details
#rendered_content ⇒ Object (readonly)
33 34 35 |
# File 'lib/view_component/test_helpers.rb', line 33 def rendered_content @rendered_content end |
Instance Method Details
#build_controller(klass) ⇒ Object
165 166 167 |
# File 'lib/view_component/test_helpers.rb', line 165 def build_controller(klass) klass.new.tap { |c| c.request = request }.extend(Rails.application.routes.url_helpers) end |
#controller ⇒ Object
84 85 86 |
# File 'lib/view_component/test_helpers.rb', line 84 def controller @controller ||= build_controller(Base.test_controller.constantize) end |
#render_in_view_context(&block) ⇒ Object
Execute the given block in the view context. Internally sets page to be a Capybara::Node::Simple, allowing for Capybara assertions to be used:
“‘ruby render_in_view_context do
render(MyComponent.new)
end
assert_text(“Hello, World!”) “‘
78 79 80 81 |
# File 'lib/view_component/test_helpers.rb', line 78 def render_in_view_context(&block) @rendered_content = controller.view_context.instance_exec(&block) Nokogiri::HTML.fragment(@rendered_content) end |
#render_inline(component, **args, &block) ⇒ Nokogiri::HTML
Render a component inline. Internally sets page to be a Capybara::Node::Simple, allowing for Capybara assertions to be used:
“‘ruby render_inline(MyComponent.new) assert_text(“Hello, World!”) “`
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/view_component/test_helpers.rb', line 57 def render_inline(component, **args, &block) @rendered_content = 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_content) end |
#rendered_component ⇒ String
Returns the result of a render_inline call.
38 39 40 41 42 43 44 45 |
# File 'lib/view_component/test_helpers.rb', line 38 def rendered_component ViewComponent::Deprecation.warn( "`rendered_component` is deprecated and will be removed in v3.0.0. " \ "Use `page` instead." ) rendered_content end |
#request ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/view_component/test_helpers.rb', line 89 def request @request ||= begin request = ActionDispatch::TestRequest.create request.session = ActionController::TestSession.new request end end |
#with_controller_class(klass) ⇒ Object
Set the controller to be used while executing the given block, allowing access to controller-specific methods:
“‘ruby with_controller_class(UsersController) do
render_inline(MyComponent.new)
end “‘
126 127 128 129 130 131 132 133 |
# File 'lib/view_component/test_helpers.rb', line 126 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
Set the URL of the current request (such as when using request-dependent path helpers):
“‘ruby with_request_url(“/users/42”) do
render_inline(MyComponent.new)
end “‘
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/view_component/test_helpers.rb', line 144 def with_request_url(path) old_request_path_info = request.path_info old_request_path_parameters = request.path_parameters old_request_query_parameters = request.query_parameters old_request_query_string = request.query_string old_controller = defined?(@controller) && @controller request.path_info = path request.path_parameters = Rails.application.routes.recognize_path(path) request.set_header("action_dispatch.request.query_parameters", Rack::Utils.parse_nested_query(path.split("?")[1])) request.set_header(Rack::QUERY_STRING, path.split("?")[1]) yield ensure request.path_info = old_request_path_info request.path_parameters = old_request_path_parameters request.set_header("action_dispatch.request.query_parameters", old_request_query_parameters) request.set_header(Rack::QUERY_STRING, old_request_query_string) @controller = old_controller end |
#with_variant(variant) ⇒ Object
Set the Action Pack request variant for the given block:
“‘ruby with_variant(:phone) do
render_inline(MyComponent.new)
end “‘
107 108 109 110 111 112 113 114 |
# File 'lib/view_component/test_helpers.rb', line 107 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 |