public
Description: A Ruby web application framework
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack.git
Click here to lend your support to: mack and make a donation at www.pledgie.com !
mack / test / unit / view_helpers / form_helpers_spec.rb
100644 299 lines (223 sloc) 12.129 kb
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent.parent + 'spec_helper'
 
describe Mack::ViewHelpers::FormHelpers do
  include Mack::ViewHelpers
  
  class Cop
    attr_accessor :full_name
    attr_accessor :level
    attr_accessor :tos
    attr_accessor :bio_file
  end
 
  before(:each) do
    @cop = Cop.new
    @cop.full_name = "ness"
    @cop.level = 1
    @cop.bio_file = "~/bio.doc"
    @simple = "hi"
    @default_file = "~/resume.doc"
    @select_options = [["one", 1], ["two", 2], ["three", 3]]
  end
  
  describe "check_box" do
    
    it "should create a nested checkbox for a model" do
      check_box(:cop, :tos).should == %{<input id="cop_tos" name="cop[tos]" type="checkbox" />}
    end
    
    it "should create a non-nested checkbox for a simple object" do
      check_box(:simple).should == %{<input checked="checked" id="simple" name="simple" type="checkbox" />}
    end
    
    it "should create a non-nested checkbox for just a symbol" do
      check_box(:unknown).should == %{<input id="unknown" name="unknown" type="checkbox" />}
    end
    
    it "should be checked if the value is true" do
      @cop.tos = true
      check_box(:cop, :tos).should == %{<input checked="checked" id="cop_tos" name="cop[tos]" type="checkbox" />}
    end
    
    it "should be unchecked if the value is false" do
      @cop.tos = false
      check_box(:cop, :tos).should == %{<input id="cop_tos" name="cop[tos]" type="checkbox" />}
    end
    
  end
  
  describe "file_field" do
    
    it "should create a nested file_field for a model" do
      file_field(:cop, :bio_file).should == %{<input id="cop_bio_file" name="cop[bio_file]" type="file" value="~/bio.doc" />}
    end
    
    it "should create a non-nested file_field for a simple object" do
      file_field(:default_file).should == %{<input id="default_file" name="default_file" type="file" value="~/resume.doc" />}
    end
    
    it "should create a non-nested file_field for just a symbol" do
      file_field(:unknown).should == %{<input id="unknown" name="unknown" type="file" />}
    end
 
    it "should create a nested file_field for a model with an empty value if value is false" do
      @cop.bio_file = nil
      file_field(:cop, :bio_file).should == %{<input id="cop_bio_file" name="cop[bio_file]" type="file" value="" />}
    end
    
  end
  
  describe "hidden_field" do
    
    it "should create a nested hidden_field for a model" do
      hidden_field(:cop, :full_name).should == %{<input id="cop_full_name" name="cop[full_name]" type="hidden" value="ness" />}
    end
    
    it "should create a non-nested hidden_field for a simple object" do
      hidden_field(:simple).should == %{<input id="simple" name="simple" type="hidden" value="hi" />}
    end
    
    it "should create a non-nested hidden_field for just a symbol" do
      hidden_field(:unknown).should == %{<input id="unknown" name="unknown" type="hidden" />}
    end
 
    it "should create a nested hidden_field for a model with an empty value if value is false" do
      @cop.full_name = nil
      hidden_field(:cop, :full_name).should == %{<input id="cop_full_name" name="cop[full_name]" type="hidden" value="" />}
    end
    
  end
  
  describe "password_field" do
    
    it "should create a nested password_field for a model" do
      password_field(:cop, :full_name).should == %{<input id="cop_full_name" name="cop[full_name]" type="password" value="ness" />}
    end
    
    it "should create a non-nested password_field for a simple object" do
      password_field(:simple).should == %{<input id="simple" name="simple" type="password" value="hi" />}
    end
    
    it "should create a non-nested password_field for just a symbol" do
      password_field(:unknown).should == %{<input id="unknown" name="unknown" type="password" />}
    end
 
    it "should create a nested password_field for a model with an empty value if value is false" do
      @cop.full_name = nil
      password_field(:cop, :full_name).should == %{<input id="cop_full_name" name="cop[full_name]" type="password" value="" />}
    end
    
  end
  
  describe "image_submit" do
    
    it "should create an image submit tag" do
      image_submit("login.png").should == %{<input src="/images/login.png" type="image" />}
      image_submit("purchase.png", :disabled => true).should == %{<input disabled="disabled" src="/images/purchase.png" type="image" />}
      image_submit("search.png", :class => 'search-button').should == %{<input class="search-button" src="/images/search.png" type="image" />}
    end
    
  end
  
  describe "label_tag" do
    
    it "should create a nested label for a model" do
      label_tag(:cop, :full_name).should == %{<label for="cop_full_name">Full name</label>}
    end
    
    it "should create a non-nested label for a simple object" do
      label_tag(:simple).should == %{<label for="simple">Simple</label>}
    end
    
    it "should create a non-nested label for just a symbol" do
      label_tag(:unknown).should == %{<label for="unknown">Unknown</label>}
    end
    
    it "should create a non-nested label for just a symbol" do
      label_tag(:unknown).should == %{<label for="unknown">Unknown</label>}
    end
    
    it "should create a non-nested label and use :value for it's content" do
      label_tag(:unknown, :value => "My Label").should == %{<label for="unknown">My Label</label>}
    end
    
    it "should create a non-nested label and use :for for it's for" do
      label_tag(:unknown, :for => "my_label").should == %{<label for="my_label">Unknown</label>}
    end
    
  end
  
  describe "radio_button" do
    
    it "should create a nested radio_button for a model" do
      radio_button(:cop, :level).should == %{<input checked="checked" id="cop_level" name="cop[level]" type="radio" value="1" />}
      radio_button(:cop, :level, :value => "twoa").should == %{<input id="cop_level" name="cop[level]" type="radio" value="twoa" />}
    end
    
    it "should create a non-nested radio_button for a simple object" do
      radio_button(:simple).should == %{<input checked="checked" id="simple" name="simple" type="radio" value="hi" />}
      radio_button(:simple, :value => "twob").should == %{<input id="simple" name="simple" type="radio" value="twob" />}
    end
    
    it "should create a non-nested radio_button for just a symbol" do
      radio_button(:unknown).should == %{<input id="unknown" name="unknown" type="radio" value="" />}
      radio_button(:unknown, :value => "twoc").should == %{<input id="unknown" name="unknown" type="radio" value="twoc" />}
      radio_button(:unknown, :value => "twod", :checked => true).should == %{<input checked="checked" id="unknown" name="unknown" type="radio" value="twod" />}
    end
    
  end
  
  describe "select_tag" do
    
    it "should create a nested select tag for a model" do
      select_tag(:cop, :level).should == %{<select id="cop_level" name="cop[level]"></select>}
    end
    
    it "should create a non-nested select tag for a simple model" do
      select_tag(:simple).should == %{<select id="simple" name="simple"></select>}
    end
    
    it "should build the options from a given array of arrays" do
      select_tag(:simple, :options => @select_options).should == %{<select id="simple" name="simple"><option value="1" >one</option><option value="2" >two</option><option value="3" >three</option></select>}
    end
    
    it "should build the options from a given hash" do
      select_tag(:simple, :options => {"one" => 1, "two" => 2, "three" => 3}).should == %{<select id="simple" name="simple"><option value="1" >one</option><option value="3" >three</option><option value="2" >two</option></select>}
    end
    
    it "should mark an option as selected if the model has it seleceted" do
      select_tag(:cop, :level, :options => @select_options).should == %{<select id="cop_level" name="cop[level]"><option value="1" selected>one</option><option value="2" >two</option><option value="3" >three</option></select>}
    end
    
    it "should mark an option as selected if the selected options is available" do
      select_tag(:simple, :options => @select_options, :selected => 1).should == %{<select id="simple" name="simple"><option value="1" selected>one</option><option value="2" >two</option><option value="3" >three</option></select>}
    end
    
  end
  
  describe "text_area" do
    
    it "should create a nested text_area for a model" do
      text_area(:cop, :full_name).should == %{<textarea cols="60" id="cop_full_name" name="cop[full_name]" rows="20">ness</textarea>}
    end
    
    it "should create a non-nested text_area for a simple object" do
      text_area(:simple).should == %{<textarea cols="60" id="simple" name="simple" rows="20">hi</textarea>}
    end
    
    it "should create a non-nested text_area for just a symbol" do
      text_area(:unknown).should == %{<textarea cols="60" id="unknown" name="unknown" rows="20"></textarea>}
      text_area(:unknown, :value => "hi there").should == %{<textarea cols="60" id="unknown" name="unknown" rows="20">hi there</textarea>}
    end
 
    it "should create a nested text_area for a model with an empty value if value is false" do
      @cop.full_name = nil
      text_area(:cop, :full_name).should == %{<textarea cols="60" id="cop_full_name" name="cop[full_name]" rows="20"></textarea>}
    end
    
  end
  
  describe "text_field" do
    
    it "should create a nested text_field for a model" do
      text_field(:cop, :full_name).should == %{<input id="cop_full_name" name="cop[full_name]" type="text" value="ness" />}
    end
    
    it "should create a non-nested text_field for a simple object" do
      text_field(:simple).should == %{<input id="simple" name="simple" type="text" value="hi" />}
    end
    
    it "should create a non-nested text_field for just a symbol" do
      text_field(:unknown).should == %{<input id="unknown" name="unknown" type="text" />}
    end
 
    it "should create a nested text_field for a model with an empty value if value is false" do
      @cop.full_name = nil
      text_field(:cop, :full_name).should == %{<input id="cop_full_name" name="cop[full_name]" type="text" value="" />}
    end
    
  end
  
  
  describe "form" do
    include CommonHelpers
    
    it "should generate proper form tags" do
      tmp = <<-EOF
<% form("http://www.mackframework.com") do %>
Hello
<% end %>
EOF
      erb(tmp).should == "<form action=\"http://www.mackframework.com\" method=\"post\">\nHello\n</form>"
      tmp = <<-EOF
<% form("http://www.mackframework.com", :multipart => true) do %>
Hello
<% end %>
EOF
      erb(tmp).should == "<form action=\"http://www.mackframework.com\" enctype=\"multipart/form-data\" method=\"post\">\nHello\n</form>"
      tmp = <<-EOF
<% form("http://www.mackframework.com", :id => "my_form") do %>
Hello
<% end %>
EOF
      erb(tmp).should == "<form action=\"http://www.mackframework.com\" class=\"my_form\" id=\"my_form\" method=\"post\">\nHello\n</form>"
      
      tmp = <<-EOF
<% form("http://www.mackframework.com", :id => "my_form", :method => :get) do %>
Hello
<% end %>
EOF
      erb(tmp).should == "<form action=\"http://www.mackframework.com\" class=\"my_form\" id=\"my_form\" method=\"get\">\nHello\n</form>"
      tmp = <<-EOF
<% form("http://www.mackframework.com", :id => "my_form", :method => :put) do %>
Hello
<% end %>
EOF
      erb(tmp).should == "<form action=\"http://www.mackframework.com\" class=\"my_form\" id=\"my_form\" method=\"post\">\n<input name=\"_method\" type=\"hidden\" value=\"put\" />\nHello\n</form>"
    end
    
  end
  
  describe "submit_button" do
    
    it "should build a simple submit tag" do
      submit_button.should == %{<input type="submit" value="Submit" />}
    end
    
    it "should allow you to change the value" do
      submit_button("Login").should == %{<input type="submit" value="Login" />}
    end
    
    it "should take options" do
      submit_button("Login", {:class => :foo}).should == %{<input class="foo" type="submit" value="Login" />}
    end
    
  end
  
end