-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathspecial_form_class_models_test.rb
87 lines (78 loc) · 3.98 KB
/
special_form_class_models_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
require_relative "test_helper"
class SpecialFormClassModelsTest < ActionView::TestCase
include BootstrapForm::ActionViewExtensions::FormHelper
test "Anonymous models are supported for form builder" do
user_klass = Class.new(User)
def user_klass.model_name
ActiveModel::Name.new(User)
end
@user = user_klass.new(email: "[email protected]", password: "secret", comments: "my comment")
@builder = BootstrapForm::FormBuilder.new(:user, @user, self, {})
@horizontal_builder =
BootstrapForm::FormBuilder.new(:user, @user, self, layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10")
I18n.backend.store_translations(:en, activerecord: { help: { user: {
password: "A good password should be at least six characters long"
} } })
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="date" />
</div>
HTML
assert_equivalent_html expected, @builder.date_field(:misc)
end
test "Nil models are supported for form builder" do
@user = nil
@builder = BootstrapForm::FormBuilder.new(:user, @user, self, {})
@horizontal_builder =
BootstrapForm::FormBuilder.new(:user, @user, self, layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10")
I18n.backend.store_translations(:en, activerecord: { help: { user: {
password: "A good password should be at least six characters long"
} } })
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="date" />
</div>
HTML
assert_equivalent_html expected, @builder.date_field(:misc)
end
test "Objects without model names are supported for form builder" do
@user = FauxUser.new(email: "[email protected]", password: "secret", comments: "my comment")
@builder = BootstrapForm::FormBuilder.new(:user, @user, self, {})
@horizontal_builder =
BootstrapForm::FormBuilder.new(:user, @user, self, layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10")
I18n.backend.store_translations(:en, activerecord: { help: { faux_user: {
password: "A good password should be at least six characters long"
} } })
expected = <<~HTML
<div class="mb-3">
<label class="form-label" for="user_misc">Misc</label>
<input class="form-control" id="user_misc" name="user[misc]" type="date" />
</div>
HTML
assert_equivalent_html expected, @builder.date_field(:misc)
end
test "ActiveModel objects are supported for form builder" do
@user = ModelUser.new(email: "[email protected]", comments: "my comment")
assert_equal false, @user.valid?
@builder = BootstrapForm::FormBuilder.new(:user, @user, self, {})
@horizontal_builder = BootstrapForm::FormBuilder.new(:user, @user, self, layout: :horizontal, label_col: "col-sm-2",
control_col: "col-sm-10")
I18n.backend.store_translations(:en, activerecord: { help: { faux_user: {
password: "A good password should be at least six characters long"
} } })
expected = <<~HTML
<div class="mb-3">
<div class="field_with_errors">
<label class="form-label required" for="user_password">Password</label>
</div>
<div class="field_with_errors">
<input class="form-control is-invalid" id="user_password" name="user[password]" required="required" type="text">
</div>
<div class="invalid-feedback">can't be blank</div>
</div>
HTML
assert_equivalent_html expected, @builder.text_field(:password)
end
end