From 288aa9e377300eb93f32765be009838652049b3f Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Thu, 3 Dec 2020 09:51:21 -0500 Subject: [PATCH] Expand Substitution Matching Types support Add substitution support for `Symbol` and `Numeric`. It can be convenient to pass in the value of an `ActiveRecord::Base#id`, or a `Symbol` declared elsewhere in the test. --- .../dom/testing/assertions/selector_assertions.rb | 5 ++++- .../selector_assertions/substitution_context.rb | 2 +- test/selector_assertions_test.rb | 10 ++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/rails/dom/testing/assertions/selector_assertions.rb b/lib/rails/dom/testing/assertions/selector_assertions.rb index 8de6596..909caf0 100644 --- a/lib/rails/dom/testing/assertions/selector_assertions.rb +++ b/lib/rails/dom/testing/assertions/selector_assertions.rb @@ -96,11 +96,14 @@ def css_select(*args) # assert_select "li", 8 # end # - # The selector may be a CSS selector expression (String) or an expression + # The selector may be a CSS selector expression (String, Symbol, or Numeric) or an expression # with substitution values (Array). # Substitution uses a custom pseudo class match. Pass in whatever attribute you want to match (enclosed in quotes) and a ? for the substitution. # assert_select returns nil if called with an invalid css selector. # + # assert_select "div:match('id', ?)", "id_string" + # assert_select "div:match('id', ?)", :id_string + # assert_select "div:match('id', ?)", 1 # assert_select "div:match('id', ?)", /\d+/ # # === Equality Tests diff --git a/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb b/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb index 0e140a0..f6d1c35 100644 --- a/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb +++ b/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb @@ -28,6 +28,6 @@ def matcher_for(value, format_for_presentation) end def substitutable?(value) - value.is_a?(String) || value.is_a?(Regexp) + [ Symbol, Numeric, String, Regexp ].any? { |type| value.is_a? type } end end diff --git a/test/selector_assertions_test.rb b/test/selector_assertions_test.rb index ededb24..cd1b9f6 100644 --- a/test/selector_assertions_test.rb +++ b/test/selector_assertions_test.rb @@ -135,6 +135,16 @@ def test_substitution_values end end + def test_substitution_values_with_values_other_than_string_or_regexp + render_html %Q{
symbol
numeric
} + assert_select "div:match('id', ?)", :id_string do |elements| + assert_equal 1, elements.size + end + assert_select "div:match('id', ?)", 1 do |elements| + assert_equal 1, elements.size + end + end + def test_assert_select_root_html render_html ''