aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/template/record_tag_helper_test.rb
blob: 247704928e28a8aeda6772cf59a563d655e2906b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require 'abstract_unit'
require 'controller/fake_models'

class Post
  extend ActiveModel::Naming
  include ActiveModel::Conversion
  attr_writer :id, :body

  def initialize
    @id = nil
    @body = nil
    super
  end

  def id
     @id || 45
  end

  def body
    super || @body || "What a wonderful world!"
  end
end

class RecordTagHelperTest < ActionView::TestCase
  include RenderERBUtils

  tests ActionView::Helpers::RecordTagHelper

  def setup
    super
    @post = Post.new
    @post.persisted = true
  end

  def test_content_tag_for
    expected = %(<li class="post" id="post_45"></li>)
    actual = content_tag_for(:li, @post) { }
    assert_dom_equal expected, actual
  end

  def test_content_tag_for_prefix
    expected = %(<ul class="archived_post" id="archived_post_45"></ul>)
    actual = content_tag_for(:ul, @post, :archived) { }
    assert_dom_equal expected, actual
  end

  def test_content_tag_for_with_extra_html_options
    expected = %(<tr class="post bar" id="post_45" style='background-color: #f0f0f0'></tr>)
    actual = content_tag_for(:tr, @post, :class => "bar", :style => "background-color: #f0f0f0") { }
    assert_dom_equal expected, actual
  end

  def test_content_tag_for_with_prefix_and_extra_html_options
    expected = %(<tr class="archived_post bar" id="archived_post_45" style='background-color: #f0f0f0'></tr>)
    actual = content_tag_for(:tr, @post, :archived, :class => "bar", :style => "background-color: #f0f0f0") { }
    assert_dom_equal expected, actual
  end

  def test_block_not_in_erb_multiple_calls
    expected = %(<div class="post bar" id="post_45">#{@post.body}</div>)
    actual = div_for(@post, :class => "bar") { @post.body }
    assert_dom_equal expected, actual
    actual = div_for(@post, :class => "bar") { @post.body }
    assert_dom_equal expected, actual
  end

  def test_block_works_with_content_tag_for_in_erb
    expected = %(<tr class="post" id="post_45">#{@post.body}</tr>)
    actual = render_erb("<%= content_tag_for(:tr, @post) do %><%= @post.body %><% end %>")
    assert_dom_equal expected, actual
  end

  def test_div_for_in_erb
    expected = %(<div class="post bar" id="post_45">#{@post.body}</div>)
    actual = render_erb("<%= div_for(@post, :class => 'bar') do %><%= @post.body %><% end %>")
    assert_dom_equal expected, actual
  end

  def test_content_tag_for_collection
    post_1 = Post.new.tap { |post| post.id = 101; post.body = "Hello!"; post.persisted = true }
    post_2 = Post.new.tap { |post| post.id = 102; post.body = "World!"; post.persisted = true }
    expected = %(<li class="post" id="post_101">Hello!</li>\n<li class="post" id="post_102">World!</li>)
    actual = content_tag_for(:li, [post_1, post_2]) { |post| concat post.body }
    assert_dom_equal expected, actual
  end

  def test_div_for_collection
    post_1 = Post.new.tap { |post| post.id = 101; post.body = "Hello!"; post.persisted = true }
    post_2 = Post.new.tap { |post| post.id = 102; post.body = "World!"; post.persisted = true }
    expected = %(<div class="post" id="post_101">Hello!</div>\n<div class="post" id="post_102">World!</div>)
    actual = div_for([post_1, post_2]) { |post| concat post.body }
    assert_dom_equal expected, actual
  end

  def test_content_tag_for_single_record_is_html_safe
    result = div_for(@post, :class => "bar") { concat @post.body }
    assert result.html_safe?
  end

  def test_content_tag_for_collection_is_html_safe
    post_1 = Post.new.tap { |post| post.id = 101; post.body = "Hello!"; post.persisted = true }
    post_2 = Post.new.tap { |post| post.id = 102; post.body = "World!"; post.persisted = true }
    result = content_tag_for(:li, [post_1, post_2]) { |post| concat post.body }
    assert result.html_safe?
  end
end