Skip to content

Commit

Permalink
Merge pull request rails#50584 from seanpdoyle/action-view-rendered-h…
Browse files Browse the repository at this point in the history
…tml-parser

Parse `ActionView::TestCase#rendered` as DocumentFragment
  • Loading branch information
rafaelfranca authored Jan 4, 2024
2 parents bed0b44 + bec8037 commit 64c9360
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 4 additions & 0 deletions actionview/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Parse `ActionView::TestCase#rendered` HTML content as `Nokogiri::XML::DocumentFragment` instead of `Nokogiri::XML::Document`

*Sean Doyle*

* Rename `ActionView::TestCase::Behavior::{Content,RenderedViewContent}`

*Sean Doyle*
Expand Down
2 changes: 1 addition & 1 deletion actionview/lib/action_view/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def include_helper_modules!

setup :setup_with_controller

register_parser :html, -> rendered { Rails::Dom::Testing.html_document.parse(rendered).root }
register_parser :html, -> rendered { Rails::Dom::Testing.html_document_fragment.parse(rendered) }
register_parser :json, -> rendered { JSON.parse(rendered, object_class: ActiveSupport::HashWithIndifferentAccess) }

ActiveSupport.run_load_hooks(:action_view_test_case, self)
Expand Down
6 changes: 4 additions & 2 deletions actionview/test/template/test_case_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,13 @@ class RenderedViewContentTest < ActionView::TestCase
end

class HTMLParserTest < ActionView::TestCase
test "rendered.html is a Nokogiri::XML::Element" do
test "rendered.html is a Nokogiri::XML::DocumentFragment" do
developer = DeveloperStruct.new("Eloy")

render "developers/developer", developer: developer

assert_kind_of Nokogiri::XML::Element, rendered.html
assert_kind_of Nokogiri::XML::DocumentFragment, rendered.html
assert_equal rendered.to_s, rendered.html.to_s
assert_equal developer.name, document_root_element.text
end

Expand All @@ -425,6 +426,7 @@ class JSONParserTest < ActionView::TestCase
render formats: :json, partial: "developers/developer", locals: { developer: developer }

assert_kind_of ActiveSupport::HashWithIndifferentAccess, rendered.json
assert_equal rendered.to_s, rendered.json.to_json
assert_equal developer.name, rendered.json[:name]
end
end
Expand Down

0 comments on commit 64c9360

Please sign in to comment.