1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Deprecate assert_style/has_style?/have_style in favor of assert_matches_style/matches_styles?/match_style to match naming convention

This commit is contained in:
Thomas Walpole 2018-12-06 11:12:18 -08:00
parent 2c8c8cb9f8
commit 50e0aa5da6
11 changed files with 123 additions and 68 deletions

View file

@ -83,10 +83,10 @@ module Capybara
## Assert element has the provided CSS styles
#
# @!method assert_style
# see {Capybara::Node::Matchers#assert_style}
# @!method assert_matches_style
# see {Capybara::Node::Matchers#assert_matches_style}
%w[selector no_selector style
%w[selector no_selector matches_style
all_of_selectors none_of_selectors any_of_selectors
matches_selector not_matches_selector].each do |assertion_name|
class_eval <<-ASSERTION, __FILE__, __LINE__ + 1

View file

@ -17,7 +17,7 @@ module Capybara
end + [%w[assert_all_of_selectors must_have_all_of_selectors],
%w[assert_none_of_selectors must_have_none_of_selectors],
%w[assert_any_of_selectors must_have_any_of_selectors],
%w[assert_style must_have_style]] +
%w[assert_matches_style must_match_style]] +
%w[selector xpath css].flat_map do |assertion|
[%W[assert_matches_#{assertion} must_match_#{assertion}],
%W[refute_matches_#{assertion} wont_match_#{assertion}]]
@ -36,6 +36,13 @@ module Capybara
end
# rubocop:enable Style/MultilineBlockChain
##
# @deprecated
def must_have_style(*args, &block)
warn "must_have_style is deprecated, please use must_match_style"
must_match_style(*args, &block)
end
##
# Expectation that there is xpath
#
@ -169,8 +176,8 @@ module Capybara
##
# Expectation that element has style
#
# @!method must_have_style
# see {Capybara::Node::Matchers#assert_style}
# @!method must_match_style
# see {Capybara::Node::Matchers#assert_matches_style}
end
end
end

View file

@ -56,13 +56,21 @@ module Capybara
#
# Checks if a an element has the specified CSS styles
#
# element.has_style?( 'color' => 'rgb(0,0,255)', 'font-size' => /px/ )
# element.matches_style?( 'color' => 'rgb(0,0,255)', 'font-size' => /px/ )
#
# @param styles [Hash]
# @return [Boolean] If the styles match
#
def matches_style?(styles, **options)
make_predicate(options) { assert_matches_style(styles, options) }
end
##
# @deprecated
#
def has_style?(styles, **options)
make_predicate(options) { assert_style(styles, options) }
warn 'DEPRECATED: has_style? is deprecated, please use matches_style?'
matches_style?(styles, **options)
end
##
@ -108,12 +116,12 @@ module Capybara
#
# Asserts that an element has the specified CSS styles
#
# element.assert_style( 'color' => 'rgb(0,0,255)', 'font-size' => /px/ )
# element.assert_matches_style( 'color' => 'rgb(0,0,255)', 'font-size' => /px/ )
#
# @param styles [Hash]
# @raise [Capybara::ExpectationNotMet] If the element doesn't have the specified styles
#
def assert_style(styles, **options)
def assert_matches_style(styles, **options)
query_args = _set_query_session_options(styles, options)
query = Capybara::Queries::StyleQuery.new(*query_args)
synchronize(query.wait) do
@ -122,6 +130,13 @@ module Capybara
true
end
##
# @deprecated
def assert_style(styles, **options)
warn 'assert_style is deprecated, please use assert_matches_style instead'
assert_matches_style(styles, **options)
end
# Asserts that all of the provided selectors are present on the given page
# or descendants of the current node. If options are provided, the assertion
# will check that each locator is present with those options as well (other than :wait).

View file

@ -3,7 +3,7 @@
require 'capybara/rspec/matchers/have_selector'
require 'capybara/rspec/matchers/match_selector'
require 'capybara/rspec/matchers/have_current_path'
require 'capybara/rspec/matchers/have_style'
require 'capybara/rspec/matchers/match_style'
require 'capybara/rspec/matchers/have_text'
require 'capybara/rspec/matchers/have_title'
require 'capybara/rspec/matchers/become_closed'
@ -124,9 +124,17 @@ module Capybara
end
# RSpec matcher for element style
# See {Capybara::Node::Matchers#has_style?}
# See {Capybara::Node::Matchers#matches_style?}
def match_style(styles, **options)
Matchers::MatchStyle.new(styles, options)
end
##
# @deprecated
#
def have_style(styles, **options)
Matchers::HaveStyle.new(styles, options)
warn 'DEPRECATED: have_style is deprecated, please use match_style'
match_style(styles, **options)
end
%w[selector css xpath text title current_path link button

View file

@ -1,23 +0,0 @@
# frozen_string_literal: true
require 'capybara/rspec/matchers/base'
module Capybara
module RSpecMatchers
module Matchers
class HaveStyle < WrappedElementMatcher
def element_matches?(el)
el.assert_style(*@args)
end
def does_not_match?(_actual)
raise ArgumentError, 'The have_style matcher does not support use with not_to/should_not'
end
def description
'have style'
end
end
end
end
end

View file

@ -0,0 +1,38 @@
# frozen_string_literal: true
require 'capybara/rspec/matchers/base'
module Capybara
module RSpecMatchers
module Matchers
class MatchStyle < WrappedElementMatcher
def element_matches?(el)
el.assert_matches_style(*@args)
end
def does_not_match?(_actual)
raise ArgumentError, 'The match_style matcher does not support use with not_to/should_not'
end
def description
'match style'
end
end
end
end
end
module Capybara
module RSpecMatchers
module Matchers
##
# @deprecated
class HaveStyle < MatchStyle
def initialize(*args, &filter_block)
warn 'HaveStyle matcher is deprecated, please use the MatchStyle matcher instead'
super
end
end
end
end
end

View file

@ -1,17 +1,17 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#assert_style', requires: [:css] do
Capybara::SpecHelper.spec '#assert_matches_style', requires: [:css] do
it 'should not raise if the elements style contains the given properties' do
@session.visit('/with_html')
expect do
@session.find(:css, '#first').assert_style(display: 'block')
@session.find(:css, '#first').assert_matches_style(display: 'block')
end.not_to raise_error
end
it "should raise error if the elements style doesn't contain the given properties" do
@session.visit('/with_html')
expect do
@session.find(:css, '#first').assert_style(display: 'inline')
@session.find(:css, '#first').assert_matches_style(display: 'inline')
end.to raise_error(Capybara::ExpectationNotMet, 'Expected node to have styles {"display"=>"inline"}. Actual styles were {"display"=>"block"}')
end
@ -20,7 +20,7 @@ Capybara::SpecHelper.spec '#assert_style', requires: [:css] do
el = @session.find(:css, '#change')
@session.click_link('Change size')
expect do
el.assert_style({ 'font-size': '50px' }, wait: 3)
el.assert_matches_style({ 'font-size': '50px' }, wait: 3)
end.not_to raise_error
end
end

View file

@ -1,25 +0,0 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#has_style?', requires: [:css] do
before do
@session.visit('/with_html')
end
it 'should be true if the element has the given style' do
expect(@session.find(:css, '#first')).to have_style(display: 'block')
expect(@session.find(:css, '#first').has_style?(display: 'block')).to be true
expect(@session.find(:css, '#second')).to have_style('display' => 'inline')
expect(@session.find(:css, '#second').has_style?('display' => 'inline')).to be true
end
it 'should be false if the element does not have the given style' do
expect(@session.find(:css, '#first').has_style?('display' => 'inline')).to be false
expect(@session.find(:css, '#second').has_style?(display: 'block')).to be false
end
it 'allows Regexp for value matching' do
expect(@session.find(:css, '#first')).to have_style(display: /^bl/)
expect(@session.find(:css, '#first').has_style?('display' => /^bl/)).to be true
expect(@session.find(:css, '#first').has_style?(display: /^in/)).to be false
end
end

View file

@ -0,0 +1,35 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#matches_style?', requires: [:css] do
before do
@session.visit('/with_html')
end
it 'should be true if the element has the given style' do
expect(@session.find(:css, '#first')).to match_style(display: 'block')
expect(@session.find(:css, '#first').matches_style?(display: 'block')).to be true
expect(@session.find(:css, '#second')).to match_style('display' => 'inline')
expect(@session.find(:css, '#second').matches_style?('display' => 'inline')).to be true
end
it 'should be false if the element does not have the given style' do
expect(@session.find(:css, '#first').matches_style?('display' => 'inline')).to be false
expect(@session.find(:css, '#second').matches_style?(display: 'block')).to be false
end
it 'allows Regexp for value matching' do
expect(@session.find(:css, '#first')).to match_style(display: /^bl/)
expect(@session.find(:css, '#first').matches_style?('display' => /^bl/)).to be true
expect(@session.find(:css, '#first').matches_style?(display: /^in/)).to be false
end
it 'deprecated has_style?' do
expect_any_instance_of(Kernel).to receive(:warn).once
have_style(display: /^bl/)
el = @session.find(:css, '#first')
allow(el).to receive(:warn).and_return(nil)
el.has_style?('display' => /^bl/)
expect(el).to have_received(:warn)
end
end

View file

@ -125,10 +125,10 @@ class MinitestTest < Minitest::Test
refute_matches_xpath(find(:select, 'form_title'), './/select[@id="form_other_title"]')
end
def test_assert_style
def test_assert_matches_style
skip "Rack test doesn't support style" if Capybara.current_driver == :rack_test
visit('/with_html')
assert_style(find(:css, '#second'), display: 'inline')
assert_matches_style(find(:css, '#second'), display: 'inline')
end
end

View file

@ -124,7 +124,7 @@ class MinitestSpecTest < Minitest::Spec
it 'supports style expectations' do
skip "Rack test doesn't support style" if Capybara.current_driver == :rack_test
visit('/with_html')
find(:css, '#second').must_have_style('display' => 'inline')
find(:css, '#second').must_match_style('display' => 'inline')
end
end