-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
form_builder_spec.rb
964 lines (839 loc) · 28.5 KB
/
form_builder_spec.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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
# frozen_string_literal: true
require "rails_helper"
require "rspec/mocks/standalone"
RSpec.describe ActiveAdmin::FormBuilder do
# Setup an ActionView::Base object which can be used for
# generating the form for.
let(:helpers) do
view = mock_action_view
def view.posts_path
"/posts"
end
def view.protect_against_forgery?
false
end
def view.url_for(*args)
if args.first == { action: "index" }
posts_path
else
super
end
end
def view.action_name
"edit"
end
view
end
def form_html(options = {}, form_object = Post.new, &block)
options = { url: helpers.posts_path }.merge(options)
form = render_arbre_component({ form_object: form_object, form_options: options, form_block: block }, helpers) do
active_admin_form_for(assigns[:form_object], assigns[:form_options], &assigns[:form_block])
end.to_s
end
def build_form(options = {}, form_object = Post.new, &block)
form = form_html(options, form_object, &block)
Capybara.string(form)
end
context "in general" do
context "it without custom settings" do
let :body do
build_form do |f|
f.inputs do
f.input :title
f.input :body
end
end
end
it "should generate a fieldset with a inputs class" do
expect(body).to have_css("fieldset.inputs")
end
end
context "it with custom settings" do
let :body do
build_form do |f|
f.inputs class: "custom_class", name: "custom_name", custom_attr: "custom_attr" do
f.input :title
f.input :body
end
end
end
it "should generate a fieldset with a inputs and custom class" do
expect(body).to have_css("fieldset.custom_class")
end
it "should generate a fieldset with a custom legend" do
expect(body).to have_css("legend", text: "custom_name")
end
it "should generate a fieldset with a custom attributes" do
expect(body).to have_css("fieldset[custom_attr='custom_attr']")
end
end
end
context "in general with actions" do
let :body do
build_form do |f|
f.inputs do
f.input :title
f.input :body
end
f.actions do
f.action :submit, label: "Submit Me"
f.action :submit, label: "Another Button"
end
end
end
it "should generate a text input" do
expect(body).to have_field("post[title]", type: "text")
end
it "should generate a textarea" do
expect(body).to have_css("textarea[name='post[body]']")
end
it "should only generate the form once" do
expect(body).to have_css("form", count: 1)
end
it "should generate actions" do
expect(body).to have_button("Submit Me")
expect(body).to have_button("Another Button")
end
end
context "when polymorphic relationship" do
it "should raise error" do
expect do
comment = ActiveAdmin::Comment.new
build_form({ url: "admins/comments" }, comment) do |f|
f.inputs :resource
end
end.to raise_error(Formtastic::PolymorphicInputWithoutCollectionError)
end
end
describe "passing in options with actions" do
let :body do
build_form html: { multipart: true } do |f|
f.inputs :title
f.actions
end
end
it "should pass the options on to the form" do
expect(body).to have_css("form[enctype='multipart/form-data']")
end
end
context "file input present" do
let :body do
build_form do |f|
f.input :body, as: :file
end
end
it "adds multipart attribute automatically" do
expect(body).to have_css("form[enctype='multipart/form-data']")
end
end
context "with actions" do
it "should generate the form once" do
body = build_form do |f|
f.inputs do
f.input :title
end
f.actions
end
expect(body).to have_css("[id=post_title]", count: 1)
end
context "create another checkbox" do
subject do
build_form do |f|
f.actions
end
end
%w(new create).each do |action_name|
it "generates create another checkbox on #{action_name} page" do
expect(helpers).to receive(:action_name) { action_name }
allow(helpers).to receive(:active_admin_config) { instance_double(ActiveAdmin::Resource, create_another: true) }
is_expected.to have_css("[type=checkbox]", count: 1)
.and have_css("[name=create_another]", count: 1)
end
end
%w(show edit update).each do |action_name|
it "doesn't generate create another checkbox on #{action_name} page" do
is_expected.to have_no_css("[name=create_another]", count: 1)
end
end
end
it "should generate one button create another checkbox and a cancel link" do
body = build_form do |f|
f.actions
end
expect(body).to have_css("[type=submit]", count: 1)
expect(body).to have_css(".cancel", count: 1)
end
it "should generate multiple actions" do
body = build_form do |f|
f.actions do
f.action :submit, label: "Create & Continue"
f.action :submit, label: "Create & Edit"
end
end
expect(body).to have_css("[type=submit]", count: 2)
expect(body).to have_css(".cancel", count: 0)
end
end
context "with Arbre inside" do
it "should render the Arbre in the expected place" do
body = build_form do |f|
div do
h1 "Heading"
end
f.inputs do
span "Top note"
f.input :title
span "Bottom note"
end
h3 "Footer"
f.actions
end
expect(body).to have_css("div > h1")
expect(body).to have_css("h1", count: 1)
expect(body).to have_css(".inputs > ol > span")
expect(body).to have_css("span", count: 2)
end
it "should allow a simplified syntax" do
body = build_form do |f|
div do
h1 "Heading"
end
inputs do
span "Top note"
input :title
span "Bottom note"
end
h3 "Footer"
actions
end
expect(body).to have_css("div > h1")
expect(body).to have_css("h1", count: 1)
expect(body).to have_css(".inputs > ol > span")
expect(body).to have_css("span", count: 2)
end
end
context "without passing a block to inputs" do
let :body do
build_form do |f|
f.inputs :title, :body
end
end
it "should have a title input" do
expect(body).to have_field("post[title]", type: "text")
end
it "should have a body textarea" do
expect(body).to have_css("textarea[name='post[body]']")
end
end
context "with semantic fields for" do
let :body do
build_form do |f|
f.inputs do
f.input :title
f.input :body
end
f.form_builder.instance_eval do
@object.author = User.new
end
f.semantic_fields_for :author do |author|
author.inputs :first_name, :last_name
end
end
end
it "should generate a nested text input once" do
expect(body).to have_css("[id=post_author_attributes_first_name_input]", count: 1)
end
end
context "with collection inputs" do
before do
User.create first_name: "John", last_name: "Doe"
User.create first_name: "Jane", last_name: "Doe"
end
describe "as select" do
let :body do
build_form do |f|
f.input :author, include_blank: false
end
end
it "should create 2 options" do
expect(body).to have_css("option", count: 2)
end
end
describe "as radio buttons" do
let :body do
build_form do |f|
f.input :author, as: :radio
end
end
it "should create 2 radio buttons" do
expect(body).to have_css("[type=radio]", count: 2)
end
end
end
context "with inputs component inside has_many" do
def user
u = User.new
u.profile = Profile.new(bio: "bio")
u
end
let :body do
author = user()
build_form do |f|
f.form_builder.instance_eval do
@object.author = author
end
f.inputs name: "Author", for: :author do |author|
author.has_many :profile, allow_destroy: true do |profile|
profile.inputs "inputs for profile #{profile.object.bio}" do
profile.input :bio
end
end
end
end
end
it "should see the profile fields for an existing profile" do
expect(body).to have_css("[id='post_author_attributes_profile_attributes_bio']", count: 1)
expect(body).to have_css("textarea[name='post[author_attributes][profile_attributes][bio]']")
end
end
context "with a has_one relation on an author's profile" do
let :body do
author = user()
build_form do |f|
f.inputs do
f.input :title
f.input :body
end
f.form_builder.instance_eval do
@object.author = author
end
f.inputs name: "Author", for: :author do |author|
author.has_many :profile, allow_destroy: true do |profile|
profile.input :bio
end
end
end
end
it "should see the button to add profile" do
def user
User.new
end
expect(body).to have_css("a[contains(data-html,'post[author_attributes][profile_attributes][bio]')]")
end
it "should see the profile fields for an existing profile" do
def user
u = User.new
u.profile = Profile.new
u
end
expect(body).to have_css("[id='post_author_attributes_profile_attributes_bio']", count: 1)
expect(body).to have_css("textarea[name='post[author_attributes][profile_attributes][bio]']")
end
end
shared_examples :inputs_with_for_expectation do
it "should generate a nested text input once" do
expect(body).to have_css("[id=post_author_attributes_first_name_input]", count: 1)
expect(body).to have_css("[id=post_author_attributes_last_name_input]", count: 1)
end
it "should add author first and last name fields" do
expect(body).to have_field("post[author_attributes][first_name]")
expect(body).to have_field("post[author_attributes][last_name]")
end
end
context "with inputs 'for'" do
let :body do
build_form do |f|
f.inputs do
f.input :title
f.input :body
end
f.form_builder.instance_eval do
@object.author = User.new
end
f.inputs name: "Author", for: :author do |author|
author.inputs :first_name, :last_name
end
end
end
include_examples :inputs_with_for_expectation
end
context "with two input fields 'for' at the end of block" do
let :body do
build_form do |f|
f.inputs do
f.input :title
f.input :body
end
f.form_builder.instance_eval do
@object.author = User.new
end
f.inputs name: "Author", for: :author do |author|
author.input :first_name
author.input :last_name
end
end
end
include_examples :inputs_with_for_expectation
end
context "with two input fields 'for' at the beginning of block" do
let :body do
build_form do |f|
f.form_builder.instance_eval do
@object.author = User.new
end
f.inputs name: "Author", for: :author do |author|
author.input :first_name
author.input :last_name
end
f.inputs do
f.input :title
f.input :body
end
end
end
include_examples :inputs_with_for_expectation
end
context "with wrapper html" do
it "should set a class" do
body = build_form do |f|
f.input :title, wrapper_html: { class: "important" }
end
expect(body).to have_css("li[class='important string input optional stringish']")
end
end
context "with inputs twice" do
let :body do
build_form do |f|
f.inputs do
f.input :title
f.input :body
end
f.inputs do
f.input :author
f.input :created_at
end
end
end
it "should render four inputs" do
expect(body).to have_field("post[title]", count: 1)
expect(body).to have_css("textarea[name='post[body]']", count: 1)
expect(body).to have_select("post[author_id]", count: 1)
expect(body).to have_select("post[created_at(1i)]", count: 1)
expect(body).to have_select("post[created_at(2i)]", count: 1)
expect(body).to have_select("post[created_at(3i)]", count: 1)
expect(body).to have_select("post[created_at(4i)]", count: 1)
end
end
context "with has many inputs" do
describe "with simple block" do
let :body do
build_form({ url: "/categories" }, Category.new) do |f|
f.object.posts.build
f.has_many :posts do |p|
p.input :title
p.input :body
end
f.inputs
end
end
let(:valid_html_id) { /^[A-Za-z]+[\w\-\:\.]*$/ }
it "should translate the association name in header" do
with_translation %i[activerecord models post one], "Blog Post" do
with_translation %i[activerecord models post other], "Blog Posts" do
expect(body).to have_css("h3", text: "Blog Posts")
end
end
end
it "should use model name when there is no translation for given model in header" do
expect(body).to have_css("h3", text: "Post")
end
it "should translate the association name in has many new button" do
with_translation %i[activerecord models post one], "Blog Post" do
with_translation %i[activerecord models post other], "Blog Posts" do
expect(body).to have_css("a", text: "Add New Blog Post")
end
end
end
it "should translate the attribute name" do
with_translation %i[activerecord attributes post title], "A very nice title" do
expect(body).to have_css("label", text: "A very nice title")
end
end
it "should use model name when there is no translation for given model in has many new button" do
expect(body).to have_css("a", text: "Add New Post")
end
it "should render the nested form" do
expect(body).to have_field("category[posts_attributes][0][title]")
expect(body).to have_css("textarea[name='category[posts_attributes][0][body]']")
end
it "should add a link to remove new nested records" do
expect(body).to have_css(".has-many-container > fieldset > ol > li > a.has-many-remove[href='#']", text: "Remove")
end
it "should add a link to add new nested records" do
expect(body).to have_css(".has-many-container > a.has-many-add[href='#']", text: "Add New Post")
end
it "should set an HTML-id valid placeholder" do
link = body.find(".has-many-container > a.has-many-add")
expect(link[:'data-placeholder']).to match valid_html_id
end
describe "with namespaced model" do
it "should set an HTML-id valid placeholder" do
allow(Post).to receive(:name).and_return "ActiveAdmin::Post"
link = body.find(".has-many-container > a.has-many-add")
expect(link[:'data-placeholder']).to match valid_html_id
end
end
end
describe "with complex block" do
let :body do
build_form({ url: "/categories" }, Category.new) do |f|
f.object.posts.build
f.has_many :posts do |p, i|
p.input :title, label: "Title #{i}"
end
end
end
it "should accept a block with a second argument" do
expect(body).to have_css("label", text: "Title 1")
end
it "should add a custom header" do
expect(body).to have_css("h3", text: "Post")
end
end
describe "without heading and new record link" do
let :body do
build_form({ url: "/categories" }, Category.new) do |f|
f.object.posts.build
f.has_many :posts, heading: false, new_record: false do |p|
p.input :title
end
end
end
it "should not add a header" do
expect(body).to have_no_css("h3", text: "Post")
end
it "should not add link to new nested records" do
expect(body).to have_no_css("a", text: "Add New Post")
end
it "should render the nested form" do
expect(body).to have_field("category[posts_attributes][0][title]")
end
end
describe "with custom heading" do
let :body do
build_form({ url: "/categories" }, Category.new) do |f|
f.object.posts.build
f.has_many :posts, heading: "Test heading" do |p|
p.input :title
end
end
end
it "should add a custom header" do
expect(body).to have_css("h3", text: "Test heading")
end
end
describe "with custom new record link" do
let :body do
build_form({ url: "/categories" }, Category.new) do |f|
f.object.posts.build
f.has_many :posts, new_record: "My Custom New Post" do |p|
p.input :title
end
end
end
it "should add a custom new record link" do
expect(body).to have_css("a", text: "My Custom New Post")
end
end
describe "with allow destroy" do
shared_examples_for "has many with allow_destroy = true" do |child_num|
it "should render the nested form" do
expect(body).to have_field("category[posts_attributes][#{child_num}][title]")
end
it "should include a boolean field for _destroy" do
expect(body).to have_field("category[posts_attributes][#{child_num}][_destroy]")
end
it "should have a check box with 'Remove' as its label" do
expect(body).to have_css("label[for=category_posts_attributes_#{child_num}__destroy]", text: "Delete")
end
it "should wrap the destroy field in an li with class 'has-many-delete'" do
expect(body).to have_css(".has-many-container > fieldset > ol > li.has-many-delete > input", count: 1, visible: :hidden)
end
end
shared_examples_for "has many with allow_destroy = false" do |child_num|
it "should render the nested form" do
expect(body).to have_field("category[posts_attributes][#{child_num}][title]")
end
it "should not have a boolean field for _destroy" do
expect(body).to have_no_field("category[posts_attributes][#{child_num}][_destroy]", visible: :all)
end
it "should not have a check box with 'Remove' as its label" do
expect(body).to have_no_css("label[for=category_posts_attributes_#{child_num}__destroy]", text: "Remove")
end
end
shared_examples_for "has many with allow_destroy as String, Symbol or Proc" do |allow_destroy_option|
let :body do
s = self
build_form({ url: "/categories" }, Category.new) do |f|
s.instance_exec do
allow(f.object.posts.build).to receive(:foo?).and_return(true)
allow(f.object.posts.build).to receive(:foo?).and_return(false)
f.object.posts.each do |post|
allow(post).to receive(:new_record?).and_return(false)
end
end
f.has_many :posts, allow_destroy: allow_destroy_option do |p|
p.input :title
end
end
end
context "for the child that responds with true" do
it_behaves_like "has many with allow_destroy = true", 0
end
context "for the child that responds with false" do
it_behaves_like "has many with allow_destroy = false", 1
end
end
context "with an existing post" do
context "with allow_destroy = true" do
let :body do
s = self
build_form({ url: "/categories" }, Category.new) do |f|
s.instance_exec do
allow(f.object.posts.build).to receive(:new_record?).and_return(false)
end
f.has_many :posts, allow_destroy: true do |p|
p.input :title
end
end
end
it_behaves_like "has many with allow_destroy = true", 0
end
context "with allow_destroy = false" do
let :body do
s = self
build_form({ url: "/categories" }, Category.new) do |f|
s.instance_exec do
allow(f.object.posts.build).to receive(:new_record?).and_return(false)
end
f.has_many :posts, allow_destroy: false do |p|
p.input :title
end
end
end
it_behaves_like "has many with allow_destroy = false", 0
end
context "with allow_destroy = nil" do
let :body do
s = self
build_form({ url: "/categories" }, Category.new) do |f|
s.instance_exec do
allow(f.object.posts.build).to receive(:new_record?).and_return(false)
end
f.has_many :posts, allow_destroy: nil do |p|
p.input :title
end
end
end
it_behaves_like "has many with allow_destroy = false", 0
end
context "with allow_destroy as Symbol" do
it_behaves_like("has many with allow_destroy as String, Symbol or Proc", :foo?)
end
context "with allow_destroy as String" do
it_behaves_like("has many with allow_destroy as String, Symbol or Proc", "foo?")
end
context "with allow_destroy as proc" do
it_behaves_like(
"has many with allow_destroy as String, Symbol or Proc",
Proc.new { |child| child.foo? })
end
context "with allow_destroy as lambda" do
it_behaves_like(
"has many with allow_destroy as String, Symbol or Proc",
lambda { |child| child.foo? })
end
context "with allow_destroy as any other expression that evaluates to true" do
let :body do
s = self
build_form({ url: "/categories" }, Category.new) do |f|
s.instance_exec do
allow(f.object.posts.build).to receive(:new_record?).and_return(false)
end
f.has_many :posts, allow_destroy: Object.new do |p|
p.input :title
end
end
end
it_behaves_like "has many with allow_destroy = true", 0
end
end
context "with a new post" do
context "with allow_destroy = true" do
let :body do
build_form({ url: "/categories" }, Category.new) do |f|
f.object.posts.build
f.has_many :posts, allow_destroy: true do |p|
p.input :title
end
end
end
it_behaves_like "has many with allow_destroy = false", 0
end
end
end
describe "sortable" do
# TODO: it doesn't make any sense to use your foreign key as something that's sortable (and therefore editable)
context "with a new post" do
let :body do
build_form({ url: "/categories" }, Category.new) do |f|
f.object.posts.build
f.has_many :posts, sortable: :position do |p|
p.input :title
end
end
end
it "shows the nested fields for unsaved records" do
expect(body).to have_css("fieldset.inputs.has-many-fields")
end
end
context "with post returning nil for the sortable attribute" do
let :body do
build_form({ url: "/categories" }, Category.new) do |f|
f.object.posts.build position: 3
f.object.posts.build
f.has_many :posts, sortable: :position do |p|
p.input :title
end
end
end
it "shows the nested fields for unsaved records" do
expect(body).to have_css("fieldset.inputs.has-many-fields")
end
end
context "with existing and new posts" do
let! :category do
Category.create name: "Name"
end
let! :post do
category.posts.create
end
let :body do
build_form({ url: "/categories" }, category) do |f|
f.object.posts.build
f.has_many :posts, sortable: :position do |p|
p.input :title
end
end
end
it "shows the nested fields for saved and unsaved records" do
expect(body).to have_css("fieldset.inputs.has-many-fields")
end
end
context "without sortable_start set" do
let :body do
build_form({ url: "/categories" }, Category.new) do |f|
f.object.posts.build
f.has_many :posts, sortable: :position do |p|
p.input :title
end
end
end
it "defaults to 0" do
expect(body).to have_css("div.has-many-container[data-sortable-start='0']")
end
end
context "with sortable_start set" do
let :body do
build_form({ url: "/categories" }, Category.new) do |f|
f.object.posts.build
f.has_many :posts, sortable: :position, sortable_start: 15 do |p|
p.input :title
end
end
end
it "sets the data attribute" do
expect(body).to have_css("div.has-many-container[data-sortable-start='15']")
end
end
end
describe "with nesting" do
context "in an inputs block" do
let :body do
build_form({ url: "/categories" }, Category.new) do |f|
f.inputs "Field Wrapper" do
f.object.posts.build
f.has_many :posts do |p|
p.input :title
end
end
end
end
it "should wrap the has_many fieldset in an li" do
expect(body).to have_css("ol > li.has-many-container")
end
it "should have a direct fieldset child" do
expect(body).to have_css("li.has-many-container > fieldset")
end
it "should not contain invalid li children" do
expect(body).to have_no_css("div.has-many-container > li")
end
end
context "in another has_many block" do
let :body_html do
form_html({ url: "/categories" }, Category.new) do |f|
f.object.posts.build
f.has_many :posts do |p|
p.object.taggings.build
p.input :title
p.has_many :taggings do |t|
t.input :tag
t.input :position
end
end
end
end
let(:body) { Capybara.string body_html }
it "displays the input between the outer and inner has_many" do
expect(body).to have_css(".has-many-container ol > li:first-child input#category_posts_attributes_0_title")
expect(body).to have_css(".has-many-container ol > li:nth-child(2).has-many-container > fieldset")
end
it "should not contain invalid li children" do
expect(body).to have_no_css(".has-many-container div.has-many-container > li")
end
end
end
it "should render the block if it returns nil" do
body = build_form({ url: "/categories" }, Category.new) do |f|
f.object.posts.build
f.has_many :posts do |p|
p.input :title
nil
end
end
expect(body).to have_field("category[posts_attributes][0][title]")
end
end
{ # Testing that the same input can be used multiple times
"f.input :title, as: :string" => "post_title",
"f.input :title, as: :text" => "post_title",
"f.input :created_at, as: :time_select" => "post_created_at_2i",
"f.input :created_at, as: :datetime_select" => "post_created_at_2i",
"f.input :created_at, as: :date_select" => "post_created_at_2i",
# Testing that return values don't screw up the form
"f.input :title; nil" => "post_title",
"f.input :title; []" => "post_title",
"[:title].each{ |r| f.input r }" => "post_title",
"[:title].map { |r| f.input r }" => "post_title",
}.each do |source, selector|
it "should properly buffer `#{source}`" do
body = build_form do |f|
f.inputs do
eval source
eval source
end
end
expect(body).to have_css("[id=#{selector}]", count: 2, visible: :all)
end
end
end