Module: ViewComponent::TestHelpers
- Included in:
- SystemTestHelpers, TestCase
- Defined in:
- lib/view_component/test_helpers.rb
Instance Attribute Summary collapse
-
#rendered_content ⇒ ActionView::OutputBuffer
readonly
Returns the result of a render_inline call.
Instance Method Summary collapse
-
#render_in_view_context ⇒ Object
Execute the given block in the view context (using
instance_exec). -
#render_inline(component, **args, &block) ⇒ Nokogiri::HTML5
Render a component inline.
-
#render_preview(name, from: __vc_test_helpers_preview_class, params: {}) ⇒ Nokogiri::HTML5
Render a preview inline.
-
#rendered_json ⇒ Object
JSON.parse-d component output. -
#vc_test_controller ⇒ ActionController::Base
Access the controller used by
render_inline:. -
#vc_test_controller_class ⇒ Object
Set the controller used by
render_inline:. -
#vc_test_request ⇒ ActionDispatch::TestRequest
Access the request used by
render_inline:. -
#vc_test_view_context ⇒ ActionView::Base
Returns the view context used to render components in tests.
-
#with_controller_class(klass) ⇒ Object
Set the controller to be used while executing the given block, allowing access to controller-specific methods:.
-
#with_format(*formats) ⇒ Object
Set format of the current request.
-
#with_request_url(full_path, host: nil, method: nil, protocol: nil) ⇒ Object
Set the URL of the current request (such as when using request-dependent path helpers):.
-
#with_variant(*variants) ⇒ Object
Set the Action Pack request variant for the given block:.
Instance Attribute Details
#rendered_content ⇒ ActionView::OutputBuffer (readonly)
Returns the result of a render_inline call.
27 28 29 |
# File 'lib/view_component/test_helpers.rb', line 27 def rendered_content @rendered_content end |
Instance Method Details
#render_in_view_context ⇒ 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.
render_in_view_context(arg1, arg2: nil) do |arg1, arg2:|
render(MyComponent.new(arg1, arg2))
end
assert_text("Hello, World!")
114 115 116 117 118 |
# File 'lib/view_component/test_helpers.rb', line 114 def render_in_view_context(...) @page = nil @rendered_content = vc_test_view_context.instance_exec(...) Nokogiri::HTML5.fragment(@rendered_content) end |
#render_inline(component, **args, &block) ⇒ Nokogiri::HTML5
Render a component inline. Internally sets page to be a Capybara::Node::Simple,
allowing for Capybara assertions to be used:
render_inline(MyComponent.new)
assert_text("Hello, World!")
39 40 41 42 43 44 45 46 |
# File 'lib/view_component/test_helpers.rb', line 39 def render_inline(component, **args, &block) @page = nil @rendered_content = vc_test_view_context.render(component, args, &block) fragment = Nokogiri::HTML5.fragment(@rendered_content, context: "template") @vc_test_view_context = nil fragment end |
#render_preview(name, from: __vc_test_helpers_preview_class, params: {}) ⇒ Nokogiri::HTML5
Render a preview inline. Internally sets page to be a Capybara::Node::Simple,
allowing for Capybara assertions to be used:
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.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/view_component/test_helpers.rb', line 85 def render_preview(name, from: __vc_test_helpers_preview_class, params: {}) previews_controller = __vc_test_helpers_build_controller(Rails.application.config.view_component.previews.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::HTML5.fragment(@rendered_content) end |
#rendered_json ⇒ Object
JSON.parse-d component output.
render_inline(MyJsonComponent.new)
assert_equal(rendered_json["hello"], "world")
62 63 64 |
# File 'lib/view_component/test_helpers.rb', line 62 def rendered_json JSON.parse(rendered_content) end |
#vc_test_controller ⇒ ActionController::Base
Access the controller used by render_inline:
test "logged out user sees login link" do
vc_test_controller.expects(:logged_in?).at_least_once.returns(false)
render_inline(LoginComponent.new)
assert_selector("[aria-label='You must be signed in']")
end
257 258 259 |
# File 'lib/view_component/test_helpers.rb', line 257 def vc_test_controller @vc_test_controller ||= __vc_test_helpers_build_controller(vc_test_controller_class) end |
#vc_test_controller_class ⇒ Object
Set the controller used by render_inline:
def vc_test_controller_class
MyTestController
end
268 269 270 271 272 |
# File 'lib/view_component/test_helpers.rb', line 268 def vc_test_controller_class return @__vc_test_controller_class if defined?(@__vc_test_controller_class) defined?(ApplicationController) ? ApplicationController : ActionController::Base end |
#vc_test_request ⇒ ActionDispatch::TestRequest
Access the request used by render_inline:
test "component does not render in Firefox" do
request.env["HTTP_USER_AGENT"] = "Mozilla/5.0"
render_inline(NoFirefoxComponent.new)
refute_component_rendered
end
285 286 287 288 289 290 291 292 293 294 |
# File 'lib/view_component/test_helpers.rb', line 285 def vc_test_request require "action_controller/test_case" @vc_test_request ||= begin out = ActionDispatch::TestRequest.create out.session = ActionController::TestSession.new out end end |
#vc_test_view_context ⇒ ActionView::Base
Returns the view context used to render components in tests. Note that the view context
is reset after each call to render_inline.
52 53 54 |
# File 'lib/view_component/test_helpers.rb', line 52 def vc_test_view_context @vc_test_view_context ||= vc_test_controller.view_context end |
#with_controller_class(klass) ⇒ Object
Set the controller to be used while executing the given block, allowing access to controller-specific methods:
with_controller_class(UsersController) do
render_inline(MyComponent.new)
end
148 149 150 151 152 153 154 155 |
# File 'lib/view_component/test_helpers.rb', line 148 def with_controller_class(klass) old_controller = defined?(@vc_test_controller) && @vc_test_controller @vc_test_controller = __vc_test_helpers_build_controller(klass) yield ensure @vc_test_controller = old_controller end |
#with_format(*formats) ⇒ Object
Set format of the current request
with_format(:json) do
render_inline(MyComponent.new)
end
166 167 168 169 170 171 172 173 |
# File 'lib/view_component/test_helpers.rb', line 166 def with_format(*formats) old_formats = vc_test_controller.view_context.lookup_context.formats vc_test_controller.view_context.lookup_context.formats = formats yield ensure vc_test_controller.view_context.lookup_context.formats = old_formats end |
#with_request_url(full_path, host: nil, method: nil, protocol: nil) ⇒ Object
Set the URL of the current request (such as when using request-dependent path helpers):
with_request_url("/users/42") do
render_inline(MyComponent.new)
end
To use a specific host, pass the host param:
with_request_url("/users/42", host: "app.example.com") do
render_inline(MyComponent.new)
end
To specify a request method, pass the method param:
with_request_url("/users/42", method: "POST") do
render_inline(MyComponent.new)
end
To specify a protocol, pass the protocol param:
with_request_url("/users/42", protocol: :https) do
render_inline(MyComponent.new)
end
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/view_component/test_helpers.rb', line 211 def with_request_url(full_path, host: nil, method: nil, protocol: nil) old_request_host = vc_test_request.host old_request_method = vc_test_request.request_method old_request_path_info = vc_test_request.path_info old_request_path_parameters = vc_test_request.path_parameters old_request_query_parameters = vc_test_request.query_parameters old_request_query_string = vc_test_request.query_string old_request_format = vc_test_request.format.symbol old_request_scheme = vc_test_request.scheme old_controller = defined?(@vc_test_controller) && @vc_test_controller path, query = full_path.split("?", 2) vc_test_request.instance_variable_set(:@fullpath, full_path) vc_test_request.instance_variable_set(:@original_fullpath, full_path) vc_test_request.host = host if host vc_test_request.request_method = method if method vc_test_request.set_header(Rack::RACK_URL_SCHEME, protocol.to_s) if protocol vc_test_request.path_info = path vc_test_request.path_parameters = Rails.application.routes.recognize_path_with_request(vc_test_request, path, {}) vc_test_request.set_header("action_dispatch.request.query_parameters", Rack::Utils.parse_nested_query(query).with_indifferent_access) vc_test_request.set_header(Rack::QUERY_STRING, query) yield ensure vc_test_request.host = old_request_host vc_test_request.request_method = old_request_method vc_test_request.set_header(Rack::RACK_URL_SCHEME, old_request_scheme) vc_test_request.path_info = old_request_path_info vc_test_request.path_parameters = old_request_path_parameters vc_test_request.set_header("action_dispatch.request.query_parameters", old_request_query_parameters) vc_test_request.set_header(Rack::QUERY_STRING, old_request_query_string) vc_test_request.format = old_request_format @vc_test_controller = old_controller end |
#with_variant(*variants) ⇒ Object
Set the Action Pack request variant for the given block:
with_variant(:phone) do
render_inline(MyComponent.new)
end
129 130 131 132 133 134 135 136 |
# File 'lib/view_component/test_helpers.rb', line 129 def with_variant(*variants) old_variants = vc_test_controller.view_context.lookup_context.variants vc_test_controller.view_context.lookup_context.variants += variants yield ensure vc_test_controller.view_context.lookup_context.variants = old_variants end |