simple_form 5.2.0 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -1
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +3 -4
  5. data/lib/simple_form/components/label_input.rb +1 -1
  6. data/lib/simple_form/form_builder.rb +1 -5
  7. data/lib/simple_form/railtie.rb +4 -0
  8. data/lib/simple_form/version.rb +1 -1
  9. data/lib/simple_form/wrappers/leaf.rb +1 -1
  10. data/lib/simple_form.rb +8 -4
  11. metadata +5 -85
  12. data/test/action_view_extensions/builder_test.rb +0 -634
  13. data/test/action_view_extensions/form_helper_test.rb +0 -172
  14. data/test/components/custom_components_test.rb +0 -62
  15. data/test/components/label_test.rb +0 -352
  16. data/test/form_builder/association_test.rb +0 -252
  17. data/test/form_builder/button_test.rb +0 -48
  18. data/test/form_builder/error_notification_test.rb +0 -80
  19. data/test/form_builder/error_test.rb +0 -281
  20. data/test/form_builder/general_test.rb +0 -539
  21. data/test/form_builder/hint_test.rb +0 -150
  22. data/test/form_builder/input_field_test.rb +0 -195
  23. data/test/form_builder/label_test.rb +0 -136
  24. data/test/form_builder/wrapper_test.rb +0 -378
  25. data/test/generators/simple_form_generator_test.rb +0 -43
  26. data/test/inputs/boolean_input_test.rb +0 -256
  27. data/test/inputs/collection_check_boxes_input_test.rb +0 -323
  28. data/test/inputs/collection_radio_buttons_input_test.rb +0 -446
  29. data/test/inputs/collection_select_input_test.rb +0 -380
  30. data/test/inputs/color_input_test.rb +0 -10
  31. data/test/inputs/country_input_test.rb +0 -36
  32. data/test/inputs/datetime_input_test.rb +0 -176
  33. data/test/inputs/disabled_test.rb +0 -92
  34. data/test/inputs/discovery_test.rb +0 -142
  35. data/test/inputs/file_input_test.rb +0 -17
  36. data/test/inputs/general_test.rb +0 -133
  37. data/test/inputs/grouped_collection_select_input_test.rb +0 -196
  38. data/test/inputs/hidden_input_test.rb +0 -32
  39. data/test/inputs/numeric_input_test.rb +0 -177
  40. data/test/inputs/readonly_test.rb +0 -102
  41. data/test/inputs/required_test.rb +0 -158
  42. data/test/inputs/rich_text_area_input_test.rb +0 -15
  43. data/test/inputs/string_input_test.rb +0 -158
  44. data/test/inputs/text_input_test.rb +0 -37
  45. data/test/inputs/time_zone_input_test.rb +0 -36
  46. data/test/simple_form_test.rb +0 -18
  47. data/test/support/discovery_inputs.rb +0 -65
  48. data/test/support/misc_helpers.rb +0 -274
  49. data/test/support/mock_controller.rb +0 -30
  50. data/test/support/models.rb +0 -357
  51. data/test/test_helper.rb +0 -95
@@ -1,539 +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 ActiveStorage::Attached::Many' do
248
- with_form_for UserWithAttachment.build, :avatars
249
- assert_select 'form input#user_with_attachment_avatars.file'
250
- end
251
-
252
- test 'builder generates file input for Refile >= 0.3.0 and CarrierWave >= 0.2.2' do
253
- with_form_for UserWithAttachment.build, :cover
254
- assert_select 'form input#user_with_attachment_cover.file'
255
- end
256
-
257
- test 'builder generates file input for Refile >= 0.4.0 and Shrine >= 0.9.0' do
258
- with_form_for UserWithAttachment.build, :profile_image
259
- assert_select 'form input#user_with_attachment_profile_image.file'
260
- end
261
-
262
- test 'builder generates file input for Paperclip ~> 2.0' do
263
- with_form_for UserWithAttachment.build, :portrait
264
- assert_select 'form input#user_with_attachment_portrait.file'
265
- end
266
-
267
- test 'build generates select if a collection is given' do
268
- with_form_for @user, :age, collection: 1..60
269
- assert_select 'form select#user_age.select'
270
- end
271
-
272
- test 'builder does not generate url fields for columns that contain only the letters url' do
273
- with_form_for @user, :hourly
274
- assert_no_select 'form input#user_url.string.url'
275
- assert_select 'form input#user_hourly.string'
276
- end
277
-
278
- test 'builder allows overriding default input type for text' do
279
- with_form_for @user, :name, as: :text
280
- assert_no_select 'form input#user_name'
281
- assert_select 'form textarea#user_name.text'
282
- end
283
-
284
- test 'builder allows overriding default input type for radio_buttons' do
285
- with_form_for @user, :active, as: :radio_buttons
286
- assert_no_select 'form input[type=checkbox]'
287
- assert_select 'form input.radio_buttons[type=radio]', count: 2
288
- end
289
-
290
- test 'builder allows overriding default input type for string' do
291
- with_form_for @user, :born_at, as: :string
292
- assert_no_select 'form select'
293
- assert_select 'form input#user_born_at.string'
294
- end
295
-
296
- # COMMON OPTIONS
297
- # Remove this test when SimpleForm.form_class is removed in 4.x
298
- test 'builder adds chosen form class' do
299
- ActiveSupport::Deprecation.silence do
300
- swap SimpleForm, form_class: :my_custom_class do
301
- with_form_for @user, :name
302
- assert_select 'form.my_custom_class'
303
- end
304
- end
305
- end
306
-
307
- # Remove this test when SimpleForm.form_class is removed in 4.x
308
- test 'builder adds chosen form class and default form class' do
309
- ActiveSupport::Deprecation.silence do
310
- swap SimpleForm, form_class: "my_custom_class", default_form_class: "my_default_class" do
311
- with_form_for @user, :name
312
- assert_select 'form.my_custom_class.my_default_class'
313
- end
314
- end
315
- end
316
-
317
- test 'builder adds default form class' do
318
- swap SimpleForm, default_form_class: "default_class" do
319
- with_form_for @user, :name
320
- assert_select 'form.default_class'
321
- end
322
- end
323
-
324
- test 'builder allows passing options to input' do
325
- with_form_for @user, :name, input_html: { class: 'my_input', id: 'my_input' }
326
- assert_select 'form input#my_input.my_input.string'
327
- end
328
-
329
- test 'builder does not propagate input options to wrapper' do
330
- with_form_for @user, :name, input_html: { class: 'my_input', id: 'my_input' }
331
- assert_no_select 'form div.input.my_input.string'
332
- assert_select 'form input#my_input.my_input.string'
333
- end
334
-
335
- test 'builder does not propagate input options to wrapper with custom wrapper' do
336
- swap_wrapper :default, custom_wrapper_with_wrapped_input do
337
- with_form_for @user, :name, input_html: { class: 'my_input' }
338
- assert_no_select 'form div.input.my_input'
339
- assert_select 'form input.my_input.string'
340
- end
341
- end
342
-
343
- test 'builder does not propagate label options to wrapper with custom wrapper' do
344
- swap_wrapper :default, custom_wrapper_with_wrapped_label do
345
- with_form_for @user, :name, label_html: { class: 'my_label' }
346
- assert_no_select 'form div.label.my_label'
347
- assert_select 'form label.my_label.string'
348
- end
349
- end
350
-
351
- test 'builder generates an input with label' do
352
- with_form_for @user, :name
353
- assert_select 'form label.string[for=user_name]', /Name/
354
- end
355
-
356
- test 'builder is able to disable the label for an input' do
357
- with_form_for @user, :name, label: false
358
- assert_no_select 'form label'
359
- end
360
-
361
- test 'builder is able to disable the label for an input and return a html safe string' do
362
- with_form_for @user, :name, label: false, wrapper: custom_wrapper_with_wrapped_label_input
363
- assert_select 'form input#user_name'
364
- end
365
-
366
- test 'builder uses custom label' do
367
- with_form_for @user, :name, label: 'Yay!'
368
- assert_select 'form label', /Yay!/
369
- end
370
-
371
- test 'builder passes options to label' do
372
- with_form_for @user, :name, label_html: { id: "cool" }
373
- assert_select 'form label#cool', /Name/
374
- end
375
-
376
- test 'builder does not generate hints for an input' do
377
- with_form_for @user, :name
378
- assert_no_select 'span.hint'
379
- end
380
-
381
- test 'builder is able to add a hint for an input' do
382
- with_form_for @user, :name, hint: 'test'
383
- assert_select 'span.hint', 'test'
384
- end
385
-
386
- test 'builder is able to disable a hint even if it exists in i18n' do
387
- store_translations(:en, simple_form: { hints: { name: 'Hint test' } }) do
388
- stub_any_instance(SimpleForm::Inputs::Base, :hint, -> { raise 'Never' }) do
389
- with_form_for @user, :name, hint: false
390
- assert_no_select 'span.hint'
391
- end
392
- end
393
- end
394
-
395
- test 'builder passes options to hint' do
396
- with_form_for @user, :name, hint: 'test', hint_html: { id: "cool" }
397
- assert_select 'span.hint#cool', 'test'
398
- end
399
-
400
- test 'builder generates errors for attribute without errors' do
401
- with_form_for @user, :credit_limit
402
- assert_no_select 'span.errors'
403
- end
404
-
405
- test 'builder generates errors for attribute with errors' do
406
- with_form_for @user, :name
407
- assert_select 'span.error', "cannot be blank"
408
- end
409
-
410
- test 'builder is able to disable showing errors for an input' do
411
- with_form_for @user, :name, error: false
412
- assert_no_select 'span.error'
413
- end
414
-
415
- test 'builder passes options to errors' do
416
- with_form_for @user, :name, error_html: { id: "cool" }
417
- assert_select 'span.error#cool', "cannot be blank"
418
- end
419
-
420
- test 'placeholder does not be generated when set to false' do
421
- store_translations(:en, simple_form: { placeholders: { user: {
422
- name: 'Name goes here'
423
- } } }) do
424
- with_form_for @user, :name, placeholder: false
425
- assert_no_select 'input[placeholder]'
426
- end
427
- end
428
-
429
- # DEFAULT OPTIONS
430
- %i[input input_field].each do |method|
431
- test "builder receives a default argument and pass it to the inputs when calling '#{method}'" do
432
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
433
- f.public_send(method, :name)
434
- end
435
- assert_select 'input.default_class'
436
- end
437
-
438
- test "builder receives a default argument and pass it to the inputs without changing the defaults when calling '#{method}'" do
439
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
440
- concat(f.public_send(method, :name))
441
- concat(f.public_send(method, :credit_limit))
442
- end
443
-
444
- assert_select "input.string.default_class[name='user[name]']"
445
- assert_no_select "input.string[name='user[credit_limit]']"
446
- end
447
-
448
- test "builder receives a default argument and pass it to the inputs and nested form when calling '#{method}'" do
449
- @user.company = Company.new(1, 'Empresa')
450
-
451
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
452
- concat(f.public_send(method, :name))
453
- concat(f.simple_fields_for(:company) do |company_form|
454
- concat(company_form.public_send(method, :name))
455
- end)
456
- end
457
-
458
- assert_select "input.string.default_class[name='user[name]']"
459
- assert_select "input.string.default_class[name='user[company_attributes][name]']"
460
- end
461
- end
462
-
463
- test "builder receives a default argument and pass it to the inputs when calling 'input', respecting the specific options" do
464
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
465
- f.input :name, input_html: { id: 'specific_id' }
466
- end
467
- assert_select 'input.default_class#specific_id'
468
- end
469
-
470
- test "builder receives a default argument and pass it to the inputs when calling 'input_field', respecting the specific options" do
471
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
472
- f.input_field :name, id: 'specific_id'
473
- end
474
- assert_select 'input.default_class#specific_id'
475
- end
476
-
477
- test "builder receives a default argument and pass it to the inputs when calling 'input', overwriting the defaults with specific options" do
478
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
479
- f.input :name, input_html: { id: 'specific_id' }
480
- end
481
- assert_select 'input.default_class#specific_id'
482
- end
483
-
484
- test "builder receives a default argument and pass it to the inputs when calling 'input_field', overwriting the defaults with specific options" do
485
- with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
486
- f.input_field :name, id: 'specific_id'
487
- end
488
- assert_select 'input.default_class#specific_id'
489
- end
490
-
491
- # WITHOUT OBJECT
492
- test 'builder generates properly when object is not present' do
493
- with_form_for :project, :name
494
- assert_select 'form input.string#project_name'
495
- end
496
-
497
- test 'builder generates password fields based on attribute name when object is not present' do
498
- with_form_for :project, :password_confirmation
499
- assert_select 'form input[type=password].password#project_password_confirmation'
500
- end
501
-
502
- test 'builder generates text fields by default for all attributes when object is not present' do
503
- with_form_for :project, :created_at
504
- assert_select 'form input.string#project_created_at'
505
- with_form_for :project, :budget
506
- assert_select 'form input.string#project_budget'
507
- end
508
-
509
- test 'builder allows overriding input type when object is not present' do
510
- with_form_for :project, :created_at, as: :datetime
511
- assert_select 'form select.datetime#project_created_at_1i'
512
- with_form_for :project, :budget, as: :decimal
513
- assert_select 'form input.decimal#project_budget'
514
- end
515
-
516
- # CUSTOM FORM BUILDER
517
- test 'custom builder inherits mappings' do
518
- with_custom_form_for @user, :email
519
- assert_select 'form input[type=email]#user_email.custom'
520
- end
521
-
522
- test 'form with CustomMapTypeFormBuilder uses custom map type builder' do
523
- with_concat_custom_mapping_form_for(:user) do |user|
524
- assert user.instance_of?(CustomMapTypeFormBuilder)
525
- end
526
- end
527
-
528
- test 'form with CustomMapTypeFormBuilder uses custom mapping' do
529
- with_concat_custom_mapping_form_for(:user) do |user|
530
- assert_equal SimpleForm::Inputs::StringInput, user.class.mappings[:custom_type]
531
- end
532
- end
533
-
534
- test 'form without CustomMapTypeFormBuilder does not use custom mapping' do
535
- with_concat_form_for(:user) do |user|
536
- assert_nil user.class.mappings[:custom_type]
537
- end
538
- end
539
- 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