From 6dbac689a8d7f285ccdb2e3fa2b2967d731475ab Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Thu, 14 Sep 2006 17:50:46 +0000 Subject: 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 --- actionpack/CHANGELOG | 15 ++++++++++ .../assertions/selector_assertions.rb | 35 +++++++++++++++------- actionpack/test/controller/assert_select_test.rb | 27 +++++++++++++++++ 3 files changed, 67 insertions(+), 10 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 56d67d90c3..baaf7f2ddc 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,20 @@ *SVN* +* Add chained replace/update support for assert_select_rjs [Rick Olson] + + Given RJS like... + + page['test1'].replace "
foo
" + page['test2'].replace_html "
foo
" + + 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 . #2569 [manfred, raphinou@yahoo.com] diff --git a/actionpack/lib/action_controller/assertions/selector_assertions.rb b/actionpack/lib/action_controller/assertions/selector_assertions.rb index 7984e14c67..a6cba9d805 100644 --- a/actionpack/lib/action_controller/assertions/selector_assertions.rb +++ b/actionpack/lib/action_controller/assertions/selector_assertions.rb @@ -343,11 +343,16 @@ EOT # # === 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 @@ EOT # # 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 @@ EOT 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 @@ EOT 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| diff --git a/actionpack/test/controller/assert_select_test.rb b/actionpack/test/controller/assert_select_test.rb index abeee689f4..bed8da622d 100644 --- a/actionpack/test/controller/assert_select_test.rb +++ b/actionpack/test/controller/assert_select_test.rb @@ -340,6 +340,33 @@ class AssertSelectTest < Test::Unit::TestCase 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 "
foo
" + page['test2'].replace_html "
foo
" + page.insert_html :top, "test3", "
foo
" + 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| -- cgit v1.2.3