Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add chained replace/update support for assert_select_rjs [Rick Olson]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5110 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 6dbac689a8d7f285ccdb2e3fa2b2967d731475ab 1 parent 3ab1cb6
risk danger olson technoweenie authored
15 actionpack/CHANGELOG
View
@@ -1,5 +1,20 @@
*SVN*
+* Add chained replace/update support for assert_select_rjs [Rick Olson]
+
+ Given RJS like...
+
+ page['test1'].replace "<div id=\"1\">foo</div>"
+ page['test2'].replace_html "<div id=\"2\">foo</div>"
+
+ Test it with...
+
+ assert_select_rjs :chained_replace
+ assert_select_rjs :chained_replace, "test1"
+
+ assert_select_rjs :chained_replace_html
+ assert_select_rjs :chained_replace_html, "test2"
+
* Load helpers in alphabetical order for consistency. Resolve cyclic javascript_helper dependency. #6132, #6178 [choonkeat@gmail.com]
* Skip params with empty names, such as the &=Save query string from <input type="submit"/>. #2569 [manfred, raphinou@yahoo.com]
35 actionpack/lib/action_controller/assertions/selector_assertions.rb
View
@@ -343,11 +343,16 @@ def assert_select(*args, &block)
#
# === Examples
#
- # # Updating the element foo.
- # assert_select_rjs :update, "foo"
+ # # Replacing the element foo.
+ # # page.replace 'foo', ...
+ # assert_select_rjs :replace, "foo"
+ #
+ # # Replacing with the chained RJS proxy.
+ # # page[:foo].replace ...
+ # assert_select_rjs :chained_replace, 'foo'
#
# # Inserting into the element bar, top position.
- # assert_select rjs, :insert, :top, "bar"
+ # assert_select_rjs :insert, :top, "bar"
#
# # Changing the element foo, with an image.
# assert_select_rjs "foo" do
@@ -362,20 +367,22 @@ def assert_select(*args, &block)
# # The same, but shorter.
# assert_select "ol>li", 4
def assert_select_rjs(*args, &block)
- arg = args.shift
+ rjs_type = nil
+ arg = args.shift
# If the first argument is a symbol, it's the type of RJS statement we're looking
# for (update, replace, insertion, etc). Otherwise, we're looking for just about
# any RJS statement.
if arg.is_a?(Symbol)
- if arg == :insert
+ rjs_type = arg
+ if rjs_type == :insert
arg = args.shift
insertion = "insert_#{arg}".to_sym
raise ArgumentError, "Unknown RJS insertion type #{arg}" unless RJS_STATEMENTS[insertion]
statement = "(#{RJS_STATEMENTS[insertion]})"
else
- raise ArgumentError, "Unknown RJS statement type #{arg}" unless RJS_STATEMENTS[arg]
- statement = "(#{RJS_STATEMENTS[arg]})"
+ raise ArgumentError, "Unknown RJS statement type #{rjs_type}" unless RJS_STATEMENTS[rjs_type]
+ statement = "(#{RJS_STATEMENTS[rjs_type]})"
end
arg = args.shift
else
@@ -391,7 +398,13 @@ def assert_select_rjs(*args, &block)
id = "[^\"]*"
end
- pattern = Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE)
+ pattern =
+ case rjs_type
+ when :chained_replace, :chained_replace_html
+ Regexp.new("\\$\\(\"#{id}\"\\)#{statement}\\(#{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE)
+ else
+ Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE)
+ end
# Duplicate the body since the next step involves destroying it.
matches = nil
@@ -507,8 +520,10 @@ def assert_select_email(&block)
protected
unless const_defined?(:RJS_STATEMENTS)
RJS_STATEMENTS = {
- :replace => /Element\.replace/,
- :replace_html => /Element\.update/
+ :replace => /Element\.replace/,
+ :replace_html => /Element\.update/,
+ :chained_replace => /\.replace/,
+ :chained_replace_html => /\.update/,
}
RJS_INSERTIONS = [:top, :bottom, :before, :after]
RJS_INSERTIONS.each do |insertion|
27 actionpack/test/controller/assert_select_test.rb
View
@@ -340,6 +340,33 @@ def test_assert_select_rjs_for_replace
assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" }
end
+ def test_assert_select_rjs_for_chained_replace
+ render_rjs do |page|
+ page['test1'].replace "<div id=\"1\">foo</div>"
+ page['test2'].replace_html "<div id=\"2\">foo</div>"
+ page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
+ end
+ # Replace.
+ assert_select_rjs :chained_replace do
+ assert_select "div", 1
+ assert_select "#1"
+ end
+ assert_select_rjs :chained_replace, "test1" do
+ assert_select "div", 1
+ assert_select "#1"
+ end
+ assert_raises(AssertionFailedError) { assert_select_rjs :chained_replace, "test2" }
+ # Replace HTML.
+ assert_select_rjs :chained_replace_html do
+ assert_select "div", 1
+ assert_select "#2"
+ end
+ assert_select_rjs :chained_replace_html, "test2" do
+ assert_select "div", 1
+ assert_select "#2"
+ end
+ assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" }
+ end
def test_assert_select_rjs_for_insert
render_rjs do |page|
Please sign in to comment.
Something went wrong with that request. Please try again.