-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ancestor/sibling assertions and expectations
- Loading branch information
Showing
15 changed files
with
338 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'capybara/rspec/matchers/base' | ||
require 'capybara/rspec/matchers/count_sugar' | ||
|
||
module Capybara | ||
module RSpecMatchers | ||
module Matchers | ||
class HaveAncestor < WrappedElementMatcher | ||
include CountSugar | ||
|
||
def element_matches?(el) | ||
el.assert_ancestor(*@args, &@filter_block) | ||
end | ||
|
||
def element_does_not_match?(el) | ||
el.assert_no_ancestor(*@args, &@filter_block) | ||
end | ||
|
||
def description | ||
"have ancestor #{query.description}" | ||
end | ||
|
||
def query | ||
@query ||= Capybara::Queries::AncestorQuery.new(*session_query_args, &@filter_block) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'capybara/rspec/matchers/base' | ||
require 'capybara/rspec/matchers/count_sugar' | ||
|
||
module Capybara | ||
module RSpecMatchers | ||
module Matchers | ||
class HaveSibling < WrappedElementMatcher | ||
include CountSugar | ||
|
||
def element_matches?(el) | ||
el.assert_sibling(*@args, &@filter_block) | ||
end | ||
|
||
def element_does_not_match?(el) | ||
el.assert_no_sibling(*@args, &@filter_block) | ||
end | ||
|
||
def description | ||
"have sibling #{query.description}" | ||
end | ||
|
||
def query | ||
@query ||= Capybara::Queries::SiblingQuery.new(*session_query_args, &@filter_block) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
Capybara::SpecHelper.spec '#have_ancestor' do | ||
before do | ||
@session.visit('/with_html') | ||
end | ||
|
||
it 'should assert an ancestor using the given locator' do | ||
el = @session.find(:css, '#ancestor1') | ||
expect(el).to have_ancestor(:css, '#ancestor2') | ||
end | ||
|
||
it 'should assert an ancestor even if not parent' do | ||
el = @session.find(:css, '#child') | ||
expect(el).to have_ancestor(:css, '#ancestor3') | ||
end | ||
|
||
it 'should not raise an error if there are multiple matches' do | ||
el = @session.find(:css, '#child') | ||
expect(el).to have_ancestor(:css, 'div') | ||
end | ||
|
||
it 'should allow counts to be specified' do | ||
el = @session.find(:css, '#child') | ||
|
||
expect do | ||
expect(el).to have_ancestor(:css, 'div').once | ||
end.to raise_error(RSpec::Expectations::ExpectationNotMetError) | ||
|
||
expect(el).to have_ancestor(:css, 'div').exactly(3).times | ||
end | ||
end | ||
|
||
Capybara::SpecHelper.spec '#have_no_ancestor' do | ||
before do | ||
@session.visit('/with_html') | ||
end | ||
|
||
it 'should assert no matching ancestor' do | ||
el = @session.find(:css, '#ancestor1') | ||
expect(el).to have_no_ancestor(:css, '#child') | ||
expect(el).not_to have_ancestor(:css, '#child') | ||
end | ||
end |
Oops, something went wrong.