Retrospring/spec/support/nokogiri_matchers.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
1.3 KiB
Ruby
Raw Normal View History

2023-02-16 12:26:09 -08:00
# frozen_string_literal: true
module NokogiriMatchers
RSpec::Matchers.matcher :have_css do |css|
description { %(have at least one element matching the CSS selector #{css.inspect}) }
match do |rendered|
Nokogiri::HTML.parse(rendered).css(css).size.positive?
end
end
RSpec::Matchers.matcher :have_attribute do |expected_attribute|
description do
case expected_attribute
when Hash
raise ArgumentError.new("have_attribute only wants one key=>value pair") unless expected_attribute.size == 1
key = expected_attribute.keys.first
value = expected_attribute.values.first
%(have an attribute named #{key.inspect} with a value of #{value.inspect})
else
%(have an attribute named #{expected_attribute.inspect})
end
end
match do |element|
case expected_attribute
when Hash
raise ArgumentError.new("have_attribute only wants one key=>value pair") unless expected_attribute.size == 1
key = expected_attribute.keys.first
value = expected_attribute.values.first
element.attr(key.to_s).value == value
else
!element.attr(expected_attribute.to_s).nil?
end
end
end
2023-02-16 12:26:09 -08:00
end
RSpec.configure do |c|
c.include NokogiriMatchers, view: true
end