~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/actionpack/test/controller/assert_select_test.rb

  • Committer: Michael Forrest
  • Date: 2010-10-15 16:28:50 UTC
  • Revision ID: michael.forrest@canonical.com-20101015162850-tj2vchanv0kr0dun
refrozeĀ gems

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#--
 
2
# Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
 
3
# Under MIT and/or CC By license.
 
4
#++
 
5
 
 
6
require 'abstract_unit'
 
7
require 'controller/fake_controllers'
 
8
 
 
9
 
 
10
unless defined?(ActionMailer)
 
11
  begin
 
12
    $:.unshift("#{File.dirname(__FILE__)}/../../../actionmailer/lib")
 
13
    require 'action_mailer'
 
14
  rescue LoadError => e
 
15
    raise unless e.message =~ /action_mailer/
 
16
    require 'rubygems'
 
17
    gem 'actionmailer'
 
18
  end
 
19
end
 
20
 
 
21
ActionMailer::Base.template_root = FIXTURE_LOAD_PATH
 
22
 
 
23
class AssertSelectTest < ActionController::TestCase
 
24
  Assertion = ActiveSupport::TestCase::Assertion
 
25
 
 
26
  class AssertSelectMailer < ActionMailer::Base
 
27
    def test(html)
 
28
      recipients "test <test@test.host>"
 
29
      from "test@test.host"
 
30
      subject "Test e-mail"
 
31
      part :content_type=>"text/html", :body=>html
 
32
    end
 
33
  end
 
34
 
 
35
  class AssertSelectController < ActionController::Base
 
36
    def response_with=(content)
 
37
      @content = content
 
38
    end
 
39
 
 
40
    def response_with(&block)
 
41
      @update = block
 
42
    end
 
43
 
 
44
    def html()
 
45
      render :text=>@content, :layout=>false, :content_type=>Mime::HTML
 
46
      @content = nil
 
47
    end
 
48
 
 
49
    def rjs()
 
50
      render :update do |page|
 
51
        @update.call page
 
52
      end
 
53
      @update = nil
 
54
    end
 
55
 
 
56
    def xml()
 
57
      render :text=>@content, :layout=>false, :content_type=>Mime::XML
 
58
      @content = nil
 
59
    end
 
60
 
 
61
    def rescue_action(e)
 
62
      raise e
 
63
    end
 
64
  end
 
65
 
 
66
  tests AssertSelectController
 
67
 
 
68
  def setup
 
69
    ActionMailer::Base.delivery_method = :test
 
70
    ActionMailer::Base.perform_deliveries = true
 
71
    ActionMailer::Base.deliveries = []
 
72
  end
 
73
 
 
74
  def teardown
 
75
    ActionMailer::Base.deliveries.clear
 
76
  end
 
77
 
 
78
  def assert_failure(message, &block)
 
79
    e = assert_raise(Assertion, &block)
 
80
    assert_match(message, e.message) if Regexp === message
 
81
    assert_equal(message, e.message) if String === message
 
82
  end
 
83
 
 
84
  #
 
85
  # Test assert select.
 
86
  #
 
87
 
 
88
  def test_assert_select
 
89
    render_html %Q{<div id="1"></div><div id="2"></div>}
 
90
    assert_select "div", 2
 
91
    assert_failure(/Expected at least 3 elements matching \"div\", found 2/) { assert_select "div", 3 }
 
92
    assert_failure(/Expected at least 1 element matching \"p\", found 0/) { assert_select "p" }
 
93
  end
 
94
 
 
95
  def test_equality_true_false
 
96
    render_html %Q{<div id="1"></div><div id="2"></div>}
 
97
    assert_nothing_raised    { assert_select "div" }
 
98
    assert_raise(Assertion) { assert_select "p" }
 
99
    assert_nothing_raised    { assert_select "div", true }
 
100
    assert_raise(Assertion) { assert_select "p", true }
 
101
    assert_raise(Assertion) { assert_select "div", false }
 
102
    assert_nothing_raised    { assert_select "p", false }
 
103
  end
 
104
 
 
105
  def test_equality_string_and_regexp
 
106
    render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
 
107
    assert_nothing_raised    { assert_select "div", "foo" }
 
108
    assert_raise(Assertion) { assert_select "div", "bar" }
 
109
    assert_nothing_raised    { assert_select "div", :text=>"foo" }
 
110
    assert_raise(Assertion) { assert_select "div", :text=>"bar" }
 
111
    assert_nothing_raised    { assert_select "div", /(foo|bar)/ }
 
112
    assert_raise(Assertion) { assert_select "div", /foobar/ }
 
113
    assert_nothing_raised    { assert_select "div", :text=>/(foo|bar)/ }
 
114
    assert_raise(Assertion) { assert_select "div", :text=>/foobar/ }
 
115
    assert_raise(Assertion) { assert_select "p", :text=>/foobar/ }
 
116
  end
 
117
 
 
118
  def test_equality_of_html
 
119
    render_html %Q{<p>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</p>}
 
120
    text = "\"This is not a big problem,\" he said."
 
121
    html = "<em>\"This is <strong>not</strong> a big problem,\"</em> he said."
 
122
    assert_nothing_raised    { assert_select "p", text }
 
123
    assert_raise(Assertion) { assert_select "p", html }
 
124
    assert_nothing_raised    { assert_select "p", :html=>html }
 
125
    assert_raise(Assertion) { assert_select "p", :html=>text }
 
126
    # No stripping for pre.
 
127
    render_html %Q{<pre>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</pre>}
 
128
    text = "\n\"This is not a big problem,\" he said.\n"
 
129
    html = "\n<em>\"This is <strong>not</strong> a big problem,\"</em> he said.\n"
 
130
    assert_nothing_raised    { assert_select "pre", text }
 
131
    assert_raise(Assertion) { assert_select "pre", html }
 
132
    assert_nothing_raised    { assert_select "pre", :html=>html }
 
133
    assert_raise(Assertion) { assert_select "pre", :html=>text }
 
134
  end
 
135
 
 
136
  def test_counts
 
137
    render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
 
138
    assert_nothing_raised               { assert_select "div", 2 }
 
139
    assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
 
140
      assert_select "div", 3
 
141
    end
 
142
    assert_nothing_raised               { assert_select "div", 1..2 }
 
143
    assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
 
144
      assert_select "div", 3..4
 
145
    end
 
146
    assert_nothing_raised               { assert_select "div", :count=>2 }
 
147
    assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
 
148
      assert_select "div", :count=>3
 
149
    end
 
150
    assert_nothing_raised               { assert_select "div", :minimum=>1 }
 
151
    assert_nothing_raised               { assert_select "div", :minimum=>2 }
 
152
    assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
 
153
      assert_select "div", :minimum=>3
 
154
    end
 
155
    assert_nothing_raised               { assert_select "div", :maximum=>2 }
 
156
    assert_nothing_raised               { assert_select "div", :maximum=>3 }
 
157
    assert_failure(/Expected at most 1 element matching \"div\", found 2/) do
 
158
      assert_select "div", :maximum=>1
 
159
    end
 
160
    assert_nothing_raised               { assert_select "div", :minimum=>1, :maximum=>2 }
 
161
    assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
 
162
      assert_select "div", :minimum=>3, :maximum=>4
 
163
    end
 
164
  end
 
165
 
 
166
  def test_substitution_values
 
167
    render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
 
168
    assert_select "div#?", /\d+/ do |elements|
 
169
      assert_equal 2, elements.size
 
170
    end
 
171
    assert_select "div" do
 
172
      assert_select "div#?", /\d+/ do |elements|
 
173
        assert_equal 2, elements.size
 
174
        assert_select "#1"
 
175
        assert_select "#2"
 
176
      end
 
177
    end
 
178
  end
 
179
 
 
180
  def test_nested_assert_select
 
181
    render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
 
182
    assert_select "div" do |elements|
 
183
      assert_equal 2, elements.size
 
184
      assert_select elements[0], "#1"
 
185
      assert_select elements[1], "#2"
 
186
    end
 
187
    assert_select "div" do
 
188
      assert_select "div" do |elements|
 
189
        assert_equal 2, elements.size
 
190
        # Testing in a group is one thing
 
191
        assert_select "#1,#2"
 
192
        # Testing individually is another.
 
193
        assert_select "#1"
 
194
        assert_select "#2"
 
195
        assert_select "#3", false
 
196
      end
 
197
    end
 
198
 
 
199
    assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do
 
200
      assert_select "div" do
 
201
        assert_select "#4"
 
202
      end
 
203
    end
 
204
  end
 
205
 
 
206
  def test_assert_select_text_match
 
207
    render_html %Q{<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>}
 
208
    assert_select "div" do
 
209
      assert_nothing_raised    { assert_select "div", "foo" }
 
210
      assert_nothing_raised    { assert_select "div", "bar" }
 
211
      assert_nothing_raised    { assert_select "div", /\w*/ }
 
212
      assert_nothing_raised    { assert_select "div", /\w*/, :count=>2 }
 
213
      assert_raise(Assertion) { assert_select "div", :text=>"foo", :count=>2 }
 
214
      assert_nothing_raised    { assert_select "div", :html=>"<span>bar</span>" }
 
215
      assert_nothing_raised    { assert_select "div", :html=>"<span>bar</span>" }
 
216
      assert_nothing_raised    { assert_select "div", :html=>/\w*/ }
 
217
      assert_nothing_raised    { assert_select "div", :html=>/\w*/, :count=>2 }
 
218
      assert_raise(Assertion) { assert_select "div", :html=>"<span>foo</span>", :count=>2 }
 
219
    end
 
220
  end
 
221
 
 
222
  # With single result.
 
223
  def test_assert_select_from_rjs_with_single_result
 
224
    render_rjs do |page|
 
225
      page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
 
226
    end
 
227
    assert_select "div" do |elements|
 
228
      assert elements.size == 2
 
229
      assert_select "#1"
 
230
      assert_select "#2"
 
231
    end
 
232
    assert_select "div#?", /\d+/ do |elements|
 
233
      assert_select "#1"
 
234
      assert_select "#2"
 
235
    end
 
236
  end
 
237
 
 
238
  # With multiple results.
 
239
  def test_assert_select_from_rjs_with_multiple_results
 
240
    render_rjs do |page|
 
241
      page.replace_html "test", "<div id=\"1\">foo</div>"
 
242
      page.replace_html "test2", "<div id=\"2\">foo</div>"
 
243
    end
 
244
    assert_select "div" do |elements|
 
245
      assert elements.size == 2
 
246
      assert_select "#1"
 
247
      assert_select "#2"
 
248
    end
 
249
  end
 
250
 
 
251
  def test_assert_select_rjs_for_positioned_insert_should_fail_when_mixing_arguments
 
252
    render_rjs do |page|
 
253
      page.insert_html :top, "test1", "<div id=\"1\">foo</div>"
 
254
      page.insert_html :bottom, "test2", "<div id=\"2\">foo</div>"
 
255
    end
 
256
    assert_raise(Assertion) {assert_select_rjs :insert, :top, "test2"}
 
257
  end
 
258
 
 
259
  def test_elect_with_xml_namespace_attributes
 
260
    render_html %Q{<link xlink:href="http://nowhere.com"></link>}
 
261
    assert_nothing_raised { assert_select "link[xlink:href=http://nowhere.com]" }
 
262
  end
 
263
 
 
264
  #
 
265
  # Test css_select.
 
266
  #
 
267
 
 
268
  def test_css_select
 
269
    render_html %Q{<div id="1"></div><div id="2"></div>}
 
270
    assert 2, css_select("div").size
 
271
    assert 0, css_select("p").size
 
272
  end
 
273
 
 
274
  def test_nested_css_select
 
275
    render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
 
276
    assert_select "div#?", /\d+/ do |elements|
 
277
      assert_equal 1, css_select(elements[0], "div").size
 
278
      assert_equal 1, css_select(elements[1], "div").size
 
279
    end
 
280
    assert_select "div" do
 
281
      assert_equal 2, css_select("div").size
 
282
      css_select("div").each do |element|
 
283
        # Testing as a group is one thing
 
284
        assert !css_select("#1,#2").empty?
 
285
        # Testing individually is another
 
286
        assert !css_select("#1").empty?
 
287
        assert !css_select("#2").empty?
 
288
      end
 
289
    end
 
290
  end
 
291
 
 
292
  # With one result.
 
293
  def test_css_select_from_rjs_with_single_result
 
294
    render_rjs do |page|
 
295
      page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
 
296
    end
 
297
    assert_equal 2, css_select("div").size
 
298
    assert_equal 1, css_select("#1").size
 
299
    assert_equal 1, css_select("#2").size
 
300
  end
 
301
 
 
302
  # With multiple results.
 
303
  def test_css_select_from_rjs_with_multiple_results
 
304
    render_rjs do |page|
 
305
      page.replace_html "test", "<div id=\"1\">foo</div>"
 
306
      page.replace_html "test2", "<div id=\"2\">foo</div>"
 
307
    end
 
308
 
 
309
    assert_equal 2, css_select("div").size
 
310
    assert_equal 1, css_select("#1").size
 
311
    assert_equal 1, css_select("#2").size
 
312
  end
 
313
 
 
314
  #
 
315
  # Test assert_select_rjs.
 
316
  #
 
317
 
 
318
  # Test that we can pick up all statements in the result.
 
319
  def test_assert_select_rjs_picks_up_all_statements
 
320
    render_rjs do |page|
 
321
      page.replace "test", "<div id=\"1\">foo</div>"
 
322
      page.replace_html "test2", "<div id=\"2\">foo</div>"
 
323
      page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
 
324
    end
 
325
 
 
326
    found = false
 
327
    assert_select_rjs do
 
328
      assert_select "#1"
 
329
      assert_select "#2"
 
330
      assert_select "#3"
 
331
      found = true
 
332
    end
 
333
    assert found
 
334
  end
 
335
 
 
336
  # Test that we fail if there is nothing to pick.
 
337
  def test_assert_select_rjs_fails_if_nothing_to_pick
 
338
    render_rjs { }
 
339
    assert_raise(Assertion) { assert_select_rjs }
 
340
  end
 
341
 
 
342
  def test_assert_select_rjs_with_unicode
 
343
    # Test that non-ascii characters (which are converted into \uXXXX in RJS) are decoded correctly.
 
344
    render_rjs do |page|
 
345
      page.replace "test", "<div id=\"1\">\343\203\201\343\202\261\343\203\203\343\203\210</div>"
 
346
    end
 
347
    assert_select_rjs do
 
348
      str = "#1"
 
349
      assert_select str, :text => "\343\203\201\343\202\261\343\203\203\343\203\210"
 
350
      assert_select str, "\343\203\201\343\202\261\343\203\203\343\203\210"
 
351
      if str.respond_to?(:force_encoding)
 
352
        str.force_encoding(Encoding::UTF_8)
 
353
        assert_select str, /\343\203\201..\343\203\210/u
 
354
        assert_raise(Assertion) { assert_select str, /\343\203\201.\343\203\210/u }
 
355
      else
 
356
        assert_select str, Regexp.new("\343\203\201..\343\203\210",0,'U')
 
357
        assert_raise(Assertion) { assert_select str, Regexp.new("\343\203\201.\343\203\210",0,'U') }
 
358
      end
 
359
    end
 
360
  end
 
361
 
 
362
  def test_assert_select_rjs_with_id
 
363
    # Test that we can pick up all statements in the result.
 
364
    render_rjs do |page|
 
365
      page.replace "test1", "<div id=\"1\">foo</div>"
 
366
      page.replace_html "test2", "<div id=\"2\">foo</div>"
 
367
      page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
 
368
    end
 
369
    assert_select_rjs "test1" do
 
370
      assert_select "div", 1
 
371
      assert_select "#1"
 
372
    end
 
373
    assert_select_rjs "test2" do
 
374
      assert_select "div", 1
 
375
      assert_select "#2"
 
376
    end
 
377
    assert_select_rjs "test3" do
 
378
      assert_select "div", 1
 
379
      assert_select "#3"
 
380
    end
 
381
    assert_raise(Assertion) { assert_select_rjs "test4" }
 
382
  end
 
383
 
 
384
  def test_assert_select_rjs_for_replace
 
385
    render_rjs do |page|
 
386
      page.replace "test1", "<div id=\"1\">foo</div>"
 
387
      page.replace_html "test2", "<div id=\"2\">foo</div>"
 
388
      page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
 
389
    end
 
390
    # Replace.
 
391
    assert_select_rjs :replace do
 
392
      assert_select "div", 1
 
393
      assert_select "#1"
 
394
    end
 
395
    assert_select_rjs :replace, "test1" do
 
396
      assert_select "div", 1
 
397
      assert_select "#1"
 
398
    end
 
399
    assert_raise(Assertion) { assert_select_rjs :replace, "test2" }
 
400
    # Replace HTML.
 
401
    assert_select_rjs :replace_html do
 
402
      assert_select "div", 1
 
403
      assert_select "#2"
 
404
    end
 
405
    assert_select_rjs :replace_html, "test2" do
 
406
      assert_select "div", 1
 
407
      assert_select "#2"
 
408
    end
 
409
    assert_raise(Assertion) { assert_select_rjs :replace_html, "test1" }
 
410
  end
 
411
 
 
412
  def test_assert_select_rjs_for_chained_replace
 
413
    render_rjs do |page|
 
414
      page['test1'].replace "<div id=\"1\">foo</div>"
 
415
      page['test2'].replace_html "<div id=\"2\">foo</div>"
 
416
      page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
 
417
    end
 
418
    # Replace.
 
419
    assert_select_rjs :chained_replace do
 
420
      assert_select "div", 1
 
421
      assert_select "#1"
 
422
    end
 
423
    assert_select_rjs :chained_replace, "test1" do
 
424
      assert_select "div", 1
 
425
      assert_select "#1"
 
426
    end
 
427
    assert_raise(Assertion) { assert_select_rjs :chained_replace, "test2" }
 
428
    # Replace HTML.
 
429
    assert_select_rjs :chained_replace_html do
 
430
      assert_select "div", 1
 
431
      assert_select "#2"
 
432
    end
 
433
    assert_select_rjs :chained_replace_html, "test2" do
 
434
      assert_select "div", 1
 
435
      assert_select "#2"
 
436
    end
 
437
    assert_raise(Assertion) { assert_select_rjs :replace_html, "test1" }
 
438
  end
 
439
 
 
440
  # Simple remove
 
441
  def test_assert_select_rjs_for_remove
 
442
    render_rjs do |page|
 
443
      page.remove "test1"
 
444
    end
 
445
 
 
446
    assert_select_rjs :remove, "test1"
 
447
  end
 
448
 
 
449
  def test_assert_select_rjs_for_remove_offers_useful_error_when_assertion_fails
 
450
    render_rjs do |page|
 
451
      page.remove "test_with_typo"
 
452
    end
 
453
 
 
454
    assert_select_rjs :remove, "test1"
 
455
 
 
456
  rescue Assertion
 
457
    assert_equal "No RJS statement that removes 'test1' was rendered.", $!.message
 
458
  end
 
459
 
 
460
  def test_assert_select_rjs_for_remove_ignores_block
 
461
    render_rjs do |page|
 
462
      page.remove "test1"
 
463
    end
 
464
 
 
465
    assert_nothing_raised do
 
466
      assert_select_rjs :remove, "test1" do
 
467
        assert_select "p"
 
468
      end
 
469
    end
 
470
  end
 
471
 
 
472
  # Simple show
 
473
  def test_assert_select_rjs_for_show
 
474
    render_rjs do |page|
 
475
      page.show "test1"
 
476
    end
 
477
 
 
478
    assert_select_rjs :show, "test1"
 
479
  end
 
480
 
 
481
  def test_assert_select_rjs_for_show_offers_useful_error_when_assertion_fails
 
482
    render_rjs do |page|
 
483
      page.show "test_with_typo"
 
484
    end
 
485
 
 
486
    assert_select_rjs :show, "test1"
 
487
 
 
488
  rescue Assertion
 
489
    assert_equal "No RJS statement that shows 'test1' was rendered.", $!.message
 
490
  end
 
491
 
 
492
  def test_assert_select_rjs_for_show_ignores_block
 
493
    render_rjs do |page|
 
494
      page.show "test1"
 
495
    end
 
496
 
 
497
    assert_nothing_raised do
 
498
      assert_select_rjs :show, "test1" do
 
499
        assert_select "p"
 
500
      end
 
501
    end
 
502
  end
 
503
 
 
504
  # Simple hide
 
505
  def test_assert_select_rjs_for_hide
 
506
    render_rjs do |page|
 
507
      page.hide "test1"
 
508
    end
 
509
 
 
510
    assert_select_rjs :hide, "test1"
 
511
  end
 
512
 
 
513
  def test_assert_select_rjs_for_hide_offers_useful_error_when_assertion_fails
 
514
    render_rjs do |page|
 
515
      page.hide "test_with_typo"
 
516
    end
 
517
 
 
518
    assert_select_rjs :hide, "test1"
 
519
 
 
520
  rescue Assertion
 
521
    assert_equal "No RJS statement that hides 'test1' was rendered.", $!.message
 
522
  end
 
523
 
 
524
  def test_assert_select_rjs_for_hide_ignores_block
 
525
    render_rjs do |page|
 
526
      page.hide "test1"
 
527
    end
 
528
 
 
529
    assert_nothing_raised do
 
530
      assert_select_rjs :hide, "test1" do
 
531
        assert_select "p"
 
532
      end
 
533
    end
 
534
  end
 
535
 
 
536
  # Simple toggle
 
537
  def test_assert_select_rjs_for_toggle
 
538
    render_rjs do |page|
 
539
      page.toggle "test1"
 
540
    end
 
541
 
 
542
    assert_select_rjs :toggle, "test1"
 
543
  end
 
544
 
 
545
  def test_assert_select_rjs_for_toggle_offers_useful_error_when_assertion_fails
 
546
    render_rjs do |page|
 
547
      page.toggle "test_with_typo"
 
548
    end
 
549
 
 
550
    assert_select_rjs :toggle, "test1"
 
551
 
 
552
  rescue Assertion
 
553
    assert_equal "No RJS statement that toggles 'test1' was rendered.", $!.message
 
554
  end
 
555
 
 
556
  def test_assert_select_rjs_for_toggle_ignores_block
 
557
    render_rjs do |page|
 
558
      page.toggle "test1"
 
559
    end
 
560
 
 
561
    assert_nothing_raised do
 
562
      assert_select_rjs :toggle, "test1" do
 
563
        assert_select "p"
 
564
      end
 
565
    end
 
566
  end
 
567
 
 
568
  # Non-positioned insert.
 
569
  def test_assert_select_rjs_for_nonpositioned_insert
 
570
    render_rjs do |page|
 
571
      page.replace "test1", "<div id=\"1\">foo</div>"
 
572
      page.replace_html "test2", "<div id=\"2\">foo</div>"
 
573
      page.insert_html :top, "test3", "<div id=\"3\">foo</div>"
 
574
    end
 
575
    assert_select_rjs :insert_html do
 
576
      assert_select "div", 1
 
577
      assert_select "#3"
 
578
    end
 
579
    assert_select_rjs :insert_html, "test3" do
 
580
      assert_select "div", 1
 
581
      assert_select "#3"
 
582
    end
 
583
    assert_raise(Assertion) { assert_select_rjs :insert_html, "test1" }
 
584
  end
 
585
 
 
586
  # Positioned insert.
 
587
  def test_assert_select_rjs_for_positioned_insert
 
588
    render_rjs do |page|
 
589
      page.insert_html :top, "test1", "<div id=\"1\">foo</div>"
 
590
      page.insert_html :bottom, "test2", "<div id=\"2\">foo</div>"
 
591
      page.insert_html :before, "test3", "<div id=\"3\">foo</div>"
 
592
      page.insert_html :after, "test4", "<div id=\"4\">foo</div>"
 
593
    end
 
594
    assert_select_rjs :insert, :top do
 
595
      assert_select "div", 1
 
596
      assert_select "#1"
 
597
    end
 
598
    assert_select_rjs :insert, :bottom do
 
599
      assert_select "div", 1
 
600
      assert_select "#2"
 
601
    end
 
602
    assert_select_rjs :insert, :before do
 
603
      assert_select "div", 1
 
604
      assert_select "#3"
 
605
    end
 
606
    assert_select_rjs :insert, :after do
 
607
      assert_select "div", 1
 
608
      assert_select "#4"
 
609
    end
 
610
    assert_select_rjs :insert_html do
 
611
      assert_select "div", 4
 
612
    end
 
613
  end
 
614
 
 
615
  def test_assert_select_rjs_raise_errors
 
616
    assert_raise(ArgumentError) { assert_select_rjs(:destroy) }
 
617
    assert_raise(ArgumentError) { assert_select_rjs(:insert, :left) }
 
618
  end
 
619
 
 
620
  # Simple selection from a single result.
 
621
  def test_nested_assert_select_rjs_with_single_result
 
622
    render_rjs do |page|
 
623
      page.replace_html "test", "<div id=\"1\">foo</div>\n<div id=\"2\">foo</div>"
 
624
    end
 
625
 
 
626
    assert_select_rjs "test" do |elements|
 
627
      assert_equal 2, elements.size
 
628
      assert_select "#1"
 
629
      assert_select "#2"
 
630
    end
 
631
  end
 
632
 
 
633
  # Deal with two results.
 
634
  def test_nested_assert_select_rjs_with_two_results
 
635
    render_rjs do |page|
 
636
      page.replace_html "test", "<div id=\"1\">foo</div>"
 
637
      page.replace_html "test2", "<div id=\"2\">foo</div>"
 
638
    end
 
639
 
 
640
    assert_select_rjs "test" do |elements|
 
641
      assert_equal 1, elements.size
 
642
      assert_select "#1"
 
643
    end
 
644
 
 
645
    assert_select_rjs "test2" do |elements|
 
646
      assert_equal 1, elements.size
 
647
      assert_select "#2"
 
648
    end
 
649
  end
 
650
 
 
651
  def test_feed_item_encoded
 
652
    render_xml <<-EOF
 
653
<rss version="2.0">
 
654
  <channel>
 
655
    <item>
 
656
      <description>
 
657
        <![CDATA[
 
658
          <p>Test 1</p>
 
659
        ]]>
 
660
      </description>
 
661
    </item>
 
662
    <item>
 
663
      <description>
 
664
        <![CDATA[
 
665
          <p>Test 2</p>
 
666
        ]]>
 
667
      </description>
 
668
    </item>
 
669
  </channel>
 
670
</rss>
 
671
EOF
 
672
    assert_select "channel item description" do
 
673
      # Test element regardless of wrapper.
 
674
      assert_select_encoded do
 
675
        assert_select "p", :count=>2, :text=>/Test/
 
676
      end
 
677
      # Test through encoded wrapper.
 
678
      assert_select_encoded do
 
679
        assert_select "encoded p", :count=>2, :text=>/Test/
 
680
      end
 
681
      # Use :root instead (recommended)
 
682
      assert_select_encoded do
 
683
        assert_select ":root p", :count=>2, :text=>/Test/
 
684
      end
 
685
      # Test individually.
 
686
      assert_select "description" do |elements|
 
687
        assert_select_encoded elements[0] do
 
688
          assert_select "p", "Test 1"
 
689
        end
 
690
        assert_select_encoded elements[1] do
 
691
          assert_select "p", "Test 2"
 
692
        end
 
693
      end
 
694
    end
 
695
 
 
696
    # Test that we only un-encode element itself.
 
697
    assert_select "channel item" do
 
698
      assert_select_encoded do
 
699
        assert_select "p", 0
 
700
      end
 
701
    end
 
702
  end
 
703
 
 
704
  #
 
705
  # Test assert_select_email
 
706
  #
 
707
 
 
708
  def test_assert_select_email
 
709
    assert_raise(Assertion) { assert_select_email {} }
 
710
    AssertSelectMailer.deliver_test "<div><p>foo</p><p>bar</p></div>"
 
711
    assert_select_email do
 
712
      assert_select "div:root" do
 
713
        assert_select "p:first-child", "foo"
 
714
        assert_select "p:last-child", "bar"
 
715
      end
 
716
    end
 
717
  end
 
718
 
 
719
  protected
 
720
    def render_html(html)
 
721
      @controller.response_with = html
 
722
      get :html
 
723
    end
 
724
 
 
725
    def render_rjs(&block)
 
726
      @controller.response_with &block
 
727
      get :rjs
 
728
    end
 
729
 
 
730
    def render_xml(xml)
 
731
      @controller.response_with = xml
 
732
      get :xml
 
733
    end
 
734
end