diff --git a/lib/view_component/test_helpers.rb b/lib/view_component/test_helpers.rb index 1cb733451..c8b952aff 100644 --- a/lib/view_component/test_helpers.rb +++ b/lib/view_component/test_helpers.rb @@ -75,7 +75,7 @@ def render_inline(component, **args, &block) # @param params [Hash] Parameters to be passed to the preview. # @return [Nokogiri::HTML] def render_preview(name, from: __vc_test_helpers_preview_class, params: {}) - previews_controller = __vc_test_helpers_build_controller(Rails.application.config.view_component.preview_controller.constantize) + previews_controller = __vc_render_preview_controller # From what I can tell, it's not possible to overwrite all request parameters # at once, so we set them individually here. @@ -103,9 +103,9 @@ def render_preview(name, from: __vc_test_helpers_preview_class, params: {}) # # assert_text("Hello, World!") # ``` - def render_in_view_context(*args, &block) + def render_in_view_context(*, &block) @page = nil - @rendered_content = vc_test_controller.view_context.instance_exec(*args, &block) + @rendered_content = vc_test_controller.view_context.instance_exec(*, &block) Nokogiri::HTML.fragment(@rendered_content) end ruby2_keywords(:render_in_view_context) if respond_to?(:ruby2_keywords, true) @@ -225,6 +225,11 @@ def vc_test_request end end + def before_setup + @request = __vc_render_preview_controller.request + super + end + # Note: We prefix private methods here to prevent collisions in consumer's tests. private @@ -244,5 +249,9 @@ def __vc_test_helpers_preview_class rescue NameError raise NameError, "`render_preview` expected to find #{result}, but it does not exist." end + + def __vc_render_preview_controller + @vc_render_preview_controller ||= __vc_test_helpers_build_controller(Rails.application.config.view_component.preview_controller.constantize) + end end end