From ffcddbe82707970b8a71cc2ac4c49d54f8cda296 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Fri, 3 Jan 2025 17:32:34 +0100 Subject: [PATCH] [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. (cherry picked from commit 228ca124326073fb88bf789416a65a13e664cb9b) # Conflicts: # admin/lib/solidus_admin/testing_support/component_helpers.rb # admin/spec/components/solidus_admin/base_component_spec.rb --- .../components/solidus_admin/base_component_spec.rb | 4 ++-- admin/spec/support/solidus_admin/component_helpers.rb | 11 +++-------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/admin/spec/components/solidus_admin/base_component_spec.rb b/admin/spec/components/solidus_admin/base_component_spec.rb index dc2ab173aa9..17f74588e33 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") diff --git a/admin/spec/support/solidus_admin/component_helpers.rb b/admin/spec/support/solidus_admin/component_helpers.rb index e68d7d37c9d..9ac2607d223 100644 --- a/admin/spec/support/solidus_admin/component_helpers.rb +++ b/admin/spec/support/solidus_admin/component_helpers.rb @@ -11,14 +11,9 @@ module ComponentHelpers # "Rendered" # end # end - def mock_component(&definition) - Class.new(SolidusAdmin::BaseComponent) do - # 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 - def self.name - "Foo" - end - end.tap { |klass| klass.class_eval(&definition) if definition } + 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