Skip to content

Commit ea76dad

Browse files
committed
Add test for multiple template variants
1 parent 5ca5fbc commit ea76dad

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

lib/view_component/compiler.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ def template_errors
129129

130130
invalid_templates =
131131
templates
132-
.group_by { |template| template[:variant] }
133-
.map { |variant, grouped| variant if grouped.length > 1 }
132+
.group_by { |template| template[:variant].present? ? "#{template[:base_name]}+#{template[:variant]}" : template[:base_name] }
133+
.map { |template, grouped| template if grouped.length > 1 }
134134
.compact
135135
.sort
136136

137137
unless invalid_templates.empty?
138138
errors <<
139-
"More than one template found for #{"variant".pluralize(invalid_templates.count)} " \
139+
"More than one template+variant found for #{"template".pluralize(invalid_templates.count)} " \
140140
"#{invalid_templates.map { |v| "'#{v}'" }.to_sentence} in #{component_class}. " \
141141
"There can only be one template file per variant."
142142
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<%# locals: (number:) %>
2+
<ol data-number="<%= number %>">
3+
<% @items.each do |item| %>
4+
<li><%= item %></li>
5+
<% end %>
6+
</ol>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="container">
2+
<%= render_summary_template string: string %>
3+
<%= render_ordered_list_template number: number %>
4+
</div>

test/sandbox/test/rendering_test.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,10 @@ def test_raises_error_when_more_than_one_sidecar_template_is_present
544544
render_inline(TooManySidecarFilesComponent.new)
545545
end
546546

547-
assert_includes error.message, "More than one template found for TooManySidecarFilesComponent."
547+
assert_includes(
548+
error.message,
549+
"More than one template+variant found for template 'too_many_sidecar_files_component' in TooManySidecarFilesComponent."
550+
)
548551
end
549552

550553
def test_raises_error_when_more_than_one_sidecar_template_for_a_variant_is_present
@@ -555,7 +558,9 @@ def test_raises_error_when_more_than_one_sidecar_template_for_a_variant_is_prese
555558

556559
assert_includes(
557560
error.message,
558-
"More than one template found for variants 'test' and 'testing' in TooManySidecarFilesForVariantComponent"
561+
"More than one template+variant found for templates " \
562+
"'too_many_sidecar_files_for_variant_component+test' and 'too_many_sidecar_files_for_variant_component+testing' " \
563+
"in TooManySidecarFilesForVariantComponent"
559564
)
560565
end
561566

@@ -594,7 +599,7 @@ def test_raise_error_when_template_file_and_sidecar_directory_template_exist
594599

595600
assert_includes(
596601
error.message,
597-
"More than one template found for TemplateAndSidecarDirectoryTemplateComponent."
602+
"More than one template+variant found for template 'template_and_sidecar_directory_template_component' in TemplateAndSidecarDirectoryTemplateComponent."
598603
)
599604
end
600605

0 commit comments

Comments
 (0)