Skip to content

Commit 6a35673

Browse files
Add failing spec for mocked component without template (#2191)
* Add failing spec for mocked component without template This scenario results in a syntax error, but one would probably want something like "No template found". * fix failing test * allocations * rewrite change to fix underlying issue --------- Co-authored-by: Joel Hawksley <[email protected]> Co-authored-by: Joel Hawksley <[email protected]>
1 parent b6aa218 commit 6a35673

File tree

5 files changed

+90
-69
lines changed

5 files changed

+90
-69
lines changed

.tool-versions

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ruby 3.4.1
1+
ruby 3.4.2

Gemfile.lock

+67-65
Original file line numberDiff line numberDiff line change
@@ -9,65 +9,65 @@ PATH
99
GEM
1010
remote: https://rubygems.org/
1111
specs:
12-
actioncable (8.0.1)
13-
actionpack (= 8.0.1)
14-
activesupport (= 8.0.1)
12+
actioncable (8.0.2)
13+
actionpack (= 8.0.2)
14+
activesupport (= 8.0.2)
1515
nio4r (~> 2.0)
1616
websocket-driver (>= 0.6.1)
1717
zeitwerk (~> 2.6)
18-
actionmailbox (8.0.1)
19-
actionpack (= 8.0.1)
20-
activejob (= 8.0.1)
21-
activerecord (= 8.0.1)
22-
activestorage (= 8.0.1)
23-
activesupport (= 8.0.1)
18+
actionmailbox (8.0.2)
19+
actionpack (= 8.0.2)
20+
activejob (= 8.0.2)
21+
activerecord (= 8.0.2)
22+
activestorage (= 8.0.2)
23+
activesupport (= 8.0.2)
2424
mail (>= 2.8.0)
25-
actionmailer (8.0.1)
26-
actionpack (= 8.0.1)
27-
actionview (= 8.0.1)
28-
activejob (= 8.0.1)
29-
activesupport (= 8.0.1)
25+
actionmailer (8.0.2)
26+
actionpack (= 8.0.2)
27+
actionview (= 8.0.2)
28+
activejob (= 8.0.2)
29+
activesupport (= 8.0.2)
3030
mail (>= 2.8.0)
3131
rails-dom-testing (~> 2.2)
32-
actionpack (8.0.1)
33-
actionview (= 8.0.1)
34-
activesupport (= 8.0.1)
32+
actionpack (8.0.2)
33+
actionview (= 8.0.2)
34+
activesupport (= 8.0.2)
3535
nokogiri (>= 1.8.5)
3636
rack (>= 2.2.4)
3737
rack-session (>= 1.0.1)
3838
rack-test (>= 0.6.3)
3939
rails-dom-testing (~> 2.2)
4040
rails-html-sanitizer (~> 1.6)
4141
useragent (~> 0.16)
42-
actiontext (8.0.1)
43-
actionpack (= 8.0.1)
44-
activerecord (= 8.0.1)
45-
activestorage (= 8.0.1)
46-
activesupport (= 8.0.1)
42+
actiontext (8.0.2)
43+
actionpack (= 8.0.2)
44+
activerecord (= 8.0.2)
45+
activestorage (= 8.0.2)
46+
activesupport (= 8.0.2)
4747
globalid (>= 0.6.0)
4848
nokogiri (>= 1.8.5)
49-
actionview (8.0.1)
50-
activesupport (= 8.0.1)
49+
actionview (8.0.2)
50+
activesupport (= 8.0.2)
5151
builder (~> 3.1)
5252
erubi (~> 1.11)
5353
rails-dom-testing (~> 2.2)
5454
rails-html-sanitizer (~> 1.6)
55-
activejob (8.0.1)
56-
activesupport (= 8.0.1)
55+
activejob (8.0.2)
56+
activesupport (= 8.0.2)
5757
globalid (>= 0.3.6)
58-
activemodel (8.0.1)
59-
activesupport (= 8.0.1)
60-
activerecord (8.0.1)
61-
activemodel (= 8.0.1)
62-
activesupport (= 8.0.1)
58+
activemodel (8.0.2)
59+
activesupport (= 8.0.2)
60+
activerecord (8.0.2)
61+
activemodel (= 8.0.2)
62+
activesupport (= 8.0.2)
6363
timeout (>= 0.4.0)
64-
activestorage (8.0.1)
65-
actionpack (= 8.0.1)
66-
activejob (= 8.0.1)
67-
activerecord (= 8.0.1)
68-
activesupport (= 8.0.1)
64+
activestorage (8.0.2)
65+
actionpack (= 8.0.2)
66+
activejob (= 8.0.2)
67+
activerecord (= 8.0.2)
68+
activesupport (= 8.0.2)
6969
marcel (~> 1.0)
70-
activesupport (8.0.1)
70+
activesupport (8.0.2)
7171
base64
7272
benchmark (>= 0.3)
7373
bigdecimal
@@ -153,7 +153,7 @@ GEM
153153
jbuilder (2.13.0)
154154
actionview (>= 5.0.0)
155155
activesupport (>= 5.0.0)
156-
json (2.10.1)
156+
json (2.10.2)
157157
language_server-protocol (3.17.0.4)
158158
lint_roller (1.1.0)
159159
logger (1.6.6)
@@ -173,7 +173,7 @@ GEM
173173
method_source (1.1.0)
174174
mini_mime (1.1.5)
175175
mini_portile2 (2.8.8)
176-
minitest (5.25.4)
176+
minitest (5.25.5)
177177
mutex_m (0.3.0)
178178
net-imap (0.5.6)
179179
date
@@ -218,30 +218,30 @@ GEM
218218
rack (>= 1.3)
219219
rackup (2.2.1)
220220
rack (>= 3)
221-
rails (8.0.1)
222-
actioncable (= 8.0.1)
223-
actionmailbox (= 8.0.1)
224-
actionmailer (= 8.0.1)
225-
actionpack (= 8.0.1)
226-
actiontext (= 8.0.1)
227-
actionview (= 8.0.1)
228-
activejob (= 8.0.1)
229-
activemodel (= 8.0.1)
230-
activerecord (= 8.0.1)
231-
activestorage (= 8.0.1)
232-
activesupport (= 8.0.1)
221+
rails (8.0.2)
222+
actioncable (= 8.0.2)
223+
actionmailbox (= 8.0.2)
224+
actionmailer (= 8.0.2)
225+
actionpack (= 8.0.2)
226+
actiontext (= 8.0.2)
227+
actionview (= 8.0.2)
228+
activejob (= 8.0.2)
229+
activemodel (= 8.0.2)
230+
activerecord (= 8.0.2)
231+
activestorage (= 8.0.2)
232+
activesupport (= 8.0.2)
233233
bundler (>= 1.15.0)
234-
railties (= 8.0.1)
234+
railties (= 8.0.2)
235235
rails-dom-testing (2.2.0)
236236
activesupport (>= 5.0.0)
237237
minitest
238238
nokogiri (>= 1.6)
239239
rails-html-sanitizer (1.6.2)
240240
loofah (~> 2.21)
241241
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
242-
railties (8.0.1)
243-
actionpack (= 8.0.1)
244-
activesupport (= 8.0.1)
242+
railties (8.0.2)
243+
actionpack (= 8.0.2)
244+
activesupport (= 8.0.2)
245245
irb (~> 1.13)
246246
rackup (>= 1.0.0)
247247
rake (>= 12.2)
@@ -272,9 +272,10 @@ GEM
272272
rspec-mocks (~> 3.10)
273273
rspec-support (~> 3.10)
274274
rspec-support (3.13.2)
275-
rubocop (1.71.2)
275+
rubocop (1.73.2)
276276
json (~> 2.3)
277-
language_server-protocol (>= 3.17.0)
277+
language_server-protocol (~> 3.17.0.2)
278+
lint_roller (~> 1.1.0)
278279
parallel (~> 1.10)
279280
parser (>= 3.3.0.2)
280281
rainbow (>= 2.2.2, < 4.0)
@@ -286,9 +287,10 @@ GEM
286287
parser (>= 3.3.1.0)
287288
rubocop-md (1.2.4)
288289
rubocop (>= 1.45)
289-
rubocop-performance (1.23.1)
290-
rubocop (>= 1.48.1, < 2.0)
291-
rubocop-ast (>= 1.31.1, < 2.0)
290+
rubocop-performance (1.24.0)
291+
lint_roller (~> 1.1)
292+
rubocop (>= 1.72.1, < 2.0)
293+
rubocop-ast (>= 1.38.0, < 2.0)
292294
ruby-progressbar (1.13.0)
293295
rubyzip (2.4.1)
294296
securerandom (0.4.1)
@@ -317,18 +319,18 @@ GEM
317319
actionpack (>= 5.2)
318320
activesupport (>= 5.2)
319321
sprockets (>= 3.0.0)
320-
standard (1.45.0)
322+
standard (1.47.0)
321323
language_server-protocol (~> 3.17.0.2)
322324
lint_roller (~> 1.0)
323-
rubocop (~> 1.71.0)
325+
rubocop (~> 1.73.0)
324326
standard-custom (~> 1.0.0)
325-
standard-performance (~> 1.6)
327+
standard-performance (~> 1.7)
326328
standard-custom (1.0.2)
327329
lint_roller (~> 1.0)
328330
rubocop (~> 1.50)
329-
standard-performance (1.6.0)
331+
standard-performance (1.7.0)
330332
lint_roller (~> 1.1)
331-
rubocop-performance (~> 1.23.0)
333+
rubocop-performance (~> 1.24.0)
332334
stringio (3.1.5)
333335
temple (0.10.3)
334336
terminal-table (4.0.0)

docs/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 5
1010

1111
## main
1212

13+
* Improve handling of malformed component edge case when mocking components in tests.
14+
15+
*Martin Meyerhoff*, *Joel Hawksley*
16+
1317
* Add Content Harmony & Learn To Be to list of companies using ViewComponent.
1418

1519
*Kane Jamison*

lib/view_component/template.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def safe_method_name_call
6363
return safe_method_name unless inline_call?
6464

6565
"maybe_escape_html(#{safe_method_name}) " \
66-
"{ Kernel.warn('WARNING: The #{@component} component rendered HTML-unsafe output. " \
67-
"The output will be automatically escaped, but you may want to investigate.') } "
66+
"{ Kernel.warn(\"WARNING: The #{@component} component rendered HTML-unsafe output. " \
67+
"The output will be automatically escaped, but you may want to investigate.\") } "
6868
end
6969

7070
def requires_compiled_superclass?

test/sandbox/test/rendering_test.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_render_inline_allocations
1717

1818
allocations = (Rails.version.to_f >= 8.0) ?
1919
{"3.5.0" => 115, "3.4.2" => 117, "3.3.7" => 129} :
20-
{"3.3.7" => 120, "3.3.0" => 120, "3.2.7" => 118, "3.1.6" => 118, "3.0.7" => 127}
20+
{"3.3.7" => 120, "3.3.0" => 132, "3.2.7" => 118, "3.1.6" => 118, "3.0.7" => 127}
2121

2222
assert_allocations(**allocations) do
2323
render_inline(MyComponent.new)
@@ -1240,4 +1240,19 @@ def test_request_param
12401240

12411241
assert_text("foo")
12421242
end
1243+
1244+
# In https://github.com/ViewComponent/view_component/issues/2187,
1245+
# the Solidus test suite built mocked components by hand, resulting
1246+
# in a difficult-to-debug error. While this test case is quite narrow,
1247+
# it isolates the unintentional error masking we were doing.
1248+
def test_render_anonymous_component_without_template
1249+
location = caller(1, 1).first
1250+
mock_component = Class.new(MyComponent)
1251+
mock_component.define_singleton_method(:name) { "Foo" }
1252+
mock_component.define_singleton_method(:to_s) { "#{name} (#{location})" }
1253+
1254+
assert_nothing_raised do
1255+
render_inline(mock_component.new)
1256+
end
1257+
end
12431258
end

0 commit comments

Comments
 (0)