Skip to content

Commit 4411534

Browse files
committed
[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.
1 parent 39c9c02 commit 4411534

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

admin/lib/solidus_admin/testing_support/component_helpers.rb

+3-9
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,9 @@ module ComponentHelpers
1212
# "Rendered"
1313
# end
1414
# end
15-
def mock_component(&definition)
16-
location = caller(1, 1).first
17-
component_class = Class.new(SolidusAdmin::BaseComponent)
18-
# ViewComponent will complain if we don't fake a class name:
19-
# @see https://github.com/ViewComponent/view_component/blob/5decd07842c48cbad82527daefa3fe9c65a4226a/lib/view_component/base.rb#L371
20-
component_class.define_singleton_method(:name) { "Foo" }
21-
component_class.define_singleton_method(:to_s) { "#{name} (#{location})" }
22-
component_class.class_eval(&definition) if definition
23-
component_class
15+
def mock_component(class_name = "Foo::Component", &definition)
16+
component_class = stub_const(class_name, Class.new(described_class, &definition))
17+
component_class.new
2418
end
2519
end
2620
end

admin/spec/components/solidus_admin/base_component_spec.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def call
1010
icon_tag("user-line")
1111
end
12-
end.new
12+
end
1313

1414
render_inline(component)
1515

@@ -42,7 +42,7 @@ def call
4242

4343
describe ".stimulus_id" do
4444
it "returns the stimulus id for the component" do
45-
stub_const("SolidusAdmin::Foo::Bar::Component", Class.new(described_class))
45+
mock_component("SolidusAdmin::Foo::Bar::Component") { erb_template "" }
4646

4747
expect(SolidusAdmin::Foo::Bar::Component.stimulus_id).to eq("foo--bar")
4848
expect(SolidusAdmin::Foo::Bar::Component.new.stimulus_id).to eq("foo--bar")
@@ -55,8 +55,7 @@ def call
5555

5656
allow(Rails.logger).to receive(:debug) { debug_logs << _1 }
5757

58-
component_class = stub_const("Foo::Component", Class.new(described_class){ erb_template "" })
59-
component = component_class.new
58+
component = mock_component { erb_template "" }
6059
render_inline(component)
6160
translation = component.translate("foo.bar.baz")
6261

0 commit comments

Comments
 (0)