jquery-rails 3.0.4 → 4.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,99 +1,151 @@
1
- module ActionDispatch
2
- module Assertions
3
- module SelectorAssertions
4
- # Selects content from a JQuery response. Patterned loosely on
5
- # assert_select_rjs.
6
- #
7
- # === Narrowing down
8
- #
9
- # With no arguments, asserts that one or more method calls are made.
10
- #
11
- # Use the +method+ argument to narrow down the assertion to only
12
- # statements that call that specific method.
13
- #
14
- # Use the +opt+ argument to narrow down the assertion to only statements
15
- # that pass +opt+ as the first argument.
16
- #
17
- # Use the +id+ argument to narrow down the assertion to only statements
18
- # that invoke methods on the result of using that identifier as a
19
- # selector.
20
- #
21
- # === Using blocks
22
- #
23
- # Without a block, +assert_select_jquery_ merely asserts that the
24
- # response contains one or more statements that match the conditions
25
- # specified above
26
- #
27
- # With a block +assert_select_jquery_ also asserts that the method call
28
- # passes a javascript escaped string containing HTML. All such HTML
29
- # fragments are selected and passed to the block. Nested assertions are
30
- # supported.
31
- #
32
- # === Examples
33
- #
34
- # # asserts that the #notice element is hidden
35
- # assert_select :hide, '#notice'
36
- #
37
- # # asserts that the #cart element is shown with a blind parameter
38
- # assert_select :show, :blind, '#cart'
39
- #
40
- # # asserts that #cart content contains a #current_item
41
- # assert_select :html, '#cart' do
42
- # assert_select '#current_item'
43
- # end
44
-
45
- PATTERN_HTML = "\"((\\\\\"|[^\"])*)\""
46
- PATTERN_UNICODE_ESCAPED_CHAR = /\\u([0-9a-zA-Z]{4})/
47
-
48
- def assert_select_jquery(*args, &block)
49
- jquery_method = args.first.is_a?(Symbol) ? args.shift : nil
50
- jquery_opt = args.first.is_a?(Symbol) ? args.shift : nil
51
- id = args.first.is_a?(String) ? args.shift : nil
52
-
53
- pattern = "\\.#{jquery_method || '\\w+'}\\("
54
- pattern = "#{pattern}['\"]#{jquery_opt}['\"],?\\s*" if jquery_opt
55
- pattern = "#{pattern}#{PATTERN_HTML}"
56
- pattern = "(?:jQuery|\\$)\\(['\"]#{id}['\"]\\)#{pattern}" if id
57
-
58
- fragments = []
59
- response.body.scan(Regexp.new(pattern)).each do |match|
60
- doc = HTML::Document.new(unescape_js(match.first))
61
- doc.root.children.each do |child|
62
- fragments.push child if child.tag?
63
- end
64
- end
1
+ require 'rails/dom/testing/assertions/selector_assertions'
65
2
 
66
- if fragments.empty?
67
- opts = [jquery_method, jquery_opt, id].compact
68
- flunk "No JQuery call matches #{opts.inspect}"
69
- end
3
+ module Rails::Dom::Testing::Assertions::SelectorAssertions
4
+ # Selects content from a JQuery response. Patterned loosely on
5
+ # assert_select_rjs.
6
+ #
7
+ # === Narrowing down
8
+ #
9
+ # With no arguments, asserts that one or more method calls are made.
10
+ #
11
+ # Use the +method+ argument to narrow down the assertion to only
12
+ # statements that call that specific method.
13
+ #
14
+ # Use the +opt+ argument to narrow down the assertion to only statements
15
+ # that pass +opt+ as the first argument.
16
+ #
17
+ # Use the +id+ argument to narrow down the assertion to only statements
18
+ # that invoke methods on the result of using that identifier as a
19
+ # selector.
20
+ #
21
+ # === Using blocks
22
+ #
23
+ # Without a block, +assert_select_jquery_ merely asserts that the
24
+ # response contains one or more statements that match the conditions
25
+ # specified above
26
+ #
27
+ # With a block +assert_select_jquery_ also asserts that the method call
28
+ # passes a javascript escaped string containing HTML. All such HTML
29
+ # fragments are selected and passed to the block. Nested assertions are
30
+ # supported.
31
+ #
32
+ # === Examples
33
+ #
34
+ # # asserts that the #notice element is hidden
35
+ # assert_select :hide, '#notice'
36
+ #
37
+ # # asserts that the #cart element is shown with a blind parameter
38
+ # assert_select :show, :blind, '#cart'
39
+ #
40
+ # # asserts that #cart content contains a #current_item
41
+ # assert_select :html, '#cart' do
42
+ # assert_select '#current_item'
43
+ # end
44
+ #
45
+ # # asserts that #product append to a #product_list
46
+ # assert_select_jquery :appendTo, '#product_list' do
47
+ # assert_select '.product'
48
+ # end
49
+
50
+ PATTERN_HTML = "['\"]((\\\\\"|\\\\'|[^\"'])*)['\"]"
51
+ PATTERN_UNICODE_ESCAPED_CHAR = /\\u([0-9a-zA-Z]{4})/
52
+ SKELETAL_PATTERN = "(?:jQuery|\\$)\\(%s\\)\\.%s\\(%s\\)[;]?"
53
+
54
+ def assert_select_jquery(*args, &block)
55
+ jquery_method = args.first.is_a?(Symbol) ? args.shift : nil
56
+ jquery_opt = args.first.is_a?(Symbol) ? args.shift : nil
57
+ id = args.first.is_a?(String) ? escape_id(args.shift) : nil
58
+
59
+ target_pattern = "['\"]#{id || '.*'}['\"]"
60
+ method_pattern = "#{jquery_method || '\\w+'}"
61
+ argument_pattern = jquery_opt ? "['\"]#{jquery_opt}['\"].*" : PATTERN_HTML
62
+
63
+ # $("#id").show('blind', 1000);
64
+ # $("#id").html("<div>something</div>");
65
+ # $("#id").replaceWith("<div>something</div>");
66
+ target_as_receiver_pattern = SKELETAL_PATTERN % [target_pattern, method_pattern, argument_pattern]
67
+
68
+ # $("<div>something</div>").appendTo("#id");
69
+ # $("<div>something</div>").prependTo("#id");
70
+ target_as_argument_pattern = SKELETAL_PATTERN % [argument_pattern, method_pattern, target_pattern]
71
+
72
+ # $("#id").remove();
73
+ # $("#id").hide();
74
+ argumentless_pattern = SKELETAL_PATTERN % [target_pattern, method_pattern, '']
75
+
76
+ patterns = [target_as_receiver_pattern, target_as_argument_pattern]
77
+ patterns << argumentless_pattern unless jquery_opt
78
+
79
+ matched_pattern = nil
80
+ patterns.each do |pattern|
81
+ if response.body.match(Regexp.new(pattern))
82
+ matched_pattern = pattern
83
+ break
84
+ end
85
+ end
70
86
 
71
- if block
72
- begin
73
- in_scope, @selected = @selected, fragments
74
- yield
75
- ensure
76
- @selected = in_scope
87
+ unless matched_pattern
88
+ opts = [jquery_method, jquery_opt, id].compact
89
+ flunk "No JQuery call matches #{opts.inspect}"
90
+ end
91
+
92
+ if block_given?
93
+ @selected ||= nil
94
+ fragments = Nokogiri::HTML::Document.new.fragment
95
+
96
+ if matched_pattern
97
+ response.body.scan(Regexp.new(matched_pattern)).each do |match|
98
+ flunk 'This function can\'t have HTML argument' if match.is_a?(String)
99
+
100
+ doc = Nokogiri::HTML::DocumentFragment.parse(unescape_js(match.first))
101
+ doc.children.each do |child|
102
+ fragments << child if child.element?
77
103
  end
78
104
  end
79
105
  end
80
106
 
81
- private
82
-
83
- # Unescapes a JS string.
84
- def unescape_js(js_string)
85
- # js encodes double quotes and line breaks.
86
- unescaped= js_string.gsub('\"', '"')
87
- unescaped.gsub!('\\\'', "'")
88
- unescaped.gsub!(/\\\//, '/')
89
- unescaped.gsub!('\n', "\n")
90
- unescaped.gsub!('\076', '>')
91
- unescaped.gsub!('\074', '<')
92
- # js encodes non-ascii characters.
93
- unescaped.gsub!(PATTERN_UNICODE_ESCAPED_CHAR) {|u| [$1.hex].pack('U*')}
94
- unescaped
107
+ begin
108
+ in_scope, @selected = @selected, fragments
109
+ yield
110
+ ensure
111
+ @selected = in_scope
95
112
  end
96
-
97
113
  end
98
114
  end
115
+
116
+ private
117
+
118
+ # Unescapes a JS string.
119
+ def unescape_js(js_string)
120
+ # js encodes double quotes and line breaks.
121
+ unescaped= js_string.gsub('\"', '"')
122
+ unescaped.gsub!('\\\'', "'")
123
+ unescaped.gsub!(/\\\//, '/')
124
+ unescaped.gsub!('\n', "\n")
125
+ unescaped.gsub!('\076', '>')
126
+ unescaped.gsub!('\074', '<')
127
+ unescaped.gsub!(/\\\$/, '$')
128
+ unescaped.gsub!(/\\`/, '`')
129
+ # js encodes non-ascii characters.
130
+ unescaped.gsub!(PATTERN_UNICODE_ESCAPED_CHAR) {|u| [$1.hex].pack('U*')}
131
+ unescaped
132
+ end
133
+
134
+ def escape_id(selector)
135
+ return unless selector
136
+
137
+ id = selector.gsub('[', '\[')
138
+ id.gsub!(']', '\]')
139
+ id.gsub!('*', '\*')
140
+ id.gsub!('(', '\(')
141
+ id.gsub!(')', '\)')
142
+ id.gsub!('.', '\.')
143
+ id.gsub!('|', '\|')
144
+ id.gsub!('^', '\^')
145
+ id.gsub!('$', '\$')
146
+ id.gsub!('+', "\\\\+")
147
+ id.gsub!(',', '\,')
148
+
149
+ id
150
+ end
99
151
  end
@@ -1,7 +1,9 @@
1
1
  module Jquery
2
2
  module Rails
3
- VERSION = "3.0.4"
4
- JQUERY_VERSION = "1.10.2"
5
- JQUERY_UJS_VERSION = "e9e8b8fd3abb1571781bca7640e5c0c4d9cea2e3"
3
+ VERSION = "4.4.0"
4
+ JQUERY_VERSION = "1.12.4"
5
+ JQUERY_2_VERSION = "2.2.4"
6
+ JQUERY_3_VERSION = "3.5.1"
7
+ JQUERY_UJS_VERSION = "1.2.2"
6
8
  end
7
9
  end
data/lib/jquery/rails.rb CHANGED
@@ -1,10 +1,8 @@
1
1
  require 'jquery/assert_select' if ::Rails.env.test?
2
- require 'jquery/rails/engine' if ::Rails.version >= '3.1'
3
- require 'jquery/rails/railtie'
2
+ require 'jquery/rails/engine'
4
3
  require 'jquery/rails/version'
5
4
 
6
5
  module Jquery
7
6
  module Rails
8
- PROTOTYPE_JS = %w{prototype effects dragdrop controls}
9
7
  end
10
8
  end
@@ -0,0 +1,91 @@
1
+ require 'ostruct'
2
+ require_relative 'test_helper'
3
+ require_relative '../lib/jquery/assert_select'
4
+
5
+ class AssertSelectJQueryTest < ActiveSupport::TestCase
6
+ include Rails::Dom::Testing::Assertions::SelectorAssertions
7
+ attr_reader :response
8
+
9
+ JAVASCRIPT_TEST_OUTPUT = <<-JS
10
+ $("#card").show("blind", 1000);
11
+ $("#id").html('<div><p>something</p></div>');
12
+ $('#card').html('<div><p>something else</p></div>');
13
+ jQuery("#id").replaceWith("<div><p>something</p></div>");
14
+ $("<div><p>something</p></div>").appendTo("#id");
15
+ $("<div><p>something else</p></div>").appendTo("#id");
16
+ jQuery("<div><p>something</p></div>").prependTo("#id");
17
+ $('#id').remove();
18
+ jQuery("#id").hide();
19
+ $("[data-placeholder~=name]").remove();
20
+ $("#cart tr:not(.total_line) > *").remove();
21
+ $("[href|=\"val\"][href$=\"val\"][href^=\"val\"]").remove();
22
+ $("tr + td, li").remove();
23
+
24
+ // without semicolon
25
+ $("#browser_cart").hide("blind", 1000)
26
+
27
+ $('#item').html('<div><span>\\`Total\\`: \\$12.34</span></div>');
28
+ JS
29
+
30
+ setup do
31
+ @response = OpenStruct.new(content_type: 'text/javascript', body: JAVASCRIPT_TEST_OUTPUT)
32
+ end
33
+
34
+ def test_target_as_receiver
35
+ assert_nothing_raised do
36
+ assert_select_jquery :show, :blind, '#card'
37
+ assert_select_jquery :hide, :blind, '#browser_cart'
38
+ assert_select_jquery :html, '#id' do
39
+ assert_select 'p', 'something'
40
+ end
41
+ assert_select_jquery :replaceWith, '#id' do
42
+ assert_select 'p', 'something'
43
+ end
44
+ assert_select_jquery :remove, "[data-placeholder~=name]"
45
+ assert_select_jquery :remove, "#cart tr:not(.total_line) > *"
46
+ assert_select_jquery :remove, "[href|=\"val\"][href$=\"val\"][href^=\"val\"]"
47
+ assert_select_jquery :remove, "tr + td, li"
48
+
49
+ assert_select_jquery :html, '#item' do
50
+ assert_select 'span', '`Total`: $12.34'
51
+ end
52
+ end
53
+
54
+ assert_raise Minitest::Assertion, "No JQuery call matches [:show, :some_wrong]" do
55
+ assert_select_jquery :show, :some_wrong
56
+ end
57
+
58
+ assert_raise Minitest::Assertion, "<something else> was expected but was <something>" do
59
+ assert_select_jquery :html, '#id' do
60
+ assert_select 'p', 'something else'
61
+ end
62
+ end
63
+ end
64
+
65
+ def test_target_as_argument
66
+ assert_nothing_raised do
67
+ assert_select_jquery :appendTo, '#id' do
68
+ assert_select 'p', 'something'
69
+ assert_select 'p', 'something else'
70
+ end
71
+ assert_select_jquery :prependTo, '#id' do
72
+ assert_select 'p', 'something'
73
+ end
74
+ end
75
+
76
+ assert_raise Minitest::Assertion, 'No JQuery call matches [:prependTo, "#wrong_id"]' do
77
+ assert_select_jquery :prependTo, '#wrong_id'
78
+ end
79
+ end
80
+
81
+ def test_argumentless
82
+ assert_nothing_raised do
83
+ assert_select_jquery :remove
84
+ assert_select_jquery :hide
85
+ end
86
+
87
+ assert_raise Minitest::Assertion, 'No JQuery call matches [:wrong_function]' do
88
+ assert_select_jquery :wrong_function
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,6 @@
1
+ require 'nokogiri'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
4
+ require 'minitest/autorun'
5
+
6
+ ActiveSupport::TestCase.test_order = :random