Skip to content

Commit

Permalink
Add test for nested content_tags with data attributes outside of erb
Browse files Browse the repository at this point in the history
The existing content_tag tests document how to use content_tag outside
of erb views with data attributes. But for those who need to use
content_tag with nested content_tags and data attributes outside of
erb views, it can be challenging to find out how to do that.

When I had to do this in a project, it took me longer than I wanted.
Hopefully adding this test with an explicit example of how to achieve that
will others some time.
  • Loading branch information
Stefanni Brasil committed Jul 14, 2023
1 parent c9cdd80 commit 13d8bc1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= content_tag 'div', data: { controller: "read-more", 'read-more-more-text-value': "Read more", 'read-more-less-text-value': "Read less" } do %>
<%= content_tag('p', 'Content text', class: 'content-class', data: { 'test-name': "content" }) %>
<%= content_tag('button', 'Read more', class: 'expand-button', data: { action: 'read-more#toggle'}) %>
<% end %>
5 changes: 5 additions & 0 deletions actionview/test/template/tag_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ def test_content_tag_nested_in_content_tag_in_erb
assert_equal "<p>\n <b>Hello</b>\n</p>", view.render("test/builder_tag_nested_in_content_tag")
end

def test_content_tag_nested_in_content_tag_with_data_attributes_out_of_erb
assert_equal "<div data-controller=\"read-more\" data-read-more-more-text-value=\"Read more\" data-read-more-less-text-value=\"Read less\"\>\n <p class=\"content-class\" data-test-name=\"content\">Content text</p>\n <button class=\"expand-button\" data-action=\"read-more#toggle\">Read more</button>\n</div>",
view.render("test/content_tag_nested_in_content_tag_with_data_attributes_out_of_erb")
end

def test_content_tag_with_escaped_array_class
str = content_tag("p", "limelight", class: ["song", "play>"])
assert_equal "<p class=\"song play&gt;\">limelight</p>", str
Expand Down

0 comments on commit 13d8bc1

Please sign in to comment.