From 228ca124326073fb88bf789416a65a13e664cb9b Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Fri, 3 Jan 2025 17:32:34 +0100 Subject: [PATCH 1/2] [SolidusAdmin] Fix mock_component helper The mock component in the ComponentHelpers module was not a real constant, which messes with ViewComponent's expectations about what render_inline is given. Using `stub_const` in the helper allows us to give it an actual name, and view_component > 3.21.0 will work for us. The helper is only used in the base component spec. I could have spent more time giving it an optional block, but this solution is the most straightforward. --- .../testing_support/component_helpers.rb | 12 +++--------- .../components/solidus_admin/base_component_spec.rb | 7 +++---- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/admin/lib/solidus_admin/testing_support/component_helpers.rb b/admin/lib/solidus_admin/testing_support/component_helpers.rb index 58f530eb1fd..f56960561dd 100644 --- a/admin/lib/solidus_admin/testing_support/component_helpers.rb +++ b/admin/lib/solidus_admin/testing_support/component_helpers.rb @@ -12,15 +12,9 @@ module ComponentHelpers # "Rendered" # end # end - def mock_component(&definition) - location = caller(1, 1).first - component_class = Class.new(SolidusAdmin::BaseComponent) - # ViewComponent will complain if we don't fake a class name: - # @see https://github.com/ViewComponent/view_component/blob/5decd07842c48cbad82527daefa3fe9c65a4226a/lib/view_component/base.rb#L371 - component_class.define_singleton_method(:name) { "Foo" } - component_class.define_singleton_method(:to_s) { "#{name} (#{location})" } - component_class.class_eval(&definition) if definition - component_class + def mock_component(class_name = "Foo::Component", &definition) + component_class = stub_const(class_name, Class.new(described_class, &definition)) + component_class.new end end end diff --git a/admin/spec/components/solidus_admin/base_component_spec.rb b/admin/spec/components/solidus_admin/base_component_spec.rb index 2c95f2c9fd3..8ae121dce20 100644 --- a/admin/spec/components/solidus_admin/base_component_spec.rb +++ b/admin/spec/components/solidus_admin/base_component_spec.rb @@ -9,7 +9,7 @@ def call icon_tag("user-line") end - end.new + end render_inline(component) @@ -42,7 +42,7 @@ def call describe ".stimulus_id" do it "returns the stimulus id for the component" do - stub_const("SolidusAdmin::Foo::Bar::Component", Class.new(described_class)) + mock_component("SolidusAdmin::Foo::Bar::Component") { erb_template "" } expect(SolidusAdmin::Foo::Bar::Component.stimulus_id).to eq("foo--bar") expect(SolidusAdmin::Foo::Bar::Component.new.stimulus_id).to eq("foo--bar") @@ -55,8 +55,7 @@ def call allow(Rails.logger).to receive(:debug) { debug_logs << _1 } - component_class = stub_const("Foo::Component", Class.new(described_class){ erb_template "" }) - component = component_class.new + component = mock_component { erb_template "" } render_inline(component) translation = component.translate("foo.bar.baz") From 7bd08604ebd6be5385285dc75bee973bf2097c71 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Fri, 3 Jan 2025 17:01:48 +0100 Subject: [PATCH 2/2] Revert "Merge pull request #6048 from solidusio/lock-view_component" This reverts commit 9aa24a5e63d2fdc2acbf22edd2e19a50f7a0ac61, reversing changes made to 923443688b3dc0a09dbc4a39a22bd81c2994a310. --- admin/solidus_admin.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/solidus_admin.gemspec b/admin/solidus_admin.gemspec index 0fd6ac6aff1..ce348c7b33d 100644 --- a/admin/solidus_admin.gemspec +++ b/admin/solidus_admin.gemspec @@ -34,5 +34,5 @@ Gem::Specification.new do |s| s.add_dependency 'solidus_core', '> 4.2' s.add_dependency 'stimulus-rails', '~> 1.2' s.add_dependency 'turbo-rails', '~> 2.0' - s.add_dependency 'view_component', ['~> 3.9', '< 3.21.0'] + s.add_dependency 'view_component', '~> 3.9' end