/
builder_test.rb
399 lines (307 loc) · 17.1 KB
/
builder_test.rb
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
require 'test_helper'
class BuilderTest < ActionView::TestCase
def with_custom_form_for(object, *args, &block)
with_concat_custom_form_for(object) do |f|
assert f.instance_of?(CustomFormBuilder)
yield f
end
end
def with_collection_radio(object, attribute, collection, value_method, text_method, options={}, html_options={})
with_concat_form_for(object) do |f|
f.collection_radio attribute, collection, value_method, text_method, options, html_options
end
end
def with_collection_check_boxes(object, attribute, collection, value_method, text_method, options={}, html_options={})
with_concat_form_for(object) do |f|
f.collection_check_boxes attribute, collection, value_method, text_method, options, html_options
end
end
# COLLECTION RADIO
test 'collection radio accepts a collection and generate inputs from value method' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
assert_select 'form input[type=radio][value=true]#user_active_true'
assert_select 'form input[type=radio][value=false]#user_active_false'
end
test 'collection radio accepts a collection and generate inputs from label method' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
assert_select 'form label.collection_radio[for=user_active_true]', 'true'
assert_select 'form label.collection_radio[for=user_active_false]', 'false'
end
test 'collection radio handles camelized collection values for labels correctly' do
with_collection_radio @user, :active, ['Yes', 'No'], :to_s, :to_s
assert_select 'form label.collection_radio[for=user_active_yes]', 'Yes'
assert_select 'form label.collection_radio[for=user_active_no]', 'No'
end
test 'colection radio should sanitize collection values for labels correctly' do
with_collection_radio @user, :name, ['$0.99', '$1.99'], :to_s, :to_s
assert_select 'label.collection_radio[for=user_name_099]', '$0.99'
assert_select 'label.collection_radio[for=user_name_199]', '$1.99'
end
test 'collection radio accepts checked item' do
with_collection_radio @user, :active, [[1, true], [0, false]], :last, :first, :checked => true
assert_select 'form input[type=radio][value=true][checked=checked]'
assert_no_select 'form input[type=radio][value=false][checked=checked]'
end
test 'collection radio accepts multiple disabled items' do
collection = [[1, true], [0, false], [2, 'other']]
with_collection_radio @user, :active, collection, :last, :first, :disabled => [true, false]
assert_select 'form input[type=radio][value=true][disabled=disabled]'
assert_select 'form input[type=radio][value=false][disabled=disabled]'
assert_no_select 'form input[type=radio][value=other][disabled=disabled]'
end
test 'collection radio accepts single disable item' do
collection = [[1, true], [0, false]]
with_collection_radio @user, :active, collection, :last, :first, :disabled => true
assert_select 'form input[type=radio][value=true][disabled=disabled]'
assert_no_select 'form input[type=radio][value=false][disabled=disabled]'
end
test 'collection radio accepts html options as input' do
collection = [[1, true], [0, false]]
with_collection_radio @user, :active, collection, :last, :first, {}, :class => 'radio'
assert_select 'form input[type=radio][value=true].radio#user_active_true'
assert_select 'form input[type=radio][value=false].radio#user_active_false'
end
test 'collection radio wraps the collection in the configured collection wrapper tag' do
swap SimpleForm, :collection_wrapper_tag => :ul do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
assert_select 'form ul input[type=radio][value=true]#user_active_true'
assert_select 'form ul input[type=radio][value=false]#user_active_false'
end
end
test 'collection radio wraps the collection in the given collection wrapper tag' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => :ul
assert_select 'form ul input[type=radio][value=true]#user_active_true'
assert_select 'form ul input[type=radio][value=false]#user_active_false'
end
test 'collection radio does not wrap the collection in the explicitly false collection wrapper tag' do
swap SimpleForm, :collection_wrapper_tag => :ul do
with_collection_radio @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => false
assert_no_select 'form ul'
assert_no_select 'form ul'
end
end
test 'collection radio does not wrap the collection in the explicitly nil collection wrapper tag' do
swap SimpleForm, :collection_wrapper_tag => :ul do
with_collection_radio @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => nil
assert_no_select 'form ul'
assert_no_select 'form ul'
end
end
test 'collection radio does not wrap the collection by default' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
assert_no_select 'form ul'
end
test 'collection radio uses the configured class for collection wrapper tag' do
swap SimpleForm, :collection_wrapper_tag => :ul, :collection_wrapper_class => :'inputs-list' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
assert_select 'form ul.inputs-list input[type=radio][value=true]#user_active_true'
assert_select 'form ul.inputs-list input[type=radio][value=false]#user_active_false'
end
end
test 'collection radio uses no class for collection wrapper tag by default' do
swap SimpleForm, :collection_wrapper_tag => :ul do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
assert_no_select 'form ul[class]'
end
end
test 'collection radio wraps each label/radio in the configured item wrapper tag' do
swap SimpleForm, :item_wrapper_tag => :li do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
assert_select 'form li input[type=radio][value=true]#user_active_true'
assert_select 'form li input[type=radio][value=false]#user_active_false'
end
end
test 'collection radio wraps each label/radio in the given item wrapper tag' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => :li
assert_select 'form li input[type=radio][value=true]#user_active_true'
assert_select 'form li input[type=radio][value=false]#user_active_false'
end
test 'collection radio does not wrap each label/radio in the explicitly false item wrapper tag' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => false
assert_no_select 'form span input[type=radio][value=true]#user_active_true'
assert_no_select 'form span input[type=radio][value=false]#user_active_false'
end
test 'collection radio does not wrap each label/radio in the explicitly nil item wrapper tag' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => nil
assert_no_select 'form span input[type=radio][value=true]#user_active_true'
assert_no_select 'form span input[type=radio][value=false]#user_active_false'
end
test 'collection radio wrap items in a span tag by default' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
assert_select 'form span label > input[type=radio][value=true]#user_active_true'
assert_select 'form span label > input[type=radio][value=false]#user_active_false'
end
test 'collection radio wraps input inside the label' do
with_collection_radio @user, :active, [true, false], :to_s, :to_s
assert_select 'form label > input'
end
# COLLECTION CHECK BOX
test 'collection check box accepts a collection and generate a serie of checkboxes for value method' do
collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
with_collection_check_boxes @user, :tag_ids, collection, :id, :name
assert_select "form input[type=hidden][name='user[tag_ids][]'][value=]"
assert_select 'form input#user_tag_ids_1[type=checkbox][value=1]'
assert_select 'form input#user_tag_ids_2[type=checkbox][value=2]'
end
test 'collection check box accepts a collection and generate a serie of checkboxes with labels for label method' do
collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
with_collection_check_boxes @user, :tag_ids, collection, :id, :name
assert_select 'form label.collection_check_boxes[for=user_tag_ids_1]', 'Tag 1'
assert_select 'form label.collection_check_boxes[for=user_tag_ids_2]', 'Tag 2'
end
test 'collection check box handles camelized collection values for labels correctly' do
with_collection_check_boxes @user, :active, ['Yes', 'No'], :to_s, :to_s
assert_select 'form label.collection_check_boxes[for=user_active_yes]', 'Yes'
assert_select 'form label.collection_check_boxes[for=user_active_no]', 'No'
end
test 'colection check box should sanitize collection values for labels correctly' do
with_collection_check_boxes @user, :name, ['$0.99', '$1.99'], :to_s, :to_s
assert_select 'label.collection_check_boxes[for=user_name_099]', '$0.99'
assert_select 'label.collection_check_boxes[for=user_name_199]', '$1.99'
end
test 'collection check box accepts selected values as :checked option' do
collection = (1..3).map{|i| [i, "Tag #{i}"] }
with_collection_check_boxes @user, :tag_ids, collection, :first, :last, :checked => [1, 3]
assert_select 'form input[type=checkbox][value=1][checked=checked]'
assert_select 'form input[type=checkbox][value=3][checked=checked]'
assert_no_select 'form input[type=checkbox][value=2][checked=checked]'
end
test 'collection check box accepts a single checked value' do
collection = (1..3).map{|i| [i, "Tag #{i}"] }
with_collection_check_boxes @user, :tag_ids, collection, :first, :last, :checked => 3
assert_select 'form input[type=checkbox][value=3][checked=checked]'
assert_no_select 'form input[type=checkbox][value=1][checked=checked]'
assert_no_select 'form input[type=checkbox][value=2][checked=checked]'
end
test 'collection check box accepts multiple disabled items' do
collection = (1..3).map{|i| [i, "Tag #{i}"] }
with_collection_check_boxes @user, :tag_ids, collection, :first, :last, :disabled => [1, 3]
assert_select 'form input[type=checkbox][value=1][disabled=disabled]'
assert_select 'form input[type=checkbox][value=3][disabled=disabled]'
assert_no_select 'form input[type=checkbox][value=2][disabled=disabled]'
end
test 'collection check box accepts single disable item' do
collection = (1..3).map{|i| [i, "Tag #{i}"] }
with_collection_check_boxes @user, :tag_ids, collection, :first, :last, :disabled => 1
assert_select 'form input[type=checkbox][value=1][disabled=disabled]'
assert_no_select 'form input[type=checkbox][value=3][disabled=disabled]'
assert_no_select 'form input[type=checkbox][value=2][disabled=disabled]'
end
test 'collection check box accepts a proc to disabled items' do
collection = (1..3).map{|i| [i, "Tag #{i}"] }
with_collection_check_boxes @user, :tag_ids, collection, :first, :last, :disabled => proc { |i| i.first == 1 }
assert_select 'form input[type=checkbox][value=1][disabled=disabled]'
assert_no_select 'form input[type=checkbox][value=3][disabled=disabled]'
assert_no_select 'form input[type=checkbox][value=2][disabled=disabled]'
end
test 'collection check box accepts html options' do
collection = [[1, 'Tag 1'], [2, 'Tag 2']]
with_collection_check_boxes @user, :tag_ids, collection, :first, :last, {}, :class => 'check'
assert_select 'form input.check[type=checkbox][value=1]'
assert_select 'form input.check[type=checkbox][value=2]'
end
test 'collection check box with fields for' do
collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
with_concat_form_for(@user) do |f|
f.fields_for(:post) do |p|
p.collection_check_boxes :tag_ids, collection, :id, :name
end
end
assert_select 'form input#user_post_tag_ids_1[type=checkbox][value=1]'
assert_select 'form input#user_post_tag_ids_2[type=checkbox][value=2]'
assert_select 'form label.collection_check_boxes[for=user_post_tag_ids_1]', 'Tag 1'
assert_select 'form label.collection_check_boxes[for=user_post_tag_ids_2]', 'Tag 2'
end
test 'collection check box wraps the collection in the configured collection wrapper tag' do
swap SimpleForm, :collection_wrapper_tag => :ul do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
assert_select 'form ul input[type=checkbox][value=true]#user_active_true'
assert_select 'form ul input[type=checkbox][value=false]#user_active_false'
end
end
test 'collection check box wraps the collection in the given collection wrapper tag' do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => :ul
assert_select 'form ul input[type=checkbox][value=true]#user_active_true'
assert_select 'form ul input[type=checkbox][value=false]#user_active_false'
end
test 'collection check box does not wrap the collection in the explicitly false collection wrapper tag' do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => false, :item_wrapper_tag => false
assert_select 'form > label > input[type=checkbox][value=true]#user_active_true'
assert_select 'form > label > input[type=checkbox][value=false]#user_active_false'
end
test 'collection check box does not wrap the collection by default' do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
assert_no_select 'form ul'
end
test 'collection check box uses the configured class for collection wrapper tag' do
swap SimpleForm, :collection_wrapper_tag => :ul, :collection_wrapper_class => :'inputs-list' do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
assert_select 'form ul.inputs-list input[type=checkbox][value=true]#user_active_true'
assert_select 'form ul.inputs-list input[type=checkbox][value=false]#user_active_false'
end
end
test 'collection check box uses no class for collection wrapper tag by default' do
swap SimpleForm, :collection_wrapper_tag => :ul do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
assert_no_select 'form ul[class]'
end
end
test 'collection check box wraps each label/radio in the configured item wrapper tag' do
swap SimpleForm, :item_wrapper_tag => :li do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
assert_select 'form li input[type=checkbox][value=true]#user_active_true'
assert_select 'form li input[type=checkbox][value=false]#user_active_false'
end
end
test 'collection check box wraps each label/radio in the given item wrapper tag' do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => :li
assert_select 'form li input[type=checkbox][value=true]#user_active_true'
assert_select 'form li input[type=checkbox][value=false]#user_active_false'
end
test 'collection check box does not wrapp each label/radio in the explicitly false item wrapper tag' do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => false
assert_select 'form > label > input[type=checkbox][value=true]#user_active_true'
assert_select 'form > label > input[type=checkbox][value=false]#user_active_false'
end
test 'collection check box wrap items in a span tag by default' do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
assert_select 'form span label > input[type=checkbox][value=true]#user_active_true'
assert_select 'form span label > input[type=checkbox][value=false]#user_active_false'
end
test 'collection check box wraps input inside the label' do
with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
assert_select 'form label > input'
end
# SIMPLE FIELDS
test 'simple fields for is available and yields an instance of FormBuilder' do
with_concat_form_for(@user) do |f|
f.simple_fields_for(:posts) do |posts_form|
assert posts_form.instance_of?(SimpleForm::FormBuilder)
end
end
end
test 'fields for with a hash like model yeilds an instance of FormBuilder' do
@hash_backed_author = HashBackedAuthor.new
with_concat_form_for(:user) do |f|
f.simple_fields_for(:author, @hash_backed_author) do |author|
assert author.instance_of?(SimpleForm::FormBuilder)
author.input :name
end
end
assert_select "input[name='user[author][name]'][value='hash backed author']"
end
test 'fields for yields an instance of CustomBuilder if main builder is a CustomBuilder' do
with_custom_form_for(:user) do |f|
f.simple_fields_for(:company) do |company|
assert company.instance_of?(CustomFormBuilder)
end
end
end
test 'fields for yields an instance of FormBuilder if it was set in options' do
with_custom_form_for(:user) do |f|
f.simple_fields_for(:company, :builder => SimpleForm::FormBuilder) do |company|
assert company.instance_of?(SimpleForm::FormBuilder)
end
end
end
end