Skip to content

Share the view context in tests #2410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 6

## main

* Share the view context in tests to prevent out-of-order rendering issues for certain advanced use-cases, eg. testing instances of Rails' `FormBuilder`.

*Cameron Dutro*

## 4.0.1

* Setup Trusted Publishing to RubyGems to improve software supply chain safety.
Expand Down
16 changes: 13 additions & 3 deletions lib/view_component/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,19 @@ def assert_component_rendered
# @return [Nokogiri::HTML5]
def render_inline(component, **args, &block)
@page = nil
@rendered_content = vc_test_controller.view_context.render(component, args, &block)
@rendered_content = vc_test_view_context.render(component, args, &block)

Nokogiri::HTML5.fragment(@rendered_content)
fragment = Nokogiri::HTML5.fragment(@rendered_content)
@vc_test_view_context = nil
fragment
end

# Returns the view context used to render components in tests. Note that the view context
# is reset after each call to `render_inline`.
#
# @return [ActionView::Base]
def vc_test_view_context
@vc_test_view_context ||= vc_test_controller.view_context
end

# `JSON.parse`-d component output.
Expand Down Expand Up @@ -103,7 +113,7 @@ def render_preview(name, from: __vc_test_helpers_preview_class, params: {})
# ```
def render_in_view_context(...)
@page = nil
@rendered_content = vc_test_controller.view_context.instance_exec(...)
@rendered_content = vc_test_view_context.instance_exec(...)
Nokogiri::HTML5.fragment(@rendered_content)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= @builder.label :foo do %>
<%= content %>
<% end %>
7 changes: 7 additions & 0 deletions test/sandbox/app/components/custom_form_builder_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class CustomFormBuilderComponent < ViewComponent::Base
def initialize(builder:)
@builder = builder
end
end
6 changes: 6 additions & 0 deletions test/sandbox/test/test_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ def test_with_request_url_under_constraint
def test_vc_test_controller_exists
assert vc_test_controller.is_a?(ActionController::Base)
end

def test_vc_test_view_context_is_shared_reference
builder = ActionView::Helpers::FormBuilder.new(nil, Object.new, vc_test_view_context, {})
render_inline(CustomFormBuilderComponent.new(builder: builder)) { "Label content" }
assert_selector("label[for=foo]", text: "Label content")
end
end
Loading