forked from bootstrap-ruby/bootstrap_form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap_radio_button_test.rb
466 lines (422 loc) · 20.5 KB
/
bootstrap_radio_button_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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
require_relative "test_helper"
class BootstrapRadioButtonTest < ActionView::TestCase
include BootstrapForm::ActionViewExtensions::FormHelper
setup :setup_test_fixture
test "radio_button is wrapped correctly" do
expected = <<~HTML
<div class="form-check">
<input class="form-check-input" extra="extra arg" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1">
This is a radio button
</label>
</div>
HTML
assert_equivalent_html expected, @builder.radio_button(:misc, "1", label: "This is a radio button", extra: "extra arg")
end
test "radio_button no label" do
expected = <<~HTML
<div class="form-check">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1">​</label>
</div>
HTML
# ​ is a zero-width space.
assert_equivalent_html expected, @builder.radio_button(:misc, "1", label: "​".html_safe)
end
test "radio_button with error is wrapped correctly" do
@user.errors.add(:misc, "error for test")
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="form-check">
<input class="form-check-input is-invalid" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1">
This is a radio button
</label>
<div class="invalid-feedback">error for test</div>
</div>
</form>
HTML
actual = bootstrap_form_for(@user) do |f|
f.radio_button(:misc, "1", label: "This is a radio button", error_message: true)
end
assert_equivalent_html expected, actual
end
test "radio_button disabled label is set correctly" do
expected = <<~HTML
<div class="form-check disabled">
<input class="form-check-input" disabled="disabled" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1">
This is a radio button
</label>
</div>
HTML
assert_equivalent_html expected, @builder.radio_button(:misc, "1", label: "This is a radio button", disabled: true)
end
test "radio_button label class is set correctly" do
expected = <<~HTML
<div class="form-check">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label btn" for="user_misc_1">
This is a radio button
</label>
</div>
HTML
assert_equivalent_html expected, @builder.radio_button(:misc, "1", label: "This is a radio button", label_class: "btn")
end
test "radio_button 'id' attribute is used to specify label 'for' attribute" do
expected = <<~HTML
<div class="form-check">
<input class="form-check-input" id="custom_id" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="custom_id">
This is a radio button
</label>
</div>
HTML
assert_equivalent_html expected, @builder.radio_button(:misc, "1", label: "This is a radio button", id: "custom_id")
end
test "radio_button inline label is set correctly" do
expected = <<~HTML
<div class="form-check form-check-inline">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1">
This is a radio button
</label>
</div>
HTML
assert_equivalent_html expected, @builder.radio_button(:misc, "1", label: "This is a radio button", inline: true)
end
test "radio_button inline label is set correctly from form level" do
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" class="new_user row row-cols-auto g-3 align-items-center" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="form-check form-check-inline">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1">
This is a radio button
</label>
</div>
</form>
HTML
actual = bootstrap_form_for(@user, layout: :inline) do |f|
f.radio_button(:misc, "1", label: "This is a radio button")
end
assert_equivalent_html expected, actual
end
test "radio_button disabled inline label is set correctly" do
expected = <<~HTML
<div class="form-check form-check-inline disabled">
<input class="form-check-input" disabled="disabled" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1">
This is a radio button
</label>
</div>
HTML
assert_equivalent_html expected,
@builder.radio_button(:misc, "1", label: "This is a radio button", inline: true, disabled: true)
end
test "radio_button inline label class is set correctly" do
expected = <<~HTML
<div class="form-check form-check-inline">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label btn" for="user_misc_1">
This is a radio button
</label>
</div>
HTML
assert_equivalent_html expected,
@builder.radio_button(:misc, "1", label: "This is a radio button", inline: true, label_class: "btn")
end
test "collection_radio_buttons renders the form_group correctly" do
collection = [Address.new(id: 1, street: "Foobar")]
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">This is a radio button collection</label>
<div class="form-check">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1">
Foobar
</label>
</div>
<small class="form-text text-muted">With a help!</small>
</div>
HTML
assert_equivalent_html expected,
@builder.collection_radio_buttons(:misc, collection, :id, :street,
label: "This is a radio button collection", help: "With a help!")
end
test "collection_radio_buttons renders multiple radios correctly" do
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">Misc</label>
<div class="form-check">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1"> Foo</label>
</div>
<div class="form-check">
<input class="form-check-input" id="user_misc_2" name="user[misc]" type="radio" value="2" />
<label class="form-check-label" for="user_misc_2"> Bar</label>
</div>
</div>
HTML
assert_equivalent_html expected, @builder.collection_radio_buttons(:misc, collection, :id, :street)
end
test "collection_radio_buttons renders multiple radios with error correctly" do
@user.errors.add(:misc, "error for test")
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="mb-3">
<label class="form-label" for="user_misc">Misc</label>
<div class="form-check">
<input class="form-check-input is-invalid" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1"> Foo</label>
</div>
<div class="form-check">
<input class="form-check-input is-invalid" id="user_misc_2" name="user[misc]" type="radio" value="2" />
<label class="form-check-label" for="user_misc_2"> Bar</label>
<div class="invalid-feedback">error for test</div>
</div>
</div>
</form>
HTML
actual = bootstrap_form_for(@user) do |f|
f.collection_radio_buttons(:misc, collection, :id, :street)
end
assert_equivalent_html expected, actual
end
test "collection_radio_buttons renders inline radios correctly" do
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">Misc</label>
<div class="form-check form-check-inline">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1"> Foo</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" id="user_misc_2" name="user[misc]" type="radio" value="2" />
<label class="form-check-label" for="user_misc_2"> Bar</label>
</div>
</div>
HTML
assert_equivalent_html expected, @builder.collection_radio_buttons(:misc, collection, :id, :street, inline: true)
end
test "collection_radio_buttons renders with checked option correctly" do
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">Misc</label>
<div class="form-check">
<input class="form-check-input" checked="checked" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1"> Foo</label>
</div>
<div class="form-check">
<input class="form-check-input" id="user_misc_2" name="user[misc]" type="radio" value="2" />
<label class="form-check-label" for="user_misc_2"> Bar</label>
</div>
</div>
HTML
assert_equivalent_html expected, @builder.collection_radio_buttons(:misc, collection, :id, :street, checked: 1)
end
test "collection_radio_buttons renders label defined by Proc correctly" do
collection = [Address.new(id: 1, street: "Foobar")]
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">This is a radio button collection</label>
<div class="form-check">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1"> rabooF</label>
</div>
<small class="form-text text-muted">With a help!</small>
</div>
HTML
assert_equivalent_html expected,
@builder.collection_radio_buttons(:misc, collection, :id, proc { |a| a.street.reverse },
label: "This is a radio button collection", help: "With a help!")
end
test "collection_radio_buttons renders value defined by Proc correctly" do
collection = [Address.new(id: 1, street: "Foobar")]
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">This is a radio button collection</label>
<div class="form-check">
<input class="form-check-input" id="user_misc_address_1" name="user[misc]" type="radio" value="address_1" />
<label class="form-check-label" for="user_misc_address_1"> Foobar</label>
</div>
<small class="form-text text-muted">With a help!</small>
</div>
HTML
assert_equivalent_html expected,
@builder.collection_radio_buttons(:misc, collection, proc { |a| "address_#{a.id}" },
:street, label: "This is a radio button collection",
help: "With a help!")
end
test "collection_radio_buttons renders multiple radios with label defined by Proc correctly" do
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">Misc</label>
<div class="form-check">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1"> ooF</label>
</div>
<div class="form-check">
<input class="form-check-input" id="user_misc_2" name="user[misc]" type="radio" value="2" />
<label class="form-check-label" for="user_misc_2"> raB</label>
</div>
</div>
HTML
assert_equivalent_html expected, @builder.collection_radio_buttons(:misc, collection, :id, proc { |a| a.street.reverse })
end
test "collection_radio_buttons renders multiple radios with value defined by Proc correctly" do
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">Misc</label>
<div class="form-check">
<input class="form-check-input" id="user_misc_address_1" name="user[misc]" type="radio" value="address_1" />
<label class="form-check-label" for="user_misc_address_1"> Foo</label>
</div>
<div class="form-check">
<input class="form-check-input" id="user_misc_address_2" name="user[misc]" type="radio" value="address_2" />
<label class="form-check-label" for="user_misc_address_2"> Bar</label>
</div>
</div>
HTML
assert_equivalent_html expected, @builder.collection_radio_buttons(:misc, collection, proc { |a| "address_#{a.id}" }, :street)
end
test "collection_radio_buttons renders label defined by lambda correctly" do
collection = [Address.new(id: 1, street: "Foobar")]
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">This is a radio button collection</label>
<div class="form-check">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1"> rabooF</label>
</div>
<small class="form-text text-muted">With a help!</small>
</div>
HTML
assert_equivalent_html expected,
@builder.collection_radio_buttons(:misc, collection, :id, ->(a) { a.street.reverse },
label: "This is a radio button collection", help: "With a help!")
end
test "collection_radio_buttons renders value defined by lambda correctly" do
collection = [Address.new(id: 1, street: "Foobar")]
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">This is a radio button collection</label>
<div class="form-check">
<input class="form-check-input" id="user_misc_address_1" name="user[misc]" type="radio" value="address_1" />
<label class="form-check-label" for="user_misc_address_1"> Foobar</label>
</div>
<small class="form-text text-muted">With a help!</small>
</div>
HTML
assert_equivalent_html expected,
@builder.collection_radio_buttons(:misc, collection, ->(a) { "address_#{a.id}" },
:street, label: "This is a radio button collection",
help: "With a help!")
end
test "collection_radio_buttons renders multiple radios with label defined by lambda correctly" do
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">Misc</label>
<div class="form-check">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1"> ooF</label>
</div>
<div class="form-check">
<input class="form-check-input" id="user_misc_2" name="user[misc]" type="radio" value="2" />
<label class="form-check-label" for="user_misc_2"> raB</label>
</div>
</div>
HTML
assert_equivalent_html expected, @builder.collection_radio_buttons(:misc, collection, :id, ->(a) { a.street.reverse })
end
test "collection_radio_buttons renders multiple radios with value defined by lambda correctly" do
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">Misc</label>
<div class="form-check">
<input class="form-check-input" id="user_misc_address_1" name="user[misc]" type="radio" value="address_1" />
<label class="form-check-label" for="user_misc_address_1"> Foo</label>
</div>
<div class="form-check">
<input class="form-check-input" id="user_misc_address_2" name="user[misc]" type="radio" value="address_2" />
<label class="form-check-label" for="user_misc_address_2"> Bar</label>
</div>
</div>
HTML
assert_equivalent_html expected, @builder.collection_radio_buttons(:misc, collection, ->(a) { "address_#{a.id}" }, :street)
end
test "radio button skip label" do
expected = <<~HTML
<div class="form-check">
<input class="form-check-input position-static" id="user_misc_1" name="user[misc]" type="radio" value="1" />
</div>
HTML
assert_equivalent_html expected, @builder.radio_button(:misc, "1", label: "This is a radio button", skip_label: true)
end
test "radio button hide label" do
expected = <<~HTML
<div class="form-check">
<input class="form-check-input position-static" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label visually-hidden" for="user_misc_1">This is a radio button</label>
</div>
HTML
assert_equivalent_html expected, @builder.radio_button(:misc, "1", label: "This is a radio button", hide_label: true)
end
test "radio button with custom wrapper class" do
expected = <<~HTML
<div class="form-check custom-class">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1">
This is a radio button
</label>
</div>
HTML
assert_equivalent_html expected,
@builder.radio_button(:misc, "1", label: "This is a radio button", wrapper_class: "custom-class")
end
test "inline radio button with custom wrapper class" do
expected = <<~HTML
<div class="form-check form-check-inline custom-class">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1">
This is a radio button
</label>
</div>
HTML
assert_equivalent_html expected,
@builder.radio_button(:misc, "1", label: "This is a radio button", inline: true,
wrapper_class: "custom-class")
end
test "a required radiobutton" do
expected = <<~HTML
<div class="form-check">
<input class="form-check-input" id="user_misc_0" name="user[misc]" required="required" type="radio" value="0" />
<label class="form-check-label" for="user_misc_0">
This is a radio button
</label>
</div>
HTML
assert_equivalent_html expected, @builder.radio_button(:misc, "0", label: "This is a radio button", required: true)
end
test "a required attribute as radiobutton" do
expected = <<~HTML
<div class="form-check">
<input class="form-check-input" id="user_email_0" name="user[email]" required="required" type="radio" value="0" />
<label class="form-check-label" for="user_email_0">
This is a radio button
</label>
</div>
HTML
assert_equivalent_html expected, @builder.radio_button(:email, "0", label: "This is a radio button")
end
end