2010-04-24 21:52:51 -04:00
|
|
|
# encoding: utf-8
|
2006-09-03 15:54:21 -04:00
|
|
|
#--
|
|
|
|
# Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
|
|
|
|
# Under MIT and/or CC By license.
|
|
|
|
#++
|
|
|
|
|
2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'controller/fake_controllers'
|
2006-09-03 15:54:21 -04:00
|
|
|
|
2010-03-02 16:41:02 -05:00
|
|
|
require 'action_mailer'
|
2010-01-29 10:26:36 -05:00
|
|
|
ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH
|
2008-08-06 21:08:27 -04:00
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class AssertSelectTest < ActionController::TestCase
|
|
|
|
Assertion = ActiveSupport::TestCase::Assertion
|
|
|
|
|
|
|
|
class AssertSelectMailer < ActionMailer::Base
|
|
|
|
def test(html)
|
2010-08-29 20:08:14 -04:00
|
|
|
mail :body => html, :content_type => "text/html",
|
|
|
|
:subject => "Test e-mail", :from => "test@test.host", :to => "test <test@test.host>"
|
2008-11-07 15:42:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-09-03 15:54:21 -04:00
|
|
|
class AssertSelectController < ActionController::Base
|
|
|
|
def response_with=(content)
|
|
|
|
@content = content
|
|
|
|
end
|
|
|
|
|
|
|
|
def response_with(&block)
|
|
|
|
@update = block
|
|
|
|
end
|
|
|
|
|
|
|
|
def html()
|
|
|
|
render :text=>@content, :layout=>false, :content_type=>Mime::HTML
|
|
|
|
@content = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def rjs()
|
|
|
|
render :update do |page|
|
|
|
|
@update.call page
|
|
|
|
end
|
|
|
|
@update = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def xml()
|
|
|
|
render :text=>@content, :layout=>false, :content_type=>Mime::XML
|
|
|
|
@content = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def rescue_action(e)
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
tests AssertSelectController
|
2006-09-03 15:54:21 -04:00
|
|
|
|
|
|
|
def setup
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2006-09-03 15:54:21 -04:00
|
|
|
ActionMailer::Base.delivery_method = :test
|
|
|
|
ActionMailer::Base.perform_deliveries = true
|
|
|
|
ActionMailer::Base.deliveries = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2006-09-03 15:54:21 -04:00
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
end
|
2008-08-06 21:08:27 -04:00
|
|
|
|
2007-05-26 13:25:19 -04:00
|
|
|
def assert_failure(message, &block)
|
2009-03-08 16:11:58 -04:00
|
|
|
e = assert_raise(Assertion, &block)
|
2007-05-26 13:25:19 -04:00
|
|
|
assert_match(message, e.message) if Regexp === message
|
|
|
|
assert_equal(message, e.message) if String === message
|
|
|
|
end
|
2006-09-03 15:54:21 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Test assert select.
|
|
|
|
#
|
|
|
|
|
|
|
|
def test_assert_select
|
|
|
|
render_html %Q{<div id="1"></div><div id="2"></div>}
|
|
|
|
assert_select "div", 2
|
2007-05-26 13:25:19 -04:00
|
|
|
assert_failure(/Expected at least 1 element matching \"p\", found 0/) { assert_select "p" }
|
2006-09-03 15:54:21 -04:00
|
|
|
end
|
|
|
|
|
2010-05-15 07:26:33 -04:00
|
|
|
def test_equality_integer
|
|
|
|
render_html %Q{<div id="1"></div><div id="2"></div>}
|
|
|
|
assert_failure(/Expected exactly 3 elements matching \"div\", found 2/) { assert_select "div", 3 }
|
|
|
|
assert_failure(/Expected exactly 0 elements matching \"div\", found 2/) { assert_select "div", 0 }
|
|
|
|
end
|
|
|
|
|
2006-09-03 15:54:21 -04:00
|
|
|
def test_equality_true_false
|
|
|
|
render_html %Q{<div id="1"></div><div id="2"></div>}
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "div" }
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select "p" }
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "div", true }
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select "p", true }
|
|
|
|
assert_raise(Assertion) { assert_select "div", false }
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "p", false }
|
2006-09-03 15:54:21 -04:00
|
|
|
end
|
|
|
|
|
2010-05-15 07:26:33 -04:00
|
|
|
def test_equality_false_message
|
|
|
|
render_html %Q{<div id="1"></div><div id="2"></div>}
|
|
|
|
assert_failure(/Expected exactly 0 elements matching \"div\", found 2/) { assert_select "div", false }
|
|
|
|
end
|
|
|
|
|
2006-09-03 15:54:21 -04:00
|
|
|
def test_equality_string_and_regexp
|
|
|
|
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "div", "foo" }
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select "div", "bar" }
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "div", :text=>"foo" }
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select "div", :text=>"bar" }
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "div", /(foo|bar)/ }
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select "div", /foobar/ }
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "div", :text=>/(foo|bar)/ }
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select "div", :text=>/foobar/ }
|
|
|
|
assert_raise(Assertion) { assert_select "p", :text=>/foobar/ }
|
2006-09-03 15:54:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_equality_of_html
|
|
|
|
render_html %Q{<p>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</p>}
|
|
|
|
text = "\"This is not a big problem,\" he said."
|
|
|
|
html = "<em>\"This is <strong>not</strong> a big problem,\"</em> he said."
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "p", text }
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select "p", html }
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "p", :html=>html }
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select "p", :html=>text }
|
2006-09-03 15:54:21 -04:00
|
|
|
# No stripping for pre.
|
|
|
|
render_html %Q{<pre>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</pre>}
|
|
|
|
text = "\n\"This is not a big problem,\" he said.\n"
|
|
|
|
html = "\n<em>\"This is <strong>not</strong> a big problem,\"</em> he said.\n"
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "pre", text }
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select "pre", html }
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "pre", :html=>html }
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select "pre", :html=>text }
|
2006-09-03 15:54:21 -04:00
|
|
|
end
|
|
|
|
|
2007-05-26 13:25:19 -04:00
|
|
|
def test_counts
|
2006-09-03 15:54:21 -04:00
|
|
|
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
|
|
|
|
assert_nothing_raised { assert_select "div", 2 }
|
2010-05-15 07:26:33 -04:00
|
|
|
assert_failure(/Expected exactly 3 elements matching \"div\", found 2/) do
|
2007-05-26 13:25:19 -04:00
|
|
|
assert_select "div", 3
|
|
|
|
end
|
2006-09-03 15:54:21 -04:00
|
|
|
assert_nothing_raised { assert_select "div", 1..2 }
|
2007-05-26 13:25:19 -04:00
|
|
|
assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
|
|
|
|
assert_select "div", 3..4
|
|
|
|
end
|
2006-09-03 15:54:21 -04:00
|
|
|
assert_nothing_raised { assert_select "div", :count=>2 }
|
2010-05-15 07:26:33 -04:00
|
|
|
assert_failure(/Expected exactly 3 elements matching \"div\", found 2/) do
|
2007-05-26 13:25:19 -04:00
|
|
|
assert_select "div", :count=>3
|
|
|
|
end
|
2006-09-03 15:54:21 -04:00
|
|
|
assert_nothing_raised { assert_select "div", :minimum=>1 }
|
|
|
|
assert_nothing_raised { assert_select "div", :minimum=>2 }
|
2007-05-26 13:25:19 -04:00
|
|
|
assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
|
|
|
|
assert_select "div", :minimum=>3
|
|
|
|
end
|
2006-09-03 15:54:21 -04:00
|
|
|
assert_nothing_raised { assert_select "div", :maximum=>2 }
|
|
|
|
assert_nothing_raised { assert_select "div", :maximum=>3 }
|
2007-05-26 13:25:19 -04:00
|
|
|
assert_failure(/Expected at most 1 element matching \"div\", found 2/) do
|
|
|
|
assert_select "div", :maximum=>1
|
|
|
|
end
|
2006-09-03 15:54:21 -04:00
|
|
|
assert_nothing_raised { assert_select "div", :minimum=>1, :maximum=>2 }
|
2007-05-26 13:25:19 -04:00
|
|
|
assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
|
|
|
|
assert_select "div", :minimum=>3, :maximum=>4
|
|
|
|
end
|
2006-09-03 15:54:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_substitution_values
|
|
|
|
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
|
|
|
|
assert_select "div#?", /\d+/ do |elements|
|
|
|
|
assert_equal 2, elements.size
|
|
|
|
end
|
|
|
|
assert_select "div" do
|
|
|
|
assert_select "div#?", /\d+/ do |elements|
|
|
|
|
assert_equal 2, elements.size
|
|
|
|
assert_select "#1"
|
|
|
|
assert_select "#2"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_assert_select
|
|
|
|
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
|
|
|
|
assert_select "div" do |elements|
|
|
|
|
assert_equal 2, elements.size
|
|
|
|
assert_select elements[0], "#1"
|
|
|
|
assert_select elements[1], "#2"
|
|
|
|
end
|
|
|
|
assert_select "div" do
|
|
|
|
assert_select "div" do |elements|
|
|
|
|
assert_equal 2, elements.size
|
|
|
|
# Testing in a group is one thing
|
|
|
|
assert_select "#1,#2"
|
|
|
|
# Testing individually is another.
|
|
|
|
assert_select "#1"
|
|
|
|
assert_select "#2"
|
|
|
|
assert_select "#3", false
|
|
|
|
end
|
|
|
|
end
|
2008-08-06 21:08:27 -04:00
|
|
|
|
2007-05-26 13:25:19 -04:00
|
|
|
assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do
|
|
|
|
assert_select "div" do
|
|
|
|
assert_select "#4"
|
|
|
|
end
|
|
|
|
end
|
2006-09-03 15:54:21 -04:00
|
|
|
end
|
|
|
|
|
2006-10-08 20:20:31 -04:00
|
|
|
def test_assert_select_text_match
|
|
|
|
render_html %Q{<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>}
|
|
|
|
assert_select "div" do
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "div", "foo" }
|
|
|
|
assert_nothing_raised { assert_select "div", "bar" }
|
|
|
|
assert_nothing_raised { assert_select "div", /\w*/ }
|
2010-05-16 03:34:46 -04:00
|
|
|
assert_nothing_raised { assert_select "div", :text => /\w*/, :count=>2 }
|
2010-05-17 11:41:54 -04:00
|
|
|
assert_raise(Assertion) { assert_select "div", :text=>"foo", :count=>2 }
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
|
|
|
|
assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
|
|
|
|
assert_nothing_raised { assert_select "div", :html=>/\w*/ }
|
|
|
|
assert_nothing_raised { assert_select "div", :html=>/\w*/, :count=>2 }
|
2010-05-17 11:41:54 -04:00
|
|
|
assert_raise(Assertion) { assert_select "div", :html=>"<span>foo</span>", :count=>2 }
|
2006-10-08 20:20:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-10-08 21:14:38 -04:00
|
|
|
# With single result.
|
|
|
|
def test_assert_select_from_rjs_with_single_result
|
2006-09-03 15:54:21 -04:00
|
|
|
render_rjs do |page|
|
|
|
|
page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
|
|
|
|
end
|
|
|
|
assert_select "div" do |elements|
|
|
|
|
assert elements.size == 2
|
|
|
|
assert_select "#1"
|
|
|
|
assert_select "#2"
|
|
|
|
end
|
|
|
|
assert_select "div#?", /\d+/ do |elements|
|
|
|
|
assert_select "#1"
|
|
|
|
assert_select "#2"
|
|
|
|
end
|
2006-10-08 21:14:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# With multiple results.
|
|
|
|
def test_assert_select_from_rjs_with_multiple_results
|
2006-09-03 15:54:21 -04:00
|
|
|
render_rjs do |page|
|
|
|
|
page.replace_html "test", "<div id=\"1\">foo</div>"
|
|
|
|
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
|
|
|
end
|
|
|
|
assert_select "div" do |elements|
|
|
|
|
assert elements.size == 2
|
|
|
|
assert_select "#1"
|
|
|
|
assert_select "#2"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-25 06:11:00 -05:00
|
|
|
def test_assert_select_rjs_for_positioned_insert_should_fail_when_mixing_arguments
|
|
|
|
render_rjs do |page|
|
|
|
|
page.insert_html :top, "test1", "<div id=\"1\">foo</div>"
|
|
|
|
page.insert_html :bottom, "test2", "<div id=\"2\">foo</div>"
|
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) {assert_select_rjs :insert, :top, "test2"}
|
2008-12-25 06:11:00 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-05-06 11:23:20 -04:00
|
|
|
def test_assert_select_rjs_for_redirect_to
|
|
|
|
render_rjs do |page|
|
|
|
|
page.redirect_to '/'
|
|
|
|
end
|
|
|
|
assert_select_rjs :redirect, '/'
|
|
|
|
end
|
2008-12-25 06:11:00 -05:00
|
|
|
|
2009-03-07 11:13:34 -05:00
|
|
|
def test_elect_with_xml_namespace_attributes
|
|
|
|
render_html %Q{<link xlink:href="http://nowhere.com"></link>}
|
|
|
|
assert_nothing_raised { assert_select "link[xlink:href=http://nowhere.com]" }
|
2008-12-25 06:11:00 -05:00
|
|
|
end
|
|
|
|
|
2006-09-03 15:54:21 -04:00
|
|
|
#
|
|
|
|
# Test css_select.
|
|
|
|
#
|
|
|
|
|
|
|
|
def test_css_select
|
|
|
|
render_html %Q{<div id="1"></div><div id="2"></div>}
|
2010-05-16 03:34:46 -04:00
|
|
|
assert_equal 2, css_select("div").size
|
|
|
|
assert_equal 0, css_select("p").size
|
2006-09-03 15:54:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_css_select
|
|
|
|
render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
|
|
|
|
assert_select "div#?", /\d+/ do |elements|
|
|
|
|
assert_equal 1, css_select(elements[0], "div").size
|
|
|
|
assert_equal 1, css_select(elements[1], "div").size
|
|
|
|
end
|
|
|
|
assert_select "div" do
|
|
|
|
assert_equal 2, css_select("div").size
|
|
|
|
css_select("div").each do |element|
|
|
|
|
# Testing as a group is one thing
|
|
|
|
assert !css_select("#1,#2").empty?
|
|
|
|
# Testing individually is another
|
|
|
|
assert !css_select("#1").empty?
|
|
|
|
assert !css_select("#2").empty?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-10-08 21:14:38 -04:00
|
|
|
# With one result.
|
|
|
|
def test_css_select_from_rjs_with_single_result
|
2006-09-03 15:54:21 -04:00
|
|
|
render_rjs do |page|
|
|
|
|
page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
|
|
|
|
end
|
|
|
|
assert_equal 2, css_select("div").size
|
|
|
|
assert_equal 1, css_select("#1").size
|
|
|
|
assert_equal 1, css_select("#2").size
|
2006-10-08 21:14:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# With multiple results.
|
|
|
|
def test_css_select_from_rjs_with_multiple_results
|
2006-09-03 15:54:21 -04:00
|
|
|
render_rjs do |page|
|
|
|
|
page.replace_html "test", "<div id=\"1\">foo</div>"
|
|
|
|
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
|
|
|
end
|
2006-10-08 21:14:38 -04:00
|
|
|
|
2006-09-03 15:54:21 -04:00
|
|
|
assert_equal 2, css_select("div").size
|
|
|
|
assert_equal 1, css_select("#1").size
|
|
|
|
assert_equal 1, css_select("#2").size
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Test assert_select_rjs.
|
|
|
|
#
|
|
|
|
|
2006-10-08 21:14:38 -04:00
|
|
|
# Test that we can pick up all statements in the result.
|
|
|
|
def test_assert_select_rjs_picks_up_all_statements
|
2006-09-03 15:54:21 -04:00
|
|
|
render_rjs do |page|
|
|
|
|
page.replace "test", "<div id=\"1\">foo</div>"
|
|
|
|
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
|
|
|
page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
|
|
|
|
end
|
2006-10-08 21:14:38 -04:00
|
|
|
|
2006-09-03 15:54:21 -04:00
|
|
|
found = false
|
|
|
|
assert_select_rjs do
|
|
|
|
assert_select "#1"
|
|
|
|
assert_select "#2"
|
|
|
|
assert_select "#3"
|
|
|
|
found = true
|
|
|
|
end
|
|
|
|
assert found
|
2006-10-08 21:14:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Test that we fail if there is nothing to pick.
|
|
|
|
def test_assert_select_rjs_fails_if_nothing_to_pick
|
|
|
|
render_rjs { }
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select_rjs }
|
2006-09-03 15:54:21 -04:00
|
|
|
end
|
|
|
|
|
2006-09-29 03:39:31 -04:00
|
|
|
def test_assert_select_rjs_with_unicode
|
|
|
|
# Test that non-ascii characters (which are converted into \uXXXX in RJS) are decoded correctly.
|
|
|
|
render_rjs do |page|
|
|
|
|
page.replace "test", "<div id=\"1\">\343\203\201\343\202\261\343\203\203\343\203\210</div>"
|
|
|
|
end
|
|
|
|
assert_select_rjs do
|
2008-04-27 20:52:35 -04:00
|
|
|
str = "#1"
|
|
|
|
assert_select str, :text => "\343\203\201\343\202\261\343\203\203\343\203\210"
|
|
|
|
assert_select str, "\343\203\201\343\202\261\343\203\203\343\203\210"
|
|
|
|
if str.respond_to?(:force_encoding)
|
|
|
|
assert_select str, /\343\203\201..\343\203\210/u
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select str, /\343\203\201.\343\203\210/u }
|
2008-04-27 20:52:35 -04:00
|
|
|
else
|
|
|
|
assert_select str, Regexp.new("\343\203\201..\343\203\210",0,'U')
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select str, Regexp.new("\343\203\201.\343\203\210",0,'U') }
|
2008-04-27 20:52:35 -04:00
|
|
|
end
|
2006-09-29 03:39:31 -04:00
|
|
|
end
|
|
|
|
end
|
2006-09-03 15:54:21 -04:00
|
|
|
|
|
|
|
def test_assert_select_rjs_with_id
|
|
|
|
# Test that we can pick up all statements in the result.
|
|
|
|
render_rjs do |page|
|
|
|
|
page.replace "test1", "<div id=\"1\">foo</div>"
|
|
|
|
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
|
|
|
page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
|
|
|
|
end
|
|
|
|
assert_select_rjs "test1" do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#1"
|
|
|
|
end
|
|
|
|
assert_select_rjs "test2" do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#2"
|
|
|
|
end
|
|
|
|
assert_select_rjs "test3" do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#3"
|
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select_rjs "test4" }
|
2006-09-03 15:54:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_select_rjs_for_replace
|
|
|
|
render_rjs do |page|
|
|
|
|
page.replace "test1", "<div id=\"1\">foo</div>"
|
|
|
|
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
|
|
|
page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
|
|
|
|
end
|
|
|
|
# Replace.
|
|
|
|
assert_select_rjs :replace do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#1"
|
|
|
|
end
|
|
|
|
assert_select_rjs :replace, "test1" do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#1"
|
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select_rjs :replace, "test2" }
|
2006-09-03 15:54:21 -04:00
|
|
|
# Replace HTML.
|
|
|
|
assert_select_rjs :replace_html do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#2"
|
|
|
|
end
|
|
|
|
assert_select_rjs :replace_html, "test2" do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#2"
|
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select_rjs :replace_html, "test1" }
|
2006-09-03 15:54:21 -04:00
|
|
|
end
|
|
|
|
|
2006-09-14 13:50:46 -04:00
|
|
|
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
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select_rjs :chained_replace, "test2" }
|
2006-09-14 13:50:46 -04:00
|
|
|
# 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
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select_rjs :replace_html, "test1" }
|
2006-09-14 13:50:46 -04:00
|
|
|
end
|
2006-09-03 15:54:21 -04:00
|
|
|
|
2006-11-15 07:45:52 -05:00
|
|
|
# Simple remove
|
|
|
|
def test_assert_select_rjs_for_remove
|
|
|
|
render_rjs do |page|
|
|
|
|
page.remove "test1"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select_rjs :remove, "test1"
|
|
|
|
end
|
|
|
|
|
2008-08-29 06:09:51 -04:00
|
|
|
def test_assert_select_rjs_for_remove_offers_useful_error_when_assertion_fails
|
|
|
|
render_rjs do |page|
|
|
|
|
page.remove "test_with_typo"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select_rjs :remove, "test1"
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
rescue Assertion
|
|
|
|
assert_equal "No RJS statement that removes 'test1' was rendered.", $!.message
|
2008-08-29 06:09:51 -04:00
|
|
|
end
|
|
|
|
|
2006-11-15 07:45:52 -05:00
|
|
|
def test_assert_select_rjs_for_remove_ignores_block
|
|
|
|
render_rjs do |page|
|
|
|
|
page.remove "test1"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_select_rjs :remove, "test1" do
|
|
|
|
assert_select "p"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-05-26 13:29:23 -04:00
|
|
|
# Simple show
|
|
|
|
def test_assert_select_rjs_for_show
|
|
|
|
render_rjs do |page|
|
|
|
|
page.show "test1"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select_rjs :show, "test1"
|
|
|
|
end
|
|
|
|
|
2008-08-29 06:09:51 -04:00
|
|
|
def test_assert_select_rjs_for_show_offers_useful_error_when_assertion_fails
|
|
|
|
render_rjs do |page|
|
|
|
|
page.show "test_with_typo"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select_rjs :show, "test1"
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
rescue Assertion
|
|
|
|
assert_equal "No RJS statement that shows 'test1' was rendered.", $!.message
|
2008-08-29 06:09:51 -04:00
|
|
|
end
|
|
|
|
|
2007-05-26 13:29:23 -04:00
|
|
|
def test_assert_select_rjs_for_show_ignores_block
|
|
|
|
render_rjs do |page|
|
|
|
|
page.show "test1"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_select_rjs :show, "test1" do
|
|
|
|
assert_select "p"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-08-06 21:08:27 -04:00
|
|
|
|
2007-05-26 13:29:23 -04:00
|
|
|
# Simple hide
|
|
|
|
def test_assert_select_rjs_for_hide
|
|
|
|
render_rjs do |page|
|
|
|
|
page.hide "test1"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select_rjs :hide, "test1"
|
|
|
|
end
|
|
|
|
|
2008-08-29 06:09:51 -04:00
|
|
|
def test_assert_select_rjs_for_hide_offers_useful_error_when_assertion_fails
|
|
|
|
render_rjs do |page|
|
|
|
|
page.hide "test_with_typo"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select_rjs :hide, "test1"
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
rescue Assertion
|
|
|
|
assert_equal "No RJS statement that hides 'test1' was rendered.", $!.message
|
2008-08-29 06:09:51 -04:00
|
|
|
end
|
|
|
|
|
2007-05-26 13:29:23 -04:00
|
|
|
def test_assert_select_rjs_for_hide_ignores_block
|
|
|
|
render_rjs do |page|
|
|
|
|
page.hide "test1"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_select_rjs :hide, "test1" do
|
|
|
|
assert_select "p"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-08-06 21:08:27 -04:00
|
|
|
|
2007-05-26 13:29:23 -04:00
|
|
|
# Simple toggle
|
|
|
|
def test_assert_select_rjs_for_toggle
|
|
|
|
render_rjs do |page|
|
|
|
|
page.toggle "test1"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select_rjs :toggle, "test1"
|
|
|
|
end
|
|
|
|
|
2008-08-29 06:09:51 -04:00
|
|
|
def test_assert_select_rjs_for_toggle_offers_useful_error_when_assertion_fails
|
|
|
|
render_rjs do |page|
|
|
|
|
page.toggle "test_with_typo"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select_rjs :toggle, "test1"
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
rescue Assertion
|
|
|
|
assert_equal "No RJS statement that toggles 'test1' was rendered.", $!.message
|
2008-08-29 06:09:51 -04:00
|
|
|
end
|
|
|
|
|
2007-05-26 13:29:23 -04:00
|
|
|
def test_assert_select_rjs_for_toggle_ignores_block
|
|
|
|
render_rjs do |page|
|
|
|
|
page.toggle "test1"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_select_rjs :toggle, "test1" do
|
|
|
|
assert_select "p"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-08-06 21:08:27 -04:00
|
|
|
|
2006-10-08 21:14:38 -04:00
|
|
|
# Non-positioned insert.
|
|
|
|
def test_assert_select_rjs_for_nonpositioned_insert
|
2006-09-03 15:54:21 -04:00
|
|
|
render_rjs do |page|
|
|
|
|
page.replace "test1", "<div id=\"1\">foo</div>"
|
|
|
|
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
|
|
|
page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
|
|
|
|
end
|
|
|
|
assert_select_rjs :insert_html do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#3"
|
|
|
|
end
|
|
|
|
assert_select_rjs :insert_html, "test3" do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#3"
|
|
|
|
end
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select_rjs :insert_html, "test1" }
|
2006-10-08 21:14:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Positioned insert.
|
|
|
|
def test_assert_select_rjs_for_positioned_insert
|
2006-09-03 15:54:21 -04:00
|
|
|
render_rjs do |page|
|
|
|
|
page.insert_html :top, "test1", "<div id=\"1\">foo</div>"
|
|
|
|
page.insert_html :bottom, "test2", "<div id=\"2\">foo</div>"
|
|
|
|
page.insert_html :before, "test3", "<div id=\"3\">foo</div>"
|
|
|
|
page.insert_html :after, "test4", "<div id=\"4\">foo</div>"
|
|
|
|
end
|
|
|
|
assert_select_rjs :insert, :top do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#1"
|
|
|
|
end
|
|
|
|
assert_select_rjs :insert, :bottom do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#2"
|
|
|
|
end
|
|
|
|
assert_select_rjs :insert, :before do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#3"
|
|
|
|
end
|
|
|
|
assert_select_rjs :insert, :after do
|
|
|
|
assert_select "div", 1
|
|
|
|
assert_select "#4"
|
|
|
|
end
|
|
|
|
assert_select_rjs :insert_html do
|
|
|
|
assert_select "div", 4
|
|
|
|
end
|
|
|
|
end
|
2008-08-06 21:08:27 -04:00
|
|
|
|
2008-08-07 12:07:53 -04:00
|
|
|
def test_assert_select_rjs_raise_errors
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ArgumentError) { assert_select_rjs(:destroy) }
|
|
|
|
assert_raise(ArgumentError) { assert_select_rjs(:insert, :left) }
|
2008-08-07 12:07:53 -04:00
|
|
|
end
|
|
|
|
|
2006-10-08 21:14:38 -04:00
|
|
|
# Simple selection from a single result.
|
|
|
|
def test_nested_assert_select_rjs_with_single_result
|
2006-09-03 15:54:21 -04:00
|
|
|
render_rjs do |page|
|
|
|
|
page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
|
|
|
|
end
|
2006-10-08 21:14:38 -04:00
|
|
|
|
2006-09-03 15:54:21 -04:00
|
|
|
assert_select_rjs "test" do |elements|
|
|
|
|
assert_equal 2, elements.size
|
|
|
|
assert_select "#1"
|
|
|
|
assert_select "#2"
|
|
|
|
end
|
2006-10-08 21:14:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Deal with two results.
|
|
|
|
def test_nested_assert_select_rjs_with_two_results
|
2006-09-03 15:54:21 -04:00
|
|
|
render_rjs do |page|
|
|
|
|
page.replace_html "test", "<div id=\"1\">foo</div>"
|
|
|
|
page.replace_html "test2", "<div id=\"2\">foo</div>"
|
|
|
|
end
|
2006-10-08 21:14:38 -04:00
|
|
|
|
2006-09-03 15:54:21 -04:00
|
|
|
assert_select_rjs "test" do |elements|
|
|
|
|
assert_equal 1, elements.size
|
|
|
|
assert_select "#1"
|
|
|
|
end
|
2006-10-08 21:14:38 -04:00
|
|
|
|
2006-09-03 15:54:21 -04:00
|
|
|
assert_select_rjs "test2" do |elements|
|
|
|
|
assert_equal 1, elements.size
|
|
|
|
assert_select "#2"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_feed_item_encoded
|
|
|
|
render_xml <<-EOF
|
|
|
|
<rss version="2.0">
|
|
|
|
<channel>
|
|
|
|
<item>
|
|
|
|
<description>
|
|
|
|
<![CDATA[
|
|
|
|
<p>Test 1</p>
|
|
|
|
]]>
|
|
|
|
</description>
|
|
|
|
</item>
|
|
|
|
<item>
|
|
|
|
<description>
|
|
|
|
<![CDATA[
|
|
|
|
<p>Test 2</p>
|
|
|
|
]]>
|
|
|
|
</description>
|
|
|
|
</item>
|
|
|
|
</channel>
|
|
|
|
</rss>
|
|
|
|
EOF
|
|
|
|
assert_select "channel item description" do
|
|
|
|
# Test element regardless of wrapper.
|
|
|
|
assert_select_encoded do
|
|
|
|
assert_select "p", :count=>2, :text=>/Test/
|
|
|
|
end
|
|
|
|
# Test through encoded wrapper.
|
|
|
|
assert_select_encoded do
|
|
|
|
assert_select "encoded p", :count=>2, :text=>/Test/
|
|
|
|
end
|
|
|
|
# Use :root instead (recommended)
|
|
|
|
assert_select_encoded do
|
|
|
|
assert_select ":root p", :count=>2, :text=>/Test/
|
|
|
|
end
|
|
|
|
# Test individually.
|
|
|
|
assert_select "description" do |elements|
|
|
|
|
assert_select_encoded elements[0] do
|
|
|
|
assert_select "p", "Test 1"
|
|
|
|
end
|
|
|
|
assert_select_encoded elements[1] do
|
|
|
|
assert_select "p", "Test 2"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Test that we only un-encode element itself.
|
|
|
|
assert_select "channel item" do
|
|
|
|
assert_select_encoded do
|
|
|
|
assert_select "p", 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Test assert_select_email
|
|
|
|
#
|
|
|
|
|
|
|
|
def test_assert_select_email
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(Assertion) { assert_select_email {} }
|
2010-01-29 10:26:36 -05:00
|
|
|
AssertSelectMailer.test("<div><p>foo</p><p>bar</p></div>").deliver
|
2006-09-03 15:54:21 -04:00
|
|
|
assert_select_email do
|
|
|
|
assert_select "div:root" do
|
|
|
|
assert_select "p:first-child", "foo"
|
|
|
|
assert_select "p:last-child", "bar"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
def render_html(html)
|
|
|
|
@controller.response_with = html
|
|
|
|
get :html
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_rjs(&block)
|
|
|
|
@controller.response_with &block
|
|
|
|
get :rjs
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_xml(xml)
|
|
|
|
@controller.response_with = xml
|
|
|
|
get :xml
|
|
|
|
end
|
2006-09-03 19:17:13 -04:00
|
|
|
end
|