simple_form 5.0.0 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +49 -8
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +96 -51
  5. data/lib/generators/simple_form/install_generator.rb +2 -2
  6. data/lib/generators/simple_form/templates/README +2 -3
  7. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +2 -2
  8. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +127 -195
  9. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +1 -1
  10. data/lib/simple_form/components/label_input.rb +1 -1
  11. data/lib/simple_form/components/labels.rb +3 -5
  12. data/lib/simple_form/components/maxlength.rb +0 -4
  13. data/lib/simple_form/components/minlength.rb +0 -4
  14. data/lib/simple_form/form_builder.rb +7 -10
  15. data/lib/simple_form/inputs/base.rb +0 -3
  16. data/lib/simple_form/inputs/boolean_input.rb +6 -2
  17. data/lib/simple_form/inputs/collection_check_boxes_input.rb +1 -1
  18. data/lib/simple_form/inputs/collection_input.rb +3 -5
  19. data/lib/simple_form/inputs/grouped_collection_select_input.rb +1 -1
  20. data/lib/simple_form/inputs/priority_input.rb +16 -2
  21. data/lib/simple_form/inputs/rich_text_area_input.rb +12 -0
  22. data/lib/simple_form/inputs.rb +1 -0
  23. data/lib/simple_form/railtie.rb +4 -0
  24. data/lib/simple_form/version.rb +1 -1
  25. data/lib/simple_form/wrappers/leaf.rb +1 -1
  26. data/lib/simple_form/wrappers/root.rb +8 -3
  27. data/lib/simple_form.rb +10 -6
  28. metadata +22 -93
  29. data/lib/simple_form/i18n_cache.rb +0 -23
  30. data/test/action_view_extensions/builder_test.rb +0 -634
  31. data/test/action_view_extensions/form_helper_test.rb +0 -172
  32. data/test/components/custom_components_test.rb +0 -62
  33. data/test/components/label_test.rb +0 -356
  34. data/test/form_builder/association_test.rb +0 -252
  35. data/test/form_builder/button_test.rb +0 -48
  36. data/test/form_builder/error_notification_test.rb +0 -80
  37. data/test/form_builder/error_test.rb +0 -281
  38. data/test/form_builder/general_test.rb +0 -534
  39. data/test/form_builder/hint_test.rb +0 -150
  40. data/test/form_builder/input_field_test.rb +0 -195
  41. data/test/form_builder/label_test.rb +0 -136
  42. data/test/form_builder/wrapper_test.rb +0 -371
  43. data/test/generators/simple_form_generator_test.rb +0 -43
  44. data/test/inputs/boolean_input_test.rb +0 -243
  45. data/test/inputs/collection_check_boxes_input_test.rb +0 -327
  46. data/test/inputs/collection_radio_buttons_input_test.rb +0 -450
  47. data/test/inputs/collection_select_input_test.rb +0 -378
  48. data/test/inputs/color_input_test.rb +0 -10
  49. data/test/inputs/datetime_input_test.rb +0 -176
  50. data/test/inputs/disabled_test.rb +0 -92
  51. data/test/inputs/discovery_test.rb +0 -142
  52. data/test/inputs/file_input_test.rb +0 -17
  53. data/test/inputs/general_test.rb +0 -133
  54. data/test/inputs/grouped_collection_select_input_test.rb +0 -183
  55. data/test/inputs/hidden_input_test.rb +0 -32
  56. data/test/inputs/numeric_input_test.rb +0 -177
  57. data/test/inputs/priority_input_test.rb +0 -50
  58. data/test/inputs/readonly_test.rb +0 -102
  59. data/test/inputs/required_test.rb +0 -158
  60. data/test/inputs/string_input_test.rb +0 -158
  61. data/test/inputs/text_input_test.rb +0 -37
  62. data/test/simple_form_test.rb +0 -18
  63. data/test/support/discovery_inputs.rb +0 -65
  64. data/test/support/misc_helpers.rb +0 -274
  65. data/test/support/mock_controller.rb +0 -30
  66. data/test/support/models.rb +0 -353
  67. data/test/test_helper.rb +0 -95
@@ -1,534 +0,0 @@
1
- # frozen_string_literal: true
2
- # encoding: UTF-8
3
- require 'test_helper'
4
-
5
- class FormBuilderTest < ActionView::TestCase
6
- def with_custom_form_for(object, *args, &block)
7
- with_concat_custom_form_for(object) do |f|
8
- f.input(*args, &block)
9
- end
10
- end
11
-
12
- test 'nested simple fields yields an instance of FormBuilder' do
13
- simple_form_for :user do |f|
14
- f.simple_fields_for :posts do |posts_form|
15
- assert posts_form.instance_of?(SimpleForm::FormBuilder)
16
- end
17
- end
18
- end
19
-
20
- test 'builder input is html safe' do
21
- simple_form_for @user do |f|
22
- assert f.input(:name).html_safe?
23
- end
24
- end
25
-
26
- test 'builder works without controller' do
27
- stub_any_instance ActionView::TestCase, :controller, nil do
28
- simple_form_for @user do |f|
29
- assert f.input(:name)
30
- end
31
- end
32
- end
33
-
34
- test 'builder works with decorated object responsive to #to_model' do
35
- assert_nothing_raised do
36
- with_form_for @decorated_user, :name
37
- end
38
- end
39
-
40
- test 'builder input allows a block to configure input' do
41
- with_form_for @user, :name do
42
- text_field_tag :foo, :bar, id: :cool
43
- end
44
- assert_no_select 'input.string'
45
- assert_select 'input#cool'
46
- end
47
-
48
- test 'builder allows adding custom input mappings for default input types' do
49
- swap SimpleForm, input_mappings: { /count$/ => :integer } do
50
- with_form_for @user, :post_count
51
- assert_no_select 'form input#user_post_count.string'
52
- assert_select 'form input#user_post_count.numeric.integer'
53
- end
54
- end
55
-
56
- test 'builder does not override custom input mappings for custom collection' do
57
- swap SimpleForm, input_mappings: { /gender$/ => :check_boxes } do
58
- with_concat_form_for @user do |f|
59
- f.input :gender, collection: %i[male female]
60
- end
61
-
62
- assert_no_select 'select option', 'Male'
63
- assert_select 'input[type=checkbox][value=male]'
64
- end
65
- end
66
-
67
- test 'builder allows to skip input_type class' do
68
- swap SimpleForm, generate_additional_classes_for: %i[label wrapper] do
69
- with_form_for @user, :post_count
70
- assert_no_select "form input#user_post_count.integer"
71
- assert_select "form input#user_post_count"
72
- end
73
- end
74
-
75
- test 'builder allows to add additional classes only for wrapper' do
76
- swap SimpleForm, generate_additional_classes_for: [:wrapper] do
77
- with_form_for @user, :post_count
78
- assert_no_select "form input#user_post_count.string"
79
- assert_no_select "form label#user_post_count.string"
80
- assert_select "form div.input.string"
81
- end
82
- end
83
-
84
- test 'builder allows adding custom input mappings for integer input types' do
85
- swap SimpleForm, input_mappings: { /lock_version/ => :hidden } do
86
- with_form_for @user, :lock_version
87
- assert_no_select 'form input#user_lock_version.integer'
88
- assert_select 'form input#user_lock_version.hidden'
89
- end
90
- end
91
-
92
- test 'builder uses the first matching custom input map when more than one matches' do
93
- swap SimpleForm, input_mappings: { /count$/ => :integer, /^post_/ => :password } do
94
- with_form_for @user, :post_count
95
- assert_no_select 'form input#user_post_count.password'
96
- assert_select 'form input#user_post_count.numeric.integer'
97
- end
98
- end
99
-
100
- test 'builder uses the custom map only for matched attributes' do
101
- swap SimpleForm, input_mappings: { /lock_version/ => :hidden } do
102
- with_form_for @user, :post_count
103
- assert_no_select 'form input#user_post_count.hidden'
104
- assert_select 'form input#user_post_count.string'
105
- end
106
- end
107
-
108
- test 'builder allow to use numbers in the model name' do
109
- user = UserNumber1And2.build(tags: [Tag.new(nil, 'Tag1')])
110
-
111
- with_concat_form_for(user, url: '/') do |f|
112
- f.simple_fields_for(:tags) do |tags|
113
- tags.input :name
114
- end
115
- end
116
-
117
- assert_select 'form .user_number1_and2_tags_name'
118
- assert_no_select 'form .user_number1_and2_tags_1_name'
119
- end
120
-
121
- # INPUT TYPES
122
- test 'builder generates text fields for string columns' do
123
- with_form_for @user, :name
124
- assert_select 'form input#user_name.string'
125
- end
126
-
127
- test 'builder generates text areas for text columns' do
128
- with_form_for @user, :description
129
- assert_no_select 'form input#user_description.string'
130
- assert_select 'form textarea#user_description.text'
131
- end
132
-
133
- test 'builder generates text areas for text columns when hinted' do
134
- with_form_for @user, :description, as: :text
135
- assert_no_select 'form input#user_description.string'
136
- assert_select 'form textarea#user_description.text'
137
- end
138
-
139
- test 'builder generates text field for text columns when hinted' do
140
- with_form_for @user, :description, as: :string
141
- assert_no_select 'form textarea#user_description.text'
142
- assert_select 'form input#user_description.string'
143
- end
144
-
145
- test 'builder generates text areas for hstore columns' do
146
- with_form_for @user, :hstore
147
- assert_no_select 'form input#user_hstore.string'
148
- assert_select 'form textarea#user_hstore.text'
149
- end
150
-
151
- test 'builder generates text areas for json columns' do
152
- with_form_for @user, :json
153
- assert_no_select 'form input#user_json.string'
154
- assert_select 'form textarea#user_json.text'
155
- end
156
-
157
- test 'builder generates text areas for jsonb columns' do
158
- with_form_for @user, :jsonb
159
- assert_no_select 'form input#user_jsonb.string'
160
- assert_select 'form textarea#user_jsonb.text'
161
- end
162
-
163
- test 'builder generates a checkbox for boolean columns' do
164
- with_form_for @user, :active
165
- assert_select 'form input[type=checkbox]#user_active.boolean'
166
- end
167
-
168
- test 'builder uses integer text field for integer columns' do
169
- with_form_for @user, :age
170
- assert_select 'form input#user_age.numeric.integer'
171
- end
172
-
173
- test 'builder generates decimal text field for decimal columns' do
174
- with_form_for @user, :credit_limit
175
- assert_select 'form input#user_credit_limit.numeric.decimal'
176
- end
177
-
178
- test 'builder generates uuid fields for uuid columns' do
179
- with_form_for @user, :uuid
180
- if defined? ActiveModel::Type
181
- assert_select 'form input#user_uuid.string.string'
182
- else
183
- assert_select 'form input#user_uuid.string.uuid'
184
- end
185
- end
186
-
187
- test 'builder generates string fields for citext columns' do
188
- with_form_for @user, :citext
189
- assert_select 'form input#user_citext.string'
190
- end
191
-
192
- test 'builder generates password fields for columns that matches password' do
193
- with_form_for @user, :password
194
- assert_select 'form input#user_password.password'
195
- end
196
-
197
- test 'builder generates country fields for columns that matches country' do
198
- with_form_for @user, :residence_country
199
- assert_select 'form select#user_residence_country.country'
200
- end
201
-
202
- test 'builder generates time_zone fields for columns that matches time_zone' do
203
- with_form_for @user, :time_zone
204
- assert_select 'form select#user_time_zone.time_zone'
205
- end
206
-
207
- test 'builder generates email fields for columns that matches email' do
208
- with_form_for @user, :email
209
- assert_select 'form input#user_email.string.email'
210
- end
211
-
212
- test 'builder generates tel fields for columns that matches phone' do
213
- with_form_for @user, :phone_number
214
- assert_select 'form input#user_phone_number.string.tel'
215
- end
216
-
217
- test 'builder generates url fields for columns that matches url' do
218
- with_form_for @user, :url
219
- assert_select 'form input#user_url.string.url'
220
- end
221
-
222
- test 'builder generates date select for date columns' do
223
- with_form_for @user, :born_at
224
- assert_select 'form select#user_born_at_1i.date'
225
- end
226
-
227
- test 'builder generates time select for time columns' do
228
- with_form_for @user, :delivery_time
229
- assert_select 'form select#user_delivery_time_4i.time'
230
- end
231
-
232
- test 'builder generates datetime select for datetime columns' do
233
- with_form_for @user, :created_at
234
- assert_select 'form select#user_created_at_1i.datetime'
235
- end
236
-
237
- test 'builder generates datetime select for timestamp columns' do
238
- with_form_for @user, :updated_at
239
- assert_select 'form select#user_updated_at_1i.datetime'
240
- end
241
-
242
- test 'builder generates file input for ActiveStorage >= 5.2 and Refile >= 0.2.0 <= 0.4.0' do
243
- with_form_for UserWithAttachment.build, :avatar
244
- assert_select 'form input#user_with_attachment_avatar.file'
245
- end
246
-
247
- test 'builder generates file input for Shrine >= 0.9.0, Refile >= 0.6.0 and CarrierWave >= 0.2.1' do
248
- with_form_for UserWithAttachment.build, :cover
249
- assert_select 'form input#user_with_attachment_cover.file'
250
- end
251
-
252
- test 'builder generates file input for Refile >= 0.4.0 and Shrine >= 0.9.0' do
253
- with_form_for UserWithAttachment.build, :profile_image
254
- assert_select 'form input#user_with_attachment_profile_image.file'
255
- end
256
-
257
- test 'builder generates file input for Paperclip ~> 2.0' do
258
- with_form_for UserWithAttachment.build, :portrait
259
- assert_select 'form input#user_with_attachment_portrait.file'
260
- end
261
-
262
- test 'build generates select if a collection is given' do
263
- with_form_for @user, :age, collection: 1..60
264
- assert_select 'form select#user_age.select'
265
- end
266
-
267
- test 'builder does not generate url fields for columns that contain only the letters url' do
268
- with_form_for @user, :hourly
269
- assert_no_select 'form input#user_url.string.url'
270
- assert_select 'form input#user_hourly.string'
271
- end
272
-
273
- test 'builder allows overriding default input type for text' do
274
- with_form_for @user, :name, as: :text
275
- assert_no_select 'form input#user_name'
276
- assert_select 'form textarea#user_name.text'
277
- end
278
-
279
- test 'builder allows overriding default input type for radio_buttons' do
280
- with_form_for @user, :active, as: :radio_buttons
281
- assert_no_select 'form input[type=checkbox]'
282
- assert_select 'form input.radio_buttons[type=radio]', count: 2
283
- end
284
-
285
- test 'builder allows overriding default input type for string' do
286
- with_form_for @user, :born_at, as: :string
287
- assert_no_select 'form select'
288
- assert_select 'form input#user_born_at.string'
289
- end
290
-
291
- # COMMON OPTIONS
292
- # Remove this test when SimpleForm.form_class is removed in 4.x
293
- test 'builder adds chosen form class' do
294
- ActiveSupport::Deprecation.silence do
295
- swap SimpleForm, form_class: :my_custom_class do
296
- with_form_for @user, :name
297
- assert_select 'form.my_custom_class'
298
- end
299
- end
300
- end
301
-
302
- # Remove this test when SimpleForm.form_class is removed in 4.x
303
- test 'builder adds chosen form class and default form class' do
304
- ActiveSupport::Deprecation.silence do
305
- swap SimpleForm, form_class: "my_custom_class", default_form_class: "my_default_class" do
306
- with_form_for @user, :name
307
- assert_select 'form.my_custom_class.my_default_class'
308
- end
309
- end
310
- end
311
-
312
- test 'builder adds default form class' do
313
- swap SimpleForm, default_form_class: "default_class" do
314
- with_form_for @user, :name
315
- assert_select 'form.default_class'
316
- end
317
- end
318
-
319
- test 'builder allows passing options to input' do
320
- with_form_for @user, :name, input_html: { class: 'my_input', id: 'my_input' }
321
- assert_select 'form input#my_input.my_input.string'
322
- end
323
-
324
- test 'builder does not propagate input options to wrapper' do
325
- with_form_for @user, :name, input_html: { class: 'my_input', id: 'my_input' }
326
- assert_no_select 'form div.input.my_input.string'
327
- assert_select 'form input#my_input.my_input.string'
328
- end
329
-
330
- test 'builder does not propagate input options to wrapper with custom wrapper' do
331
- swap_wrapper :default, custom_wrapper_with_wrapped_input do
332
- with_form_for @user, :name, input_html: { class: 'my_input' }
333
- assert_no_select 'form div.input.my_input'
334
- assert_select 'form input.my_input.string'
335
- end
336
- end
337
-
338
- test 'builder does not propagate label options to wrapper with custom wrapper' do
339
- swap_wrapper :default, custom_wrapper_with_wrapped_label do
340
- with_form_for @user, :name, label_html: { class: 'my_label' }
341
- assert_no_select 'form div.label.my_label'
342
- assert_select 'form label.my_label.string'
343
- end
344
- end
345
-
346
- test 'builder generates an input with label' do
347
- with_form_for @user, :name
348
- assert_select 'form label.string[for=user_name]', /Name/
349
- end
350
-
351
- test 'builder is able to disable the label for an input' do
352
- with_form_for @user, :name, label: false
353
- assert_no_select 'form label'
354
- end
355
-
356
- test 'builder is able to disable the label for an input and return a html safe string' do
357
- with_form_for @user, :name, label: false, wrapper: custom_wrapper_with_wrapped_label_input
358
- assert_select 'form input#user_name'
359
- end
360
-
361
- test 'builder uses custom label' do
362
- with_form_for @user, :name, label: 'Yay!'
363
- assert_select 'form label', /Yay!/
364
- end
365
-
366
- test 'builder passes options to label' do
367
- with_form_for @user, :name, label_html: { id: "cool" }
368
- assert_select 'form label#cool', /Name/
369
- end
370
-
371
- test 'builder does not generate hints for an input' do
372
- with_form_for @user, :name
373
- assert_no_select 'span.hint'
374
- end
375
-
376
- test 'builder is able to add a hint for an input' do
377
- with_form_for @user, :name, hint: 'test'
378
- assert_select 'span.hint', 'test'
379
- end
380
-
381
- test 'builder is able to disable a hint even if it exists in i18n' do
382
- store_translations(:en, simple_form: { hints: { name: 'Hint test' } }) do
383
- stub_any_instance(SimpleForm::Inputs::Base, :hint, -> { raise 'Never' }) do
384
- with_form_for @user, :name, hint: false
385
- assert_no_select 'span.hint'
386
- end
387
- end
388
- end
389
-
390
- test 'builder passes options to hint' do
391
- with_form_for @user, :name, hint: 'test', hint_html: { id: "cool" }
392
- assert_select 'span.hint#cool', 'test'
393
- end
394
-
395
- test 'builder generates errors for attribute without errors' do
396
- with_form_for @user, :credit_limit
397
- assert_no_select 'span.errors'
398
- end
399
-
400
- test 'builder generates errors for attribute with errors' do
401
- with_form_for @user, :name
402
- assert_select 'span.error', "cannot be blank"
403
- end
404
-
405
- test 'builder is able to disable showing errors for an input' do
406
- with_form_for @user, :name, error: false
407
- assert_no_select 'span.error'
408
- end
409
-
410
- test 'builder passes options to errors' do
411
- with_form_for @user, :name, error_html: { id: "cool" }
412
- assert_select 'span.error#cool', "cannot be blank"
413
- end
414
-
415
- test 'placeholder does not be generated when set to false' do
416
- store_translations(:en, simple_form: { placeholders: { user: {
417
- name: 'Name goes here'
418
- } } }) do
419
- with_form_for @user, :name, placeholder: false
420
- assert_no_select 'input[placeholder]'
421
- end
422
- end
423
-
424
- # DEFAULT OPTIONS
425
- %i[input input_field].each do |method|
426
- test "builder receives a default argument and pass it to the inputs when calling '#{method}'" do
427
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
428
- f.public_send(method, :name)
429
- end
430
- assert_select 'input.default_class'
431
- end
432
-
433
- test "builder receives a default argument and pass it to the inputs without changing the defaults when calling '#{method}'" do
434
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
435
- concat(f.public_send(method, :name))
436
- concat(f.public_send(method, :credit_limit))
437
- end
438
-
439
- assert_select "input.string.default_class[name='user[name]']"
440
- assert_no_select "input.string[name='user[credit_limit]']"
441
- end
442
-
443
- test "builder receives a default argument and pass it to the inputs and nested form when calling '#{method}'" do
444
- @user.company = Company.new(1, 'Empresa')
445
-
446
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
447
- concat(f.public_send(method, :name))
448
- concat(f.simple_fields_for(:company) do |company_form|
449
- concat(company_form.public_send(method, :name))
450
- end)
451
- end
452
-
453
- assert_select "input.string.default_class[name='user[name]']"
454
- assert_select "input.string.default_class[name='user[company_attributes][name]']"
455
- end
456
- end
457
-
458
- test "builder receives a default argument and pass it to the inputs when calling 'input', respecting the specific options" do
459
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
460
- f.input :name, input_html: { id: 'specific_id' }
461
- end
462
- assert_select 'input.default_class#specific_id'
463
- end
464
-
465
- test "builder receives a default argument and pass it to the inputs when calling 'input_field', respecting the specific options" do
466
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
467
- f.input_field :name, id: 'specific_id'
468
- end
469
- assert_select 'input.default_class#specific_id'
470
- end
471
-
472
- test "builder receives a default argument and pass it to the inputs when calling 'input', overwriting the defaults with specific options" do
473
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
474
- f.input :name, input_html: { id: 'specific_id' }
475
- end
476
- assert_select 'input.default_class#specific_id'
477
- end
478
-
479
- test "builder receives a default argument and pass it to the inputs when calling 'input_field', overwriting the defaults with specific options" do
480
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
481
- f.input_field :name, id: 'specific_id'
482
- end
483
- assert_select 'input.default_class#specific_id'
484
- end
485
-
486
- # WITHOUT OBJECT
487
- test 'builder generates properly when object is not present' do
488
- with_form_for :project, :name
489
- assert_select 'form input.string#project_name'
490
- end
491
-
492
- test 'builder generates password fields based on attribute name when object is not present' do
493
- with_form_for :project, :password_confirmation
494
- assert_select 'form input[type=password].password#project_password_confirmation'
495
- end
496
-
497
- test 'builder generates text fields by default for all attributes when object is not present' do
498
- with_form_for :project, :created_at
499
- assert_select 'form input.string#project_created_at'
500
- with_form_for :project, :budget
501
- assert_select 'form input.string#project_budget'
502
- end
503
-
504
- test 'builder allows overriding input type when object is not present' do
505
- with_form_for :project, :created_at, as: :datetime
506
- assert_select 'form select.datetime#project_created_at_1i'
507
- with_form_for :project, :budget, as: :decimal
508
- assert_select 'form input.decimal#project_budget'
509
- end
510
-
511
- # CUSTOM FORM BUILDER
512
- test 'custom builder inherits mappings' do
513
- with_custom_form_for @user, :email
514
- assert_select 'form input[type=email]#user_email.custom'
515
- end
516
-
517
- test 'form with CustomMapTypeFormBuilder uses custom map type builder' do
518
- with_concat_custom_mapping_form_for(:user) do |user|
519
- assert user.instance_of?(CustomMapTypeFormBuilder)
520
- end
521
- end
522
-
523
- test 'form with CustomMapTypeFormBuilder uses custom mapping' do
524
- with_concat_custom_mapping_form_for(:user) do |user|
525
- assert_equal SimpleForm::Inputs::StringInput, user.class.mappings[:custom_type]
526
- end
527
- end
528
-
529
- test 'form without CustomMapTypeFormBuilder does not use custom mapping' do
530
- with_concat_form_for(:user) do |user|
531
- assert_nil user.class.mappings[:custom_type]
532
- end
533
- end
534
- end
@@ -1,150 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'test_helper'
3
-
4
- # Tests for f.hint
5
- class HintTest < ActionView::TestCase
6
- def with_hint_for(object, *args)
7
- with_concat_form_for(object) do |f|
8
- f.hint(*args)
9
- end
10
- end
11
-
12
- test 'hint does not be generated by default' do
13
- with_hint_for @user, :name
14
- assert_no_select 'span.hint'
15
- end
16
-
17
- test 'hint is generated with optional text' do
18
- with_hint_for @user, :name, hint: 'Use with care...'
19
- assert_select 'span.hint', 'Use with care...'
20
- end
21
-
22
- test 'hint is generated with decorated object responsive to #to_model' do
23
- with_hint_for @decorated_user, :name, hint: 'Use with care...'
24
- assert_select 'span.hint', 'Use with care...'
25
- end
26
-
27
- test 'hint does not modify the options hash' do
28
- options = { hint: 'Use with care...' }
29
- with_hint_for @user, :name, options
30
- assert_select 'span.hint', 'Use with care...'
31
- assert_equal({ hint: 'Use with care...' }, options)
32
- end
33
-
34
- test 'hint is generated cleanly with optional text' do
35
- with_hint_for @user, :name, hint: 'Use with care...', hint_tag: :span
36
- assert_no_select 'span.hint[hint]'
37
- assert_no_select 'span.hint[hint_tag]'
38
- assert_no_select 'span.hint[hint_html]'
39
- end
40
-
41
- test 'hint uses the current component tag set' do
42
- with_hint_for @user, :name, hint: 'Use with care...', hint_tag: :p
43
- assert_select 'p.hint', 'Use with care...'
44
- end
45
-
46
- test 'hint is able to pass html options' do
47
- with_hint_for @user, :name, hint: 'Yay!', id: 'hint', class: 'yay'
48
- assert_select 'span#hint.hint.yay'
49
- end
50
-
51
- test 'hint is output as html_safe' do
52
- with_hint_for @user, :name, hint: '<b>Bold</b> and not...'.html_safe
53
- assert_select 'span.hint', 'Bold and not...'
54
- assert_select 'span.hint b', 'Bold'
55
- end
56
-
57
- test 'builder escapes hint text' do
58
- with_hint_for @user, :name, hint: '<script>alert(1337)</script>'
59
- assert_no_select 'span.hint script'
60
- end
61
-
62
- # Without attribute name
63
-
64
- test 'hint without attribute name' do
65
- with_hint_for @validating_user, 'Hello World!'
66
- assert_select 'span.hint', 'Hello World!'
67
- end
68
-
69
- test 'hint without attribute name generates component tag with a clean HTML' do
70
- with_hint_for @validating_user, 'Hello World!'
71
- assert_no_select 'span.hint[hint]'
72
- assert_no_select 'span.hint[hint_html]'
73
- end
74
-
75
- test 'hint without attribute name uses the current component tag set' do
76
- with_hint_for @user, 'Hello World!', hint_tag: :p
77
- assert_no_select 'p.hint[hint]'
78
- assert_no_select 'p.hint[hint_html]'
79
- assert_no_select 'p.hint[hint_tag]'
80
- end
81
-
82
- test 'hint without attribute name is able to pass html options' do
83
- with_hint_for @user, 'Yay', id: 'hint', class: 'yay'
84
- assert_select 'span#hint.hint.yay', 'Yay'
85
- end
86
-
87
- # I18n
88
-
89
- test 'hint uses i18n based on model, action, and attribute to lookup translation' do
90
- store_translations(:en, simple_form: { hints: { user: {
91
- edit: { name: 'Content of this input will be truncated...' }
92
- } } }) do
93
- with_hint_for @user, :name
94
- assert_select 'span.hint', 'Content of this input will be truncated...'
95
- end
96
- end
97
-
98
- test 'hint uses i18n with model and attribute to lookup translation' do
99
- store_translations(:en, simple_form: { hints: { user: {
100
- name: 'Content of this input will be capitalized...'
101
- } } }) do
102
- with_hint_for @user, :name
103
- assert_select 'span.hint', 'Content of this input will be capitalized...'
104
- end
105
- end
106
-
107
- test 'hint uses i18n under defaults namespace to lookup translation' do
108
- store_translations(:en, simple_form: {
109
- hints: { defaults: { name: 'Content of this input will be downcased...' } }
110
- }) do
111
- with_hint_for @user, :name
112
- assert_select 'span.hint', 'Content of this input will be downcased...'
113
- end
114
- end
115
-
116
- test 'hint uses i18n with lookup for association name' do
117
- store_translations(:en, simple_form: { hints: {
118
- user: { company: 'My company!' }
119
- } } ) do
120
- with_hint_for @user, :company_id, as: :string, reflection: Association.new(Company, :company, {})
121
- assert_select 'span.hint', /My company!/
122
- end
123
- end
124
-
125
- test 'hint outputs translations as html_safe' do
126
- store_translations(:en, simple_form: { hints: { user: {
127
- edit: { name: '<b>This is bold</b> and this is not...' }
128
- } } }) do
129
- with_hint_for @user, :name
130
- assert_select 'span.hint', 'This is bold and this is not...'
131
- end
132
- end
133
-
134
-
135
- # No object
136
-
137
- test 'hint generates properly when object is not present' do
138
- with_hint_for :project, :name, hint: 'Test without object'
139
- assert_select 'span.hint', 'Test without object'
140
- end
141
-
142
- # Custom wrappers
143
-
144
- test 'hint with custom wrappers works' do
145
- swap_wrapper do
146
- with_hint_for @user, :name, hint: "cannot be blank"
147
- assert_select 'div.omg_hint', "cannot be blank"
148
- end
149
- end
150
- end