Module: ViewComponent::TestHelpers
- Included in:
- SystemTestHelpers, 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(*args, &block) ⇒ Object
Execute the given block in the view context (using ‘instance_exec`).
-
#render_inline(component, **args, &block) ⇒ Nokogiri::HTML
Render a component inline.
-
#render_preview(name, from: preview_class, params: {}) ⇒ Nokogiri::HTML
Render a preview 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)
32 33 34 |
# File 'lib/view_component/test_helpers.rb', line 32 def rendered_content @rendered_content end |
Instance Method Details
#build_controller(klass) ⇒ Object
203 204 205 |
# File 'lib/view_component/test_helpers.rb', line 203 def build_controller(klass) klass.new.tap { |c| c.request = request }.extend(Rails.application.routes.url_helpers) end |
#controller ⇒ Object
121 122 123 |
# File 'lib/view_component/test_helpers.rb', line 121 def controller @controller ||= build_controller(Base.test_controller.constantize) end |
#render_in_view_context(*args, &block) ⇒ Object
Execute the given block in the view context (using ‘instance_exec`). Internally sets `page` to be a `Capybara::Node::Simple`, allowing for Capybara assertions to be used. All arguments are forwarded to the block.
“‘ruby render_in_view_context(arg1, arg2:) do |arg1, arg2:|
render(MyComponent.new(arg1, arg2))
end
assert_text(“Hello, World!”) “‘
113 114 115 116 117 |
# File 'lib/view_component/test_helpers.rb', line 113 def render_in_view_context(*args, &block) @page = nil @rendered_content = controller.view_context.instance_exec(*args, &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!”) “`
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/view_component/test_helpers.rb', line 53 def render_inline(component, **args, &block) @page = nil @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 |
#render_preview(name, from: preview_class, params: {}) ⇒ Nokogiri::HTML
Render a preview inline. Internally sets ‘page` to be a `Capybara::Node::Simple`, allowing for Capybara assertions to be used:
“‘ruby render_preview(:default) assert_text(“Hello, World!”) “`
Note: ‘#rendered_preview` expects a preview to be defined with the same class name as the calling test, but with `Test` replaced with `Preview`:
MyComponentTest -> MyComponentPreview etc.
In RSpec, ‘Preview` is appended to `described_class`.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/view_component/test_helpers.rb', line 84 def render_preview(name, from: preview_class, params: {}) previews_controller = 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 |
#rendered_component ⇒ String
Returns the result of a render_inline call.
37 38 39 40 41 |
# File 'lib/view_component/test_helpers.rb', line 37 def rendered_component ViewComponent::Deprecation.deprecation_warning("`rendered_component`", :"`page`") rendered_content end |
#request ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/view_component/test_helpers.rb', line 126 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 “‘
163 164 165 166 167 168 169 170 |
# File 'lib/view_component/test_helpers.rb', line 163 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 “‘
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/view_component/test_helpers.rb', line 181 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 path, query = path.split("?", 2) request.path_info = path request.path_parameters = Rails.application.routes.recognize_path_with_request(request, path, {}) request.set_header("action_dispatch.request.query_parameters", Rack::Utils.parse_nested_query(query)) request.set_header(Rack::QUERY_STRING, query) 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 “‘
144 145 146 147 148 149 150 151 |
# File 'lib/view_component/test_helpers.rb', line 144 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 |