-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathbootstrap_form_test.rb
811 lines (731 loc) · 36.2 KB
/
bootstrap_form_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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
require_relative "./test_helper"
class BootstrapFormTest < ActionView::TestCase
include BootstrapForm::ActionViewExtensions::FormHelper
setup :setup_test_fixture
test "default-style forms" do
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'}
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_for(@user) { |_f| nil }
end
test "default-style form fields layout horizontal" do
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 row">
<label class="form-label col-form-label col-sm-2 required" for="user_email">Email</label>
<div class="col-sm-10">
<input class="form-control" id="user_email" name="user[email]" type="email" value="[email protected]" />
</div>
</div>
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-label" for="user_terms">I agree to the terms</label>
</div>
<div class="mb-3 row">
<label class="form-label col-form-label col-sm-2" for="user_misc">Misc</label>
<div class="col-sm-10">
<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>
</div>
<div class="mb-3 row">
<label class="form-label col-form-label col-sm-2" for="user_status">Status</label>
<div class="col-sm-10">
<select class="form-select" id="user_status" name="user[status]">
<option value="1">activated</option>
<option value="2">blocked</option>
</select>
</div>
</div>
</form>
HTML
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
actual = bootstrap_form_for(@user) do |f|
f.email_field(:email, layout: :horizontal)
.concat(f.check_box(:terms, label: "I agree to the terms"))
.concat(f.collection_radio_buttons(:misc, collection, :id, :street, layout: :horizontal))
.concat(f.select(:status, [["activated", 1], ["blocked", 2]], layout: :horizontal))
end
assert_equivalent_xml expected, actual
# See the rendered output at: https://www.bootply.com/S2WFzEYChf
end
test "default-style form fields layout inline" do
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 col-auto g-3">
<label class="form-label mr-sm-2 required" for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" type="email" value="[email protected]" />
</div>
<div class="form-check form-check-inline mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-label" for="user_terms">I agree to the terms</label>
</div>
<div class="mb-3 col-auto g-3">
<label class="form-label mr-sm-2" 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>
<div class="mb-3 col-auto g-3">
<label class="form-label mr-sm-2" for="user_status">Status</label>
<select class="form-select" id="user_status" name="user[status]">
<option value="1">activated</option>
<option value="2">blocked</option>
</select>
</div>
</form>
HTML
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
actual = bootstrap_form_for(@user) do |f|
f.email_field(:email, layout: :inline)
.concat(f.check_box(:terms, label: "I agree to the terms", inline: true))
.concat(f.collection_radio_buttons(:misc, collection, :id, :street, layout: :inline))
.concat(f.select(:status, [["activated", 1], ["blocked", 2]], layout: :inline))
end
assert_equivalent_xml expected, actual
# See the rendered output at: https://www.bootply.com/fH5sF4fcju
# Note that the baseline of the label text to the left of the two radio buttons
# isn't aligned with the text of the radio button labels.
# TODO: Align baseline better.
end
if ::Rails::VERSION::STRING >= "5.1"
# No need to test 5.2 separately for this case, since 5.2 does *not*
# generate a default ID for the form element.
test "default-style forms bootstrap_form_with Rails 5.1+" do
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" method="post" #{'data-remote="true"' if ::Rails::VERSION::STRING < '6.1'}>
#{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'}
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_with(model: @user) { |_f| nil }
end
end
test "inline-style forms" do
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" class="new_user col-auto g-3" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="mb-3">
<label class="form-label mr-sm-2 required" for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" type="email" value="[email protected]" />
</div>
<div class="form-check form-check-inline mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-label" for="user_terms">I agree to the terms</label>
</div>
<div class="mb-3">
<label class="form-label mr-sm-2" 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>
<div class="mb-3">
<label class="form-label mr-sm-2" for="user_status">Status</label>
<select class="form-select" id="user_status" name="user[status]">
<option value="1">activated</option>
<option value="2">blocked</option>
</select>
</div>
</form>
HTML
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
actual = bootstrap_form_for(@user, layout: :inline) do |f|
f.email_field(:email)
.concat(f.check_box(:terms, label: "I agree to the terms"))
.concat(f.collection_radio_buttons(:misc, collection, :id, :street))
.concat(f.select(:status, [["activated", 1], ["blocked", 2]]))
end
assert_equivalent_xml expected, actual
end
test "horizontal-style forms" do
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 row">
<label class="form-label col-form-label col-sm-2 required" for="user_email">Email</label>
<div class="col-sm-10">
<input class="form-control" id="user_email" name="user[email]" type="email" value="[email protected]" />
</div>
</div>
<div class="form-check">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-label" for="user_terms">I agree to the terms</label>
</div>
<div class="mb-3 row">
<label class="form-label col-form-label col-sm-2" for="user_misc">Misc</label>
<div class="col-sm-10">
<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>
</div>
<div class="mb-3 row">
<label class="form-label col-form-label col-sm-2" for="user_status">Status</label>
<div class="col-sm-10">
<select class="form-select" id="user_status" name="user[status]">
<option value="1">activated</option>
<option value="2">blocked</option>
</select>
</div>
</div>
</form>
HTML
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
actual = bootstrap_form_for(@user, layout: :horizontal) do |f|
f.email_field(:email)
.concat(f.check_box(:terms, label: "I agree to the terms"))
.concat(f.collection_radio_buttons(:misc, collection, :id, :street))
.concat(f.select(:status, [["activated", 1], ["blocked", 2]]))
end
assert_equivalent_xml expected, actual
end
test "horizontal-style form fields layout default" do
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 required" for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" type="email" value="[email protected]" />
</div>
<div class="form-check">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-label" for="user_terms">I agree to the terms</label>
</div>
<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>
<div class="mb-3">
<label class="form-label" for="user_status">Status</label>
<select class="form-select" id="user_status" name="user[status]">
<option value="1">activated</option>
<option value="2">blocked</option>
</select>
</div>
</form>
HTML
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
actual = bootstrap_form_for(@user, layout: :horizontal) do |f|
f.email_field(:email, layout: :default)
.concat(f.check_box(:terms, label: "I agree to the terms"))
.concat(f.collection_radio_buttons(:misc, collection, :id, :street, layout: :default))
.concat(f.select(:status, [["activated", 1], ["blocked", 2]], layout: :default))
end
assert_equivalent_xml expected, actual
# See the rendered output at: https://www.bootply.com/4f23be1nLn
end
test "horizontal-style form fields layout inline" do
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 col-auto g-3">
<label class="form-label mr-sm-2 required" for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" type="email" value="[email protected]" />
</div>
<div class="form-check form-check-inline">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-label" for="user_terms">I agree to the terms</label>
</div>
<div class="mb-3 col-auto g-3">
<label class="form-label mr-sm-2" 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>
<div class="mb-3 col-auto g-3">
<label class="form-label mr-sm-2" for="user_status">Status</label>
<select class="form-select" id="user_status" name="user[status]">
<option value="1">activated</option>
<option value="2">blocked</option>
</select>
</div>
</form>
HTML
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
actual = bootstrap_form_for(@user, layout: :horizontal) do |f|
f.email_field(:email, layout: :inline)
.concat(f.check_box(:terms, label: "I agree to the terms", inline: true))
.concat(f.collection_radio_buttons(:misc, collection, :id, :street, layout: :inline))
.concat(f.select(:status, [["activated", 1], ["blocked", 2]], layout: :inline))
end
assert_equivalent_xml expected, actual
# See the rendered output here: https://www.bootply.com/Qby9FC9d3u#
end
test "existing styles aren't clobbered when specifying a form style" do
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" class="my-style" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="mb-3 row">
<label class="form-label col-form-label col-sm-2 required" for="user_email">Email</label>
<div class="col-sm-10">
<input class="form-control" id="user_email" name="user[email]" type="email" value="[email protected]" />
</div>
</div>
</form>
HTML
assert_equivalent_xml expected,
bootstrap_form_for(@user, layout: :horizontal, html: { class: "my-style" }) { |f| f.email_field :email }
end
test "given role attribute should not be covered by default role attribute" do
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="not-a-form">
#{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'}
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_for(@user, html: { role: "not-a-form" }) { |_f| nil }
end
test "allows to set blank default form attributes via configuration" do
BootstrapForm.config.stubs(:default_form_attributes).returns({})
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'}
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_for(@user) { |_f| nil }
end
test "allows to set custom default form attributes via configuration" do
BootstrapForm.config.stubs(:default_form_attributes).returns(foo: "bar")
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" class="new_user" foo="bar" id="new_user" method="post">
#{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'}
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_for(@user) { |_f| nil }
end
test "bootstrap_form_tag acts like a form tag" do
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" method="post">
#{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="mb-3">
<label class="form-label" for="email">Your Email</label>
<input class="form-control" id="email" name="email" type="text" />
</div>
</form>
HTML
assert_equivalent_xml expected,
bootstrap_form_tag(url: "/users") { |f| f.text_field :email, label: "Your Email" }
end
test "bootstrap_form_for does not clobber custom options" do
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 required" for="ID">Email</label>
<input class="form-control" id="ID" name="NAME" type="text" value="[email protected]" />
</div>
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.text_field :email, name: "NAME", id: "ID" }
end
test "bootstrap_form_tag does not clobber custom options" do
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" method="post">
#{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="mb-3">
<label class="form-label" for="ID">Email</label>
<input class="form-control" id="ID" name="NAME" type="text" />
</div>
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_tag(url: "/users") { |f| f.text_field :email, name: "NAME", id: "ID" }
end
test "bootstrap_form_tag allows an empty name for checkboxes" do
if ::Rails::VERSION::STRING >= "5.1"
id = "misc"
name = "misc"
else
id = "_misc"
name = "[misc]"
end
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" method="post">
#{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="form-check mb-3">
<input class="form-check-input" id="#{id}" name="#{name}" type="checkbox" value="1" />
<input #{autocomplete_attr} name="#{name}" type="hidden" value="0" />
<label class="form-check-label" for="#{id}"> Misc</label>
</div>
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_tag(url: "/users") { |f| f.check_box :misc }
end
test "errors display correctly and inline_errors are turned off by default when label_errors is true" do
@user.email = nil
assert @user.invalid?
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 required text-danger" for="user_email">Email can't be blank, is too short (minimum is 5 characters)</label>
<input class="form-control is-invalid" id="user_email" name="user[email]" type="text" />
</div>
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_for(@user, label_errors: true) { |f| f.text_field :email }
end
test "errors display correctly and inline_errors can also be on when label_errors is true" do
@user.email = nil
assert @user.invalid?
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 required text-danger" for="user_email">Email can't be blank, is too short (minimum is 5 characters)</label>
<input class="form-control is-invalid" id="user_email" name="user[email]" type="text" />
<div class="invalid-feedback">can't be blank, is too short (minimum is 5 characters)</span>
</div>
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_for(@user, label_errors: true, inline_errors: true) { |f| f.text_field :email }
end
test "label error messages use humanized attribute names" do
I18n.backend.store_translations(:en, activerecord: { attributes: { user: { email: "Your e-mail address" } } })
@user.email = nil
assert @user.invalid?
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 required text-danger" for="user_email">Your e-mail address can't be blank, is too short (minimum is 5 characters)</label>
<input class="form-control is-invalid" id="user_email" name="user[email]" type="text" />
<div class="invalid-feedback">can't be blank, is too short (minimum is 5 characters)</div>
</div>
</form>
HTML
assert_equivalent_xml expected, bootstrap_form_for(@user, label_errors: true, inline_errors: true) { |f| f.text_field :email }
ensure
I18n.backend.store_translations(:en, activerecord: { attributes: { user: { email: nil } } })
end
test "alert message is wrapped correctly" do
@user.email = nil
assert @user.invalid?
expected = <<~HTML
<div class="alert alert-danger">
<p>Please fix the following errors:</p>
<ul class="rails-bootstrap-forms-error-summary">
<li>Email can't be blank</li>
<li>Email is too short (minimum is 5 characters)</li>
<li>Terms must be accepted</li>
</ul>
</div>
HTML
assert_equivalent_xml expected, @builder.alert_message("Please fix the following errors:")
end
test "changing the class name for the alert message" do
@user.email = nil
assert @user.invalid?
expected = <<~HTML
<div class="my-css-class">
<p>Please fix the following errors:</p>
<ul class="rails-bootstrap-forms-error-summary">
<li>Email can't be blank</li>
<li>Email is too short (minimum is 5 characters)</li>
<li>Terms must be accepted</li>
</ul>
</div>
HTML
assert_equivalent_xml expected, @builder.alert_message("Please fix the following errors:", class: "my-css-class")
end
test "alert_message contains the error summary when inline_errors are turned off" do
@user.email = nil
assert @user.invalid?
output = bootstrap_form_for(@user, inline_errors: false) do |f|
f.alert_message("Please fix the following errors:")
end
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="alert alert-danger">
<p>Please fix the following errors:</p>
<ul class="rails-bootstrap-forms-error-summary">
<li>Email can't be blank</li>
<li>Email is too short (minimum is 5 characters)</li>
<li>Terms must be accepted</li>
</ul>
</div>
</form>
HTML
assert_equivalent_xml expected, output
end
test "alert_message allows the error_summary to be turned off" do
@user.email = nil
assert @user.invalid?
output = bootstrap_form_for(@user, inline_errors: false) do |f|
f.alert_message("Please fix the following errors:", error_summary: false)
end
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="alert alert-danger">
<p>Please fix the following errors:</p>
</div>
</form>
HTML
assert_equivalent_xml expected, output
end
test "alert_message allows the error_summary to be turned on with inline_errors also turned on" do
@user.email = nil
assert @user.invalid?
output = bootstrap_form_for(@user, inline_errors: true) do |f|
f.alert_message("Please fix the following errors:", error_summary: true)
end
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="alert alert-danger">
<p>Please fix the following errors:</p>
<ul class="rails-bootstrap-forms-error-summary">
<li>Email can't be blank</li>
<li>Email is too short (minimum is 5 characters)</li>
<li>Terms must be accepted</li>
</ul>
</div>
</form>
HTML
assert_equivalent_xml expected, output
end
test "error_summary returns an unordered list of errors" do
@user.email = nil
assert @user.invalid?
expected = <<~HTML
<ul class="rails-bootstrap-forms-error-summary">
<li>Email can't be blank</li>
<li>Email is too short (minimum is 5 characters)</li>
<li>Terms must be accepted</li>
</ul>
HTML
assert_equivalent_xml expected, @builder.error_summary
end
test "error_summary returns nothing if no errors" do
@user.terms = true
assert @user.valid?
assert_nil @builder.error_summary
end
test "errors_on renders the errors for a specific attribute when invalid" do
@user.email = nil
assert @user.invalid?
expected = <<~HTML
<div class="invalid-feedback">Email can't be blank, Email is too short (minimum is 5 characters)</div>
HTML
assert_equivalent_xml expected, @builder.errors_on(:email)
end
test "custom label width for horizontal forms" do
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 row">
<label class="form-label col-form-label col-sm-1 required" for="user_email">Email</label>
<div class="col-sm-10">
<input class="form-control" id="user_email" name="user[email]" type="email" value="[email protected]" />
</div>
</div>
</form>
HTML
assert_equivalent_xml expected,
bootstrap_form_for(@user, layout: :horizontal) { |f| f.email_field :email, label_col: "col-sm-1" }
end
test "offset for form group without label respects label width for horizontal forms" do
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 row">
<div class="col-md-10 offset-md-2">
<input class="btn btn-secondary" name="commit" type="submit" value="Create User" />
</div>
</div>
</form>
HTML
assert_equivalent_xml expected,
bootstrap_form_for(@user, layout: :horizontal, label_col: "col-md-2",
control_col: "col-md-10") { |f| f.form_group { f.submit } }
end
test "offset for form group without label respects multiple label widths for horizontal forms" do
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 row">
<div class="col-sm-8 col-md-10 offset-sm-4 offset-md-2">
<input class="btn btn-secondary" name="commit" type="submit" value="Create User" />
</div>
</div>
</form>
HTML
assert_equivalent_xml expected,
bootstrap_form_for(@user, layout: :horizontal, label_col: "col-sm-4 col-md-2",
control_col: "col-sm-8 col-md-10") { |f| f.form_group { f.submit } }
end
test "custom input width for horizontal forms" do
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 row">
<label class="form-label col-form-label col-sm-2 required" for="user_email">Email</label>
<div class="col-sm-5">
<input class="form-control" id="user_email" name="user[email]" type="email" value="[email protected]" />
</div>
</div>
</form>
HTML
assert_equivalent_xml expected,
bootstrap_form_for(@user, layout: :horizontal) { |f| f.email_field :email, control_col: "col-sm-5" }
end
test "additional input col class" do
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 row">
<label class="form-label col-form-label col-sm-2 required" for="user_email">Email</label>
<div class="col-sm-10 custom-class">
<input class="form-control" id="user_email" name="user[email]" type="email" value="[email protected]" />
</div>
</div>
</form>
HTML
actual = bootstrap_form_for(@user,
layout: :horizontal) { |f| f.email_field :email, add_control_col_class: "custom-class" }
assert_equivalent_xml expected, actual
end
test "the field contains the error and is not wrapped in div.field_with_errors when bootstrap_form_for is used" do
@user.email = nil
assert @user.invalid?
output = bootstrap_form_for(@user) do |f|
f.text_field(:email, help: "This is required")
end
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 required" for="user_email">Email</label>
<input class="form-control is-invalid" id="user_email" name="user[email]" type="text" />
<div class="invalid-feedback">can't be blank, is too short (minimum is 5 characters)</div>
<small class="form-text text-muted">This is required</small>
</div>
</form>
HTML
assert_equivalent_xml expected, output
end
test "the field is wrapped with div.field_with_errors when form_for is used" do
@user.email = nil
assert @user.invalid?
output = form_for(@user, builder: BootstrapForm::FormBuilder) do |f|
f.text_field(:email, help: "This is required")
end
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">
<div class="field_with_errors">
<label class="form-label required" for="user_email">Email</label>
</div>
<div class="field_with_errors">
<input class="form-control is-invalid" id="user_email" name="user[email]" type="text" />
</div>
<div class="invalid-feedback">can't be blank, is too short (minimum is 5 characters)</span>
<small class="form-text text-muted">This is required</small>
</div>
</form>
HTML
assert_equivalent_xml expected, output
end
test "help is preserved when inline_errors: false is passed to bootstrap_form_for" do
@user.email = nil
assert @user.invalid?
output = bootstrap_form_for(@user, inline_errors: false) do |f|
f.text_field(:email, help: "This is required")
end
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 required" for="user_email">Email</label>
<input class="form-control is-invalid" id="user_email" name="user[email]" type="text" />
<small class="form-text text-muted">This is required</small>
</div>
</form>
HTML
assert_equivalent_xml expected, output
end
test "help translations do not escape HTML when _html is appended to the name" do
I18n.backend.store_translations(:en, activerecord: { help: { user: { email_html: "This is <strong>useful</strong> help" } } })
output = bootstrap_form_for(@user) do |f|
f.text_field(:email)
end
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 required" for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" type="text" value="[email protected]" />
<small class="form-text text-muted">This is <strong>useful</strong> help</small>
</div>
</form>
HTML
assert_equivalent_xml expected, output
ensure
I18n.backend.store_translations(:en, activerecord: { help: { user: { email_html: nil } } })
end
test "allows the form object to be nil" do
builder = BootstrapForm::FormBuilder.new :other_model, nil, self, {}
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="other_model_email">Email</label>
<input class="form-control" id="other_model_email" name="other_model[email]" type="text" />
</div>
HTML
assert_equivalent_xml expected, builder.text_field(:email)
end
test "errors_on hide attribute name in message" do
@user.email = nil
assert @user.invalid?
expected = '<div class="invalid-feedback">can't be blank, is too short (minimum is 5 characters)</div>'
assert_equivalent_xml expected, @builder.errors_on(:email, hide_attribute_name: true)
end
test "errors_on use custom CSS classes" do
@user.email = nil
assert @user.invalid?
expected = '<div class="custom-error-class">Email can't be blank, Email is too short (minimum is 5 characters)</div>'
assert_equivalent_xml expected, @builder.errors_on(:email, custom_class: "custom-error-class")
end
end