# frozen_string_literal: true require "test_helper" class DomAssertionsTest < ActiveSupport::TestCase Assertion = Minitest::Assertion include Rails::Dom::Testing::Assertions::DomAssertions def test_responds_to_assert_dom_equal assert respond_to?(:assert_dom_equal) end def test_dom_equal html = "" assert_dom_equal(html, html.dup) end def test_equal_doms_with_different_order_attributes attributes = %{} reverse_attributes = %{} assert_dom_equal(attributes, reverse_attributes) end def test_dom_not_equal assert_dom_not_equal("", "") end def test_unequal_doms_attributes_with_different_order_and_values attributes = %{} reverse_attributes = %{} assert_dom_not_equal(attributes, reverse_attributes) end def test_custom_message_is_used_in_failures message = "This is my message." e = assert_raises(Assertion) do assert_dom_equal("", "", message) end assert_equal e.message, message end def test_unequal_dom_attributes_in_children assert_dom_not_equal( %{}, %{} ) end def test_dom_equal_with_whitespace_strict canonical = %{hello world} assert_dom_not_equal(canonical, %{\nhello\n world}, strict: true) assert_dom_not_equal(canonical, %{ \n \n hello world}, strict: true) assert_dom_not_equal(canonical, %{\n\thello world}, strict: true) assert_dom_equal(canonical, %{hello world}, strict: true) end def test_dom_equal_with_whitespace canonical = %{hello world} assert_dom_equal(canonical, %{\nhello\n world}) assert_dom_equal(canonical, %{\nhello \nworld}) assert_dom_equal(canonical, %{ \n \n hello world}) assert_dom_equal(canonical, %{ \n hello world}) assert_dom_equal(canonical, %{ \n hello world\n\n}) assert_dom_equal(canonical, %{\n\thello world}) assert_dom_equal(canonical, %{\n\thello \n\tworld}) end def test_dom_equal_with_attribute_whitespace canonical = %(
) assert_dom_equal(canonical, %(
)) assert_dom_not_equal(canonical, %(
)) end def test_dom_equal_with_indentation canonical = %{hello cruel world} assert_dom_equal(canonical, <<-HTML) hello cruel world HTML assert_dom_equal(canonical, <<-HTML) hello cruel world HTML assert_dom_equal(canonical, <<-HTML) hello cruel world HTML end def test_dom_equal_with_surrounding_whitespace canonical = %{

Lorem ipsum dolor

sit amet, consectetur adipiscing elit

} assert_dom_equal(canonical, <<-HTML)

Lorem ipsum dolor

sit amet, consectetur adipiscing elit

HTML end def test_dom_not_equal_with_interior_whitespace with_space = %{hello world} without_space = %{helloworld} assert_dom_not_equal(with_space, without_space) end end class DomAssertionsHtmlParserSelectionTest < ActiveSupport::TestCase include DomTestingHelpers include Rails::Dom::Testing::Assertions::DomAssertions def setup super # https://html.spec.whatwg.org/multipage/parsing.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser # we use these results to assert that we're invoking the expected parser. @input = "

12345

" @html4_result = jruby? ? "

12345

" : "

12345

" @html5_result = jruby? ? nil : "

12345

" end test "default value is html4" do assert_equal(:html4, Rails::Dom::Testing.default_html_version) end test "default html4, no version specified" do with_default_html_version(:html4) do assert_dom_equal(@html4_result, @input) assert_dom_not_equal(@html5_result, @input) end end test "default html4, html4 specified" do with_default_html_version(:html4) do assert_dom_equal(@html4_result, @input, html_version: :html4) assert_dom_not_equal(@html5_result, @input, html_version: :html4) end end test "default html4, html5 specified" do skip("html5 is not supported") unless Rails::Dom::Testing.html5_support? with_default_html_version(:html4) do assert_dom_equal(@html5_result, @input, html_version: :html5) assert_dom_not_equal(@html4_result, @input, html_version: :html5) end end test "default html5, no version specified" do skip("html5 is not supported") unless Rails::Dom::Testing.html5_support? with_default_html_version(:html5) do assert_dom_equal(@html5_result, @input) assert_dom_not_equal(@html4_result, @input) end end test "default html5, html4 specified" do with_default_html_version(:html5) do assert_dom_equal(@html4_result, @input, html_version: :html4) assert_dom_not_equal(@html5_result, @input, html_version: :html4) end end test "default html5, html5 specified" do skip("html5 is not supported") unless Rails::Dom::Testing.html5_support? with_default_html_version(:html5) do assert_dom_equal(@html5_result, @input, html_version: :html5) assert_dom_not_equal(@html4_result, @input, html_version: :html5) end end test "raise NotImplementedError html5 when not supported" do Rails::Dom::Testing.stub(:html5_support?, false) do with_default_html_version(:html5) do assert_raises(NotImplementedError) { assert_dom_equal("a", "b") } assert_raises(NotImplementedError) { assert_dom_equal("a", "b", html_version: :html5) } assert_nothing_raised { assert_dom_equal(@html4_result, @input, html_version: :html4) } end end end test "default set to invalid" do with_default_html_version(:html9) do assert_raises(ArgumentError) { assert_dom_equal("a", "b") } end end test "invalid version specified" do assert_raises(ArgumentError) { assert_dom_equal("a", "b", html_version: :html9) } end end