Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 5110

Show
Ignore:
Timestamp:
09/14/06 17:50:46 (2 years ago)
Author:
rick
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r5095 r5110  
    11*SVN* 
     2 
     3* Add chained replace/update support for assert_select_rjs [Rick Olson] 
     4 
     5    Given RJS like... 
     6 
     7      page['test1'].replace "<div id=\"1\">foo</div>" 
     8      page['test2'].replace_html "<div id=\"2\">foo</div>" 
     9 
     10    Test it with... 
     11 
     12      assert_select_rjs :chained_replace 
     13      assert_select_rjs :chained_replace, "test1" 
     14 
     15      assert_select_rjs :chained_replace_html 
     16      assert_select_rjs :chained_replace_html, "test2" 
    217 
    318* Load helpers in alphabetical order for consistency. Resolve cyclic javascript_helper dependency.  #6132, #6178 [choonkeat@gmail.com] 
  • trunk/actionpack/lib/action_controller/assertions/selector_assertions.rb

    r4935 r5110  
    344344      # === Examples 
    345345      # 
    346       #   # Updating the element foo. 
    347       #   assert_select_rjs :update, "foo" 
     346      #   # Replacing the element foo. 
     347      #   # page.replace 'foo', ... 
     348      #   assert_select_rjs :replace, "foo" 
     349      # 
     350      #   # Replacing with the chained RJS proxy. 
     351      #   # page[:foo].replace ... 
     352      #   assert_select_rjs :chained_replace, 'foo' 
    348353      # 
    349354      #   # Inserting into the element bar, top position. 
    350       #   assert_select rjs, :insert, :top, "bar" 
     355      #   assert_select_rjs :insert, :top, "bar" 
    351356      # 
    352357      #   # Changing the element foo, with an image. 
     
    363368      #   assert_select "ol>li", 4 
    364369      def assert_select_rjs(*args, &block) 
    365         arg = args.shift 
     370        rjs_type = nil 
     371        arg      = args.shift 
    366372 
    367373        # If the first argument is a symbol, it's the type of RJS statement we're looking 
     
    369375        # any RJS statement. 
    370376        if arg.is_a?(Symbol) 
    371           if arg == :insert 
     377          rjs_type = arg 
     378          if rjs_type == :insert 
    372379            arg = args.shift 
    373380            insertion = "insert_#{arg}".to_sym 
     
    375382            statement = "(#{RJS_STATEMENTS[insertion]})" 
    376383          else 
    377             raise ArgumentError, "Unknown RJS statement type #{arg}" unless RJS_STATEMENTS[arg
    378             statement = "(#{RJS_STATEMENTS[arg]})" 
     384            raise ArgumentError, "Unknown RJS statement type #{rjs_type}" unless RJS_STATEMENTS[rjs_type
     385            statement = "(#{RJS_STATEMENTS[rjs_type]})" 
    379386          end 
    380387          arg = args.shift 
     
    392399        end 
    393400 
    394         pattern = Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE) 
     401        pattern = 
     402          case rjs_type 
     403            when :chained_replace, :chained_replace_html 
     404              Regexp.new("\\$\\(\"#{id}\"\\)#{statement}\\(#{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE) 
     405            else 
     406              Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE) 
     407          end 
    395408           
    396409        # Duplicate the body since the next step involves destroying it. 
     
    508521        unless const_defined?(:RJS_STATEMENTS) 
    509522          RJS_STATEMENTS = { 
    510             :replace      => /Element\.replace/, 
    511             :replace_html => /Element\.update/ 
     523            :replace              => /Element\.replace/, 
     524            :replace_html         => /Element\.update/, 
     525            :chained_replace      => /\.replace/, 
     526            :chained_replace_html => /\.update/, 
    512527          } 
    513528          RJS_INSERTIONS = [:top, :bottom, :before, :after] 
  • trunk/actionpack/test/controller/assert_select_test.rb

    r4949 r5110  
    341341  end 
    342342 
     343  def test_assert_select_rjs_for_chained_replace 
     344    render_rjs do |page| 
     345      page['test1'].replace "<div id=\"1\">foo</div>" 
     346      page['test2'].replace_html "<div id=\"2\">foo</div>" 
     347      page.insert_html :top, "test3", "<div id=\"3\">foo</div>" 
     348    end 
     349    # Replace. 
     350    assert_select_rjs :chained_replace do 
     351      assert_select "div", 1 
     352      assert_select "#1" 
     353    end 
     354    assert_select_rjs :chained_replace, "test1" do 
     355      assert_select "div", 1 
     356      assert_select "#1" 
     357    end 
     358    assert_raises(AssertionFailedError) { assert_select_rjs :chained_replace, "test2" } 
     359    # Replace HTML. 
     360    assert_select_rjs :chained_replace_html do 
     361      assert_select "div", 1 
     362      assert_select "#2" 
     363    end 
     364    assert_select_rjs :chained_replace_html, "test2" do 
     365      assert_select "div", 1 
     366      assert_select "#2" 
     367    end 
     368    assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" } 
     369  end 
    343370 
    344371  def test_assert_select_rjs_for_insert