Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Changelog
=========

**NOTE**: the master branch has been deprecated, for latest docs and stuff use [main](https://github.com/kucaahbe/rspec-html-matchers/blob/main/CHANGELOG.md) branch instead

0.10.0
------

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
[![RSpec JRuby](https://github.com/kucaahbe/rspec-html-matchers/actions/workflows/rspec-jruby.yml/badge.svg)](https://github.com/kucaahbe/rspec-html-matchers/actions/workflows/rspec-jruby.yml)
[![Cucumber MRI](https://github.com/kucaahbe/rspec-html-matchers/actions/workflows/cucumber-mri.yml/badge.svg)](https://github.com/kucaahbe/rspec-html-matchers/actions/workflows/cucumber-mri.yml)

**NOTE**: the master branch has been deprecated, for latest docs and stuff use main branch instead

Goals
-----

Expand Down
5 changes: 5 additions & 0 deletions lib/rspec-html-matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def have_tag tag, options = {}, &block
@__current_scope_for_nokogiri_matcher = HaveTag.new(tag, options, &block)
end

def have_child_tag tag, options = {}, &block
options[:match_any_child_tag] = true
have_tag(tag, options, &block)
end

# tests whether tag have any content inside
#
# @example
Expand Down
6 changes: 6 additions & 0 deletions lib/rspec-html-matchers/have_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

def initialize tag, options = {}, &block
@tag = tag.to_s
@match_any_child_tag = options.delete(:match_any_child_tag)
@options = options
@block = block

Expand All @@ -62,6 +63,10 @@
@tag += ":not(.#{classes_to_selector(classes)})"
end

if @match_any_child_tag

Check failure on line 66 in lib/rspec-html-matchers/have_tag.rb

View workflow job for this annotation

GitHub Actions / Linter-Rubocop

Style/IfUnlessModifier: Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`.
@tag = "* > #{@tag}"
end

validate_options!
organize_options!
end
Expand Down Expand Up @@ -89,6 +94,7 @@
rescue NoMethodError
Nokogiri::XML::NodeSet.new(Nokogiri::XML::Document.new)
end

if tag_presents? && proper_content? && count_right?
@block.call(self) if @block
true
Expand Down Expand Up @@ -271,12 +277,12 @@
end
end

def match_succeeded! message, *args

Check failure on line 280 in lib/rspec-html-matchers/have_tag.rb

View workflow job for this annotation

GitHub Actions / Linter-Rubocop

Naming/PredicateMethod: Predicate method names should end with `?`.
@failure_message_when_negated = format MESSAGES[message], *args
true
end

def match_failed! message, *args

Check failure on line 285 in lib/rspec-html-matchers/have_tag.rb

View workflow job for this annotation

GitHub Actions / Linter-Rubocop

Naming/PredicateMethod: Predicate method names should end with `?`.
@failure_message = format MESSAGES[message], *args
false
end
Expand Down
6 changes: 4 additions & 2 deletions spec/fixtures/nested_matchers.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

<b class="nested"></b>
<p class="deep-nesting">
<b class="nested"></b>
<b class="nested">
<i>italic</i>
</b>
<b class="nested"></b>
</p>
</div>
Expand All @@ -14,4 +16,4 @@
<p class="deep-nesting">
<b class="nested"></b>
</p>
</div>
</div>
46 changes: 46 additions & 0 deletions spec/have_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,52 @@

require 'spec_helper'

describe 'have_child_tag' do
asset 'nested_matchers'

it 'should not find itself' do
expect(rendered).to have_tag('div#one') do |a|
expect(a).not_to have_child_tag 'div#one'
end
end

it 'should not find siblings tags' do
expect(rendered).to have_tag('div#one') do |a|
expect(a).not_to have_child_tag 'div#two'
end
expect(rendered).to have_tag('p.find_me', :count => 3) do |b|
expect(b).not_to have_child_tag 'p.find_me'
end
end

it 'should not find siblings tags' do
expect(rendered).to have_tag('div#one') do |a|
expect(a).not_to have_child_tag 'div#two'

expect(a).to have_tag('p.deep-nesting', :count => 1) do |b|
expect(b).not_to have_child_tag 'div#one'
expect(b).not_to have_child_tag 'p.find_me'
expect(b).to have_child_tag 'b.nested'
end
end
end

it 'should find direct children' do
expect(rendered).to have_tag('div#one') do |a|
expect(a).to have_child_tag 'p.find_me'
expect(a).to have_child_tag('p.deep-nesting', :count => 1)
end
end

it 'should find grand children at any level' do
expect(rendered).to have_tag('div#one') do |a|
expect(a).to have_child_tag 'p.find_me'
expect(a).to have_child_tag('p.deep-nesting', :count => 1)
expect(a).to have_child_tag('i', :count => 1)
end
end
end

describe 'have_tag' do
context '[through css selector]' do
asset 'search_and_submit'
Expand Down
Loading