Skip to content

Commit d4dd8cd

Browse files
committed
Add possibility to test HTML attribute selectors
Previously it wasn't possible to test something like this: ``` assert_select_jquery :remove, "[data-placeholder~=name]" ``` This commit fixes it by escaping `[` and `]` characters in the selector.
1 parent 47d1512 commit d4dd8cd

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.2.2
2+
3+
- Add possibility to test HTML attribute selectors
4+
15
## 4.2.1
26

37
- Update jQuery to 3.1.0

lib/jquery/assert_select.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ module Rails::Dom::Testing::Assertions::SelectorAssertions
5454
def assert_select_jquery(*args, &block)
5555
jquery_method = args.first.is_a?(Symbol) ? args.shift : nil
5656
jquery_opt = args.first.is_a?(Symbol) ? args.shift : nil
57-
id = args.first.is_a?(String) ? args.shift : nil
57+
id = args.first.is_a?(String) ? escape_id(args.shift) : nil
5858

59-
target_pattern = "['\"]#{id || '.*'}['\"]"
59+
target_pattern = "['\"\\[]#{id || '.*'}['\"\\]]"
6060
method_pattern = "#{jquery_method || '\\w+'}"
6161
argument_pattern = jquery_opt ? "['\"]#{jquery_opt}['\"].*" : PATTERN_HTML
6262

@@ -128,4 +128,13 @@ def unescape_js(js_string)
128128
unescaped.gsub!(PATTERN_UNICODE_ESCAPED_CHAR) {|u| [$1.hex].pack('U*')}
129129
unescaped
130130
end
131+
132+
def escape_id(selector)
133+
return unless selector
134+
135+
id = selector.gsub('[', '\[')
136+
id.gsub!(']', '\]')
137+
138+
id
139+
end
131140
end

test/assert_select_jquery_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class AssertSelectJQueryTest < ActiveSupport::TestCase
1313
jQuery("<div><p>something</p></div>").prependTo("#id");
1414
$('#id').remove();
1515
jQuery("#id").hide();
16+
$("[data-placeholder~=name]").remove();
1617
JS
1718

1819
setup do
@@ -28,6 +29,7 @@ def test_target_as_receiver
2829
assert_select_jquery :replaceWith, '#id' do
2930
assert_select 'p', 'something'
3031
end
32+
assert_select_jquery :remove, "[data-placeholder~=name]"
3133
end
3234

3335
assert_raise Minitest::Assertion, "No JQuery call matches [:show, :some_wrong]" do

0 commit comments

Comments
 (0)