form_helper_test.rb 8.1 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1
require 'test/unit'
2
require 'erb'
D
Initial  
David Heinemeier Hansson 已提交
3
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_helper'
4
require File.dirname(__FILE__) + '/../../../activesupport/lib/active_support/core_ext/hash' #for stringify keys
D
Initial  
David Heinemeier Hansson 已提交
5 6 7 8

class FormHelperTest < Test::Unit::TestCase
  include ActionView::Helpers::FormHelper

9 10 11 12 13 14 15
  silence_warnings do
    Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on, :cost)
    Post.class_eval do
      alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast)
      alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast)
      alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)
    end
16
  end
D
Initial  
David Heinemeier Hansson 已提交
17 18

  def setup
19
    @post = Post.new
D
Initial  
David Heinemeier Hansson 已提交
20 21
    def @post.errors() Class.new{ def on(field) field == "author_name" end }.new end

22
    def @post.id; 123; end
23
    def @post.id_before_type_cast; 123; end
24

D
Initial  
David Heinemeier Hansson 已提交
25 26 27 28 29 30 31 32
    @post.title       = "Hello World"
    @post.author_name = ""
    @post.body        = "Back to the hill and over it again!"
    @post.secret      = 1
    @post.written_on  = Date.new(2004, 6, 15)
  end

  def test_text_field
33
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
34 35
      '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title")
    )
36
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
37 38
      '<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />', password_field("post", "title")
    )
39
    assert_dom_equal(
40
      '<input id="person_name" name="person[name]" size="30" type="password" />', password_field("person", "name")
D
Initial  
David Heinemeier Hansson 已提交
41 42 43 44 45
    )
  end

  def test_text_field_with_escapes
    @post.title = "<b>Hello World</b>"
46
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
47 48 49 50 51
      '<input id="post_title" name="post[title]" size="30" type="text" value="&lt;b&gt;Hello World&lt;/b&gt;" />', text_field("post", "title")
    )
  end

  def test_text_field_with_options
52
    expected = '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />'
53 54
    assert_dom_equal expected, text_field("post", "title", "size" => 35)
    assert_dom_equal expected, text_field("post", "title", :size => 35)
D
Initial  
David Heinemeier Hansson 已提交
55
  end
56

D
Initial  
David Heinemeier Hansson 已提交
57
  def test_text_field_assuming_size
58
    expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />'
59 60
    assert_dom_equal expected, text_field("post", "title", "maxlength" => 35)
    assert_dom_equal expected, text_field("post", "title", :maxlength => 35)
D
Initial  
David Heinemeier Hansson 已提交
61
  end
62

D
Initial  
David Heinemeier Hansson 已提交
63
  def test_check_box
64
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
65 66 67 68
      '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
      check_box("post", "secret")
    )
    @post.secret = 0
69
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
70
      '<input id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
71
      check_box("post", "secret")
D
Initial  
David Heinemeier Hansson 已提交
72
    )
73
    assert_dom_equal(
74
      '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
75 76
      check_box("post", "secret" ,{"checked"=>"checked"})
    )
D
Initial  
David Heinemeier Hansson 已提交
77
    @post.secret = true
78
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
79 80 81 82
      '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
      check_box("post", "secret")
    )
  end
83

84 85
  def test_check_box_with_explicit_checked_and_unchecked_values
    @post.secret = "on"
86
    assert_dom_equal(
87 88 89 90
      '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" /><input name="post[secret]" type="hidden" value="off" />',
      check_box("post", "secret", {}, "on", "off")
    )
  end
91 92

  def test_radio_button
93
    assert_dom_equal('<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
94
      radio_button("post", "title", "Hello World")
95
    )
96
    assert_dom_equal('<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
97
      radio_button("post", "title", "Goodbye World")
98 99
    )
  end
100

101 102 103 104 105 106
  def test_radio_button_is_checked_with_integers
    assert_dom_equal('<input checked="checked" id="post_secret_1" name="post[secret]" type="radio" value="1" />',
      radio_button("post", "secret", "1")
   )
  end

D
Initial  
David Heinemeier Hansson 已提交
107
  def test_text_area
108
    assert_dom_equal(
109
      '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
110 111 112
      text_area("post", "body")
    )
  end
113

D
Initial  
David Heinemeier Hansson 已提交
114 115
  def test_text_area_with_escapes
    @post.body        = "Back to <i>the</i> hill and over it again!"
116
    assert_dom_equal(
117
      '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to &lt;i&gt;the&lt;/i&gt; hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
118 119 120 121 122
      text_area("post", "body")
    )
  end

  def test_date_selects
123
    assert_dom_equal(
124
      '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
125 126 127
      text_area("post", "body")
    )
  end
128

D
Initial  
David Heinemeier Hansson 已提交
129
  def test_explicit_name
130
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
131
      '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess")
132
    )
133
    assert_dom_equal(
134
      '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
135 136
      text_area("post", "body", "name" => "really!")
    )
137
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
138 139 140
      '<input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" /><input name="i mean it" type="hidden" value="0" />',
      check_box("post", "secret", "name" => "i mean it")
    )
141
    assert_dom_equal text_field("post", "title", "name" => "dont guess"),
142
                 text_field("post", "title", :name => "dont guess")
143
    assert_dom_equal text_area("post", "body", "name" => "really!"),
144
                 text_area("post", "body", :name => "really!")
145
    assert_dom_equal check_box("post", "secret", "name" => "i mean it"),
146
                 check_box("post", "secret", :name => "i mean it")
D
Initial  
David Heinemeier Hansson 已提交
147
  end
148

D
Initial  
David Heinemeier Hansson 已提交
149
  def test_explicit_id
150
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
151
      '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess")
152
    )
153
    assert_dom_equal(
154
      '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
155 156
      text_area("post", "body", "id" => "really!")
    )
157
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
158 159 160
      '<input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
      check_box("post", "secret", "id" => "i mean it")
    )
161
    assert_dom_equal text_field("post", "title", "id" => "dont guess"),
162
                 text_field("post", "title", :id => "dont guess")
163
    assert_dom_equal text_area("post", "body", "id" => "really!"),
164
                 text_area("post", "body", :id => "really!")
165
    assert_dom_equal check_box("post", "secret", "id" => "i mean it"),
166
                 check_box("post", "secret", :id => "i mean it")
D
Initial  
David Heinemeier Hansson 已提交
167
  end
168 169 170

  def test_auto_index
    pid = @post.id
171
    assert_dom_equal(
172 173
      "<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title")
    )
174
    assert_dom_equal(
175
      "<textarea cols=\"40\" id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
176 177
      text_area("post[]", "body")
    )
178
    assert_dom_equal(
179 180 181
      "<input checked=\"checked\" id=\"post_#{pid}_secret\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" /><input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" />",
      check_box("post[]", "secret")
    )
182
   assert_dom_equal(
183
"<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />",
184 185
      radio_button("post[]", "title", "Hello World")
    )
186
    assert_dom_equal("<input id=\"post_#{pid}_title_goodbye_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
187 188 189
      radio_button("post[]", "title", "Goodbye World")
    )
  end
D
Initial  
David Heinemeier Hansson 已提交
190
end