Module: Dommy::Minitest::Assertions

Defined in:
lib/dommy/minitest/assertions.rb

Overview

Custom Minitest assertions for testing Dommy DOM objects.

Examples:

require "dommy/minitest"

class MyTest < Minitest::Test
  include Dommy::Minitest::Assertions

  def test_renders_button
    dom = parse_html("<button class='primary'>Submit</button>")
    assert_dom_contains(dom, "button.primary")
    assert_dom_contains_text(dom, "Submit")
  end
end

Instance Method Summary collapse

Instance Method Details

#assert_dom_contains(scope, selector, text: nil, count: nil, msg: nil) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/dommy/minitest/assertions.rb', line 23

def assert_dom_contains(scope, selector, text: nil, count: nil, msg: nil)
  matched = dom_matched_for(scope, selector, text: text)
  msg ||= "expected to contain DOM matching #{selector.inspect}" \
    "#{text ? " with text #{text.inspect}" : ""}" \
    "#{count ? " (count: #{count.inspect})" : ""}, found #{matched.size}"
  assert(Internal::DomMatching.count_matches?(matched.size, count), msg)
end

#assert_dom_contains_text(scope, text, msg: nil) ⇒ Object



38
39
40
41
42
# File 'lib/dommy/minitest/assertions.rb', line 38

def assert_dom_contains_text(scope, text, msg: nil)
  actual = dom_text_of(scope)
  msg ||= "expected text to include #{text.inspect}, got #{actual.inspect}"
  assert(Internal::DomMatching.text_matches?(actual, text), msg)
end

#assert_dom_has_attribute(element, name, value = UNSET, msg: nil) ⇒ Object

Without a value argument, checks attribute existence only. With a value, checks string equality.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dommy/minitest/assertions.rb', line 52

def assert_dom_has_attribute(element, name, value = UNSET, msg: nil)
  present = element.has_attribute?(name.to_s)
  if value.equal?(UNSET)
    msg ||= "expected element to have attribute #{name.inspect}"
    assert(present, msg)
  else
    actual = element.get_attribute(name.to_s)
    msg ||= "expected attribute #{name.inspect} to equal #{value.inspect}, got #{actual.inspect}"
    assert_equal(value.to_s, actual.to_s, msg)
  end
end

#assert_dom_has_class(element, class_name, msg: nil) ⇒ Object



70
71
72
73
74
# File 'lib/dommy/minitest/assertions.rb', line 70

def assert_dom_has_class(element, class_name, msg: nil)
  actual_classes = element.class_list.value.to_s.split(/\s+/)
  msg ||= "expected element to have class #{class_name.inspect}, got #{actual_classes.inspect}"
  assert_includes(actual_classes, class_name.to_s, msg)
end

#assert_dom_html_equal(scope, expected_html, msg: nil) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/dommy/minitest/assertions.rb', line 82

def assert_dom_html_equal(scope, expected_html, msg: nil)
  scope = Internal::ScopeResolution.resolve(scope)
  actual_n = Internal::DomMatching.normalize_html(Internal::DomMatching.html_of(scope))
  expected_n = Internal::DomMatching.normalize_html(expected_html)
  msg ||= "expected DOM HTML to match.\nExpected: #{expected_n}\nActual:   #{actual_n}"
  assert_equal(expected_n, actual_n, msg)
end

#refute_dom_contains(scope, selector, text: nil, count: nil, msg: nil) ⇒ Object



31
32
33
34
35
36
# File 'lib/dommy/minitest/assertions.rb', line 31

def refute_dom_contains(scope, selector, text: nil, count: nil, msg: nil)
  matched = dom_matched_for(scope, selector, text: text)
  msg ||= "expected NOT to contain DOM matching #{selector.inspect}" \
    "#{text ? " with text #{text.inspect}" : ""}, found #{matched.size}"
  refute(Internal::DomMatching.count_matches?(matched.size, count), msg)
end

#refute_dom_contains_text(scope, text, msg: nil) ⇒ Object



44
45
46
47
48
# File 'lib/dommy/minitest/assertions.rb', line 44

def refute_dom_contains_text(scope, text, msg: nil)
  actual = dom_text_of(scope)
  msg ||= "expected text NOT to include #{text.inspect}, got #{actual.inspect}"
  refute(Internal::DomMatching.text_matches?(actual, text), msg)
end

#refute_dom_has_attribute(element, name, msg: nil) ⇒ Object



64
65
66
67
68
# File 'lib/dommy/minitest/assertions.rb', line 64

def refute_dom_has_attribute(element, name, msg: nil)
  present = element.has_attribute?(name.to_s)
  msg ||= "expected element NOT to have attribute #{name.inspect}"
  refute(present, msg)
end

#refute_dom_has_class(element, class_name, msg: nil) ⇒ Object



76
77
78
79
80
# File 'lib/dommy/minitest/assertions.rb', line 76

def refute_dom_has_class(element, class_name, msg: nil)
  actual_classes = element.class_list.value.to_s.split(/\s+/)
  msg ||= "expected element NOT to have class #{class_name.inspect}, got #{actual_classes.inspect}"
  refute_includes(actual_classes, class_name.to_s, msg)
end