aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/lib/action_view/testing/assertions/dom.rb
blob: 825720a56f69d5998fa4c2e7af34d858502826a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module ActionView
  module Assertions
    module DomAssertions
      # \Test two HTML strings for equivalency (e.g., equal even when attributes are in another order)
      #
      #   # assert that the referenced method generates the appropriate HTML string
      #   assert_dom_equal '<a href="http://www.example.com">Apples</a>', link_to("Apples", "http://www.example.com")
      def assert_dom_equal(expected, actual, message = nil)
        assert dom_assertion(expected, actual, message)
      end

      # The negated form of +assert_dom_equal+.
      #
      #   # assert that the referenced method does not generate the specified HTML string
      #   assert_dom_not_equal '<a href="http://www.example.com">Apples</a>', link_to("Oranges", "http://www.example.com")
      def assert_dom_not_equal(expected, actual, message = nil)
        assert_not dom_assertion(expected, actual, message)
      end

      protected
        def dom_assertion(expected_string, actual_string, message = nil)
          expected, actual = Loofah.fragment(expected_string), Loofah.fragment(actual_string)
          message ||= "Expected: #{expected}\nActual: #{actual}"
          return compare_doms(expected, actual), message
        end

        def compare_doms(expected, actual)
          return false unless expected.children.size == actual.children.size

          expected.children.each_with_index do |child, i|
            return false unless equal_children?(child, actual.children[i])
          end
          true
        end

        def equal_children?(child, other_child)
          return false unless child.type == other_child.type

          if child.element?
            child.name == other_child.name &&
                equal_attribute_nodes?(child.attribute_nodes, other_child.attribute_nodes)
          else
            child.to_s == other_child.to_s
          end
        end

        def equal_attribute_nodes?(nodes, other_nodes)
          return false unless nodes.size == other_nodes.size
          nodes = nodes.sort_by(&:name)
          other_nodes = other_nodes.sort_by(&:name)

          nodes.each_with_index do |attr, i|
            return false unless equal_attribute?(attr, other_nodes[i])
          end
          true
        end

        def equal_attribute?(attr, other_attr)
          attr.name == other_attr.name && attr.value == other_attr.value
        end
    end
  end
end