forked from bootstrap-ruby/bootstrap_form
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap_checkbox_test.rb
More file actions
331 lines (304 loc) · 14.2 KB
/
bootstrap_checkbox_test.rb
File metadata and controls
331 lines (304 loc) · 14.2 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
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
# frozen_string_literal: true
require_relative "test_helper"
class BootstrapCheckboxTest < ActionView::TestCase
include BootstrapForm::ActionViewExtensions::FormHelper
setup :setup_test_fixture
test "check_box is wrapped correctly" do
expected = <<~HTML
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input" extra="extra arg" 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>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, label: "I agree to the terms", extra: "extra arg")
end
test "check_box empty label" do
expected = <<~HTML
<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">​</label>
</div>
HTML
# ​ is a zero-width space.
assert_equivalent_html expected, @builder.check_box(:terms, label: "​".html_safe)
end
test "disabled check_box has proper wrapper classes" do
expected = <<~HTML
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" disabled="disabled" />
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="1" disabled="disabled" />
<label class="form-check-label" for="user_terms">
I agree to the terms
</label>
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, label: "I agree to the terms", disabled: true)
end
test "check_box label allows html" do
expected = <<~HTML
<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 <a href="#">terms</a>
</label>
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, label: 'I agree to the <a href="#">terms</a>'.html_safe)
end
test "check_box accepts a block to define the label" do
expected = <<~HTML
<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>
HTML
assert_equivalent_html expected, @builder.check_box(:terms) { "I agree to the terms" }
end
test "check_box accepts a custom label class" do
expected = <<~HTML
<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 btn" for="user_terms">
Terms
</label>
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, label_class: "btn")
end
test "check_box 'id' attribute is used to specify label 'for' attribute" do
expected = <<~HTML
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input" id="custom_id" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-label" for="custom_id">
Terms
</label>
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, id: "custom_id")
end
test "check_box responds to checked_value and unchecked_value arguments" do
expected = <<~HTML
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="no" />
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="yes" />
<label class="form-check-label" for="user_terms">
I agree to the terms
</label>
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, { label: "I agree to the terms" }, "yes", "no")
end
test "inline checkboxes" do
expected = <<~HTML
<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>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, label: "I agree to the terms", inline: true)
end
test "inline checkboxes from form layout" 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">
<div class="col">
<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>
</form>
HTML
actual = bootstrap_form_for(@user, layout: :inline) do |f|
f.check_box(:terms, label: "I agree to the terms")
end
assert_equivalent_html expected, actual
end
test "disabled inline check_box" do
expected = <<~HTML
<div class="form-check form-check-inline mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" disabled="disabled" />
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="1" disabled="disabled" />
<label class="form-check-label" for="user_terms">
I agree to the terms
</label>
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, label: "I agree to the terms", inline: true,
disabled: true)
end
test "inline checkboxes with custom label class" do
expected = <<~HTML
<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 btn" for="user_terms">
Terms
</label>
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, inline: true, label_class: "btn")
end
test "check_box skip label" do
expected = <<~HTML
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input position-static" id="user_terms" name="user[terms]" type="checkbox" value="1" />
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, label: "I agree to the terms", skip_label: true)
end
test "check_box hide label" do
expected = <<~HTML
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input position-static" id="user_terms" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-label visually-hidden" for="user_terms">I agree to the terms</label>
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, label: "I agree to the terms", hide_label: true)
end
test "check_box renders error when asked" do
@user.errors.add(:terms, "You must accept the terms.")
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input is-invalid" id="user_terms" aria-describedby="user_terms_feedback" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-label" for="user_terms">
I agree to the terms
</label>
<div class="invalid-feedback" id="user_terms_feedback">You must accept the terms.</div>
</div>
</form>
HTML
actual = bootstrap_form_for(@user) do |f|
f.check_box(:terms, label: "I agree to the terms", error_message: true)
end
assert_equivalent_html expected, actual
end
test "check_box renders error when asked with specified id:" do
@user.errors.add(:terms, "You must accept the terms.")
expected = <<~HTML
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input is-invalid" id="custom-id" aria-describedby="custom-id_feedback" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-label" for="custom-id">
I agree to the terms
</label>
<div class="invalid-feedback" id="custom-id_feedback">You must accept the terms.</div>
</div>
</form>
HTML
actual = bootstrap_form_for(@user) do |f|
f.check_box(:terms, label: "I agree to the terms", error_message: true, id: "custom-id")
end
assert_equivalent_html expected, actual
end
test "check box with custom wrapper class" do
expected = <<~HTML
<div class="form-check mb-3 custom-class">
<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>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, label: "I agree to the terms", wrapper_class: "custom-class")
end
test "check box with custom wrapper class false" do
expected = <<~HTML
<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>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, label: "I agree to the terms", wrapper_class: false)
assert_equivalent_html expected, @builder.check_box(:terms, label: "I agree to the terms", wrapper: { class: false })
end
test "inline check box with custom wrapper class" do
expected = <<~HTML
<div class="form-check form-check-inline mb-3 custom-class">
<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>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, label: "I agree to the terms", inline: true,
wrapper_class: "custom-class")
end
test "a required checkbox" do
expected = <<~HTML
<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]" required="required" type="checkbox" value="1"/>
<label class="form-check-label required" for="user_terms">I agree to the terms</label>
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:terms, label: "I agree to the terms", required: true)
end
test "a required attribute as checkbox" do
expected = <<~HTML
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[email]" type="hidden" value="0"/>
<input class="form-check-input" id="user_email" name="user[email]" required="required" type="checkbox" value="1"/>
<label class="form-check-label" for="user_email">Email</label>
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:email, label: "Email")
end
test "an attribute with required and if is not marked as required" do
expected = <<~HTML
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[status]" type="hidden" value="0"/>
<input class="form-check-input" id="user_status" name="user[status]" type="checkbox" value="1"/>
<label class="form-check-label" for="user_status">Status</label>
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:status, label: "Status")
end
test "an attribute with presence validator and unless is not marked as required" do
expected = <<~HTML
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[misc]" type="hidden" value="0"/>
<input class="form-check-input" id="user_misc" name="user[misc]" type="checkbox" value="1"/>
<label class="form-check-label" for="user_misc">Misc</label>
</div>
HTML
assert_equivalent_html expected, @builder.check_box(:misc)
end
if Rails::VERSION::MAJOR >= 8
test "checkbox alias works" do
expected = <<~HTML
<div class="form-check mb-3">
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
<input class="form-check-input" extra="extra arg" 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>
HTML
assert_equivalent_html expected, @builder.checkbox(:terms, label: "I agree to the terms", extra: "extra arg")
end
end
end