Skip to content

Commit 2cf0352

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. (cherry picked from commit 228ca12) # Conflicts: # admin/lib/solidus_admin/testing_support/component_helpers.rb # admin/spec/components/solidus_admin/base_component_spec.rb
1 parent f622a2c commit 2cf0352

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

admin/spec/components/solidus_admin/base_component_spec.rb

Lines changed: 2 additions & 2 deletions
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")

admin/spec/support/solidus_admin/component_helpers.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@ module ComponentHelpers
1111
# "Rendered"
1212
# end
1313
# end
14-
def mock_component(&definition)
15-
Class.new(SolidusAdmin::BaseComponent) do
16-
# ViewComponent will complain if we don't fake a class name:
17-
# @see https://github.com/ViewComponent/view_component/blob/5decd07842c48cbad82527daefa3fe9c65a4226a/lib/view_component/base.rb#L371
18-
def self.name
19-
"Foo"
20-
end
21-
end.tap { |klass| klass.class_eval(&definition) if definition }
14+
def mock_component(class_name = "Foo::Component", &definition)
15+
component_class = stub_const(class_name, Class.new(described_class, &definition))
16+
component_class.new
2217
end
2318
end
2419
end

0 commit comments

Comments
 (0)