1
1
require File . expand_path ( File . dirname ( __FILE__ ) + '/../spec_helper' )
2
2
3
- describe_with_render ActiveAdmin ::FormBuilder do
3
+ describe ActiveAdmin ::FormBuilder do
4
+ include Arbre ::HTML
5
+ let ( :assigns ) { { } }
6
+ let ( :helpers ) do
7
+ v = action_view
8
+ def v . posts_path
9
+ "/posts"
10
+ end
11
+ def v . protect_against_forgery?
12
+ false
13
+ end
14
+ v
15
+ end
4
16
5
17
def build_form ( options = { } , &block )
6
- ActiveAdmin . register Post do
7
- form ( options , &block )
8
- end
9
- get :new
18
+ active_admin_form_for Post . new , &block
10
19
end
11
20
12
21
context "in general" do
13
- before do
22
+ let :body do
14
23
build_form do |f |
15
24
f . inputs do
16
25
f . input :title
@@ -22,92 +31,84 @@ def build_form(options = {}, &block)
22
31
end
23
32
end
24
33
end
34
+
25
35
it "should generate a text input" do
26
- response . body . should have_tag ( "input" , :attributes => { :type => "text" ,
36
+ body . should have_tag ( "input" , :attributes => { :type => "text" ,
27
37
:name => "post[title]" } )
28
38
end
29
39
it "should generate a textarea" do
30
- response . body . should have_tag ( "textarea" , :attributes => { :name => "post[body]" } )
40
+ body . should have_tag ( "textarea" , :attributes => { :name => "post[body]" } )
31
41
end
32
42
it "should only generate the form once" do
33
- response . body . scan ( /Title/ ) . size . should == 1
43
+ body . scan ( /Title/ ) . size . should == 1
34
44
end
35
45
it "should generate buttons" do
36
- response . body . should have_tag ( "input" , :attributes => { :type => "submit" ,
46
+ body . should have_tag ( "input" , :attributes => { :type => "submit" ,
37
47
:value => "Submit Me" } )
38
- response . body . should have_tag ( "input" , :attributes => { :type => "submit" ,
48
+ body . should have_tag ( "input" , :attributes => { :type => "submit" ,
39
49
:value => "Another Button" } )
40
50
end
41
51
end
42
52
43
53
describe "passing in options" do
44
- before do
54
+ let :body do
45
55
build_form :html => { :multipart => true } do |f |
46
56
f . inputs :title
47
57
f . buttons
48
58
end
49
59
end
50
60
it "should pass the options on to the form" do
51
- response . body . should have_tag ( "form" , :attributes => { :enctype => "multipart/form-data" } )
52
- end
53
- end
54
-
55
- context "with default settings" do
56
- before do
57
- @controller . class . reset_form_config!
58
- end
59
- it "should generate one post title field" do
60
- response . body . scan ( 'id="post_title"' ) . size . should == 1
61
+ body . should have_tag ( "form" , :attributes => { :enctype => "multipart/form-data" } )
61
62
end
62
63
end
63
64
64
65
context "with buttons" do
65
66
it "should generate the form once" do
66
- build_form do |f |
67
+ body = build_form do |f |
67
68
f . inputs do
68
69
f . input :title
69
70
end
70
71
f . buttons
71
72
end
72
- response . body . scan ( /id=\" post_title\" / ) . size . should == 1
73
+ body . scan ( /id=\" post_title\" / ) . size . should == 1
73
74
end
74
75
it "should generate one button and a cancel link" do
75
- build_form do |f |
76
+ body = build_form do |f |
76
77
f . buttons
77
78
end
78
- response . body . scan ( /type=\" submit\" / ) . size . should == 1
79
- response . body . scan ( /class=\" cancel\" / ) . size . should == 1
79
+ body . scan ( /type=\" submit\" / ) . size . should == 1
80
+ body . scan ( /class=\" cancel\" / ) . size . should == 1
80
81
end
81
82
it "should generate multiple buttons" do
82
- build_form do |f |
83
+ body = build_form do |f |
83
84
f . buttons do
84
85
f . commit_button "Create & Continue"
85
86
f . commit_button "Create & Edit"
86
87
end
87
88
end
88
- response . body . scan ( /type=\" submit\" / ) . size . should == 2
89
- response . body . scan ( /class=\" cancel\" / ) . size . should == 0
89
+ body . scan ( /type=\" submit\" / ) . size . should == 2
90
+ body . scan ( /class=\" cancel\" / ) . size . should == 0
90
91
end
91
92
92
93
end
93
94
94
95
context "without passing a block to inputs" do
95
- before do
96
+ let :body do
96
97
build_form do |f |
97
98
f . inputs :title , :body
98
99
end
99
100
end
100
101
it "should have a title input" do
101
- response . body . should have_tag ( "input" , :attributes => { :type => "text" ,
102
+ body . should have_tag ( "input" , :attributes => { :type => "text" ,
102
103
:name => "post[title]" } )
103
104
end
104
105
it "should have a body textarea" do
105
- response . body . should have_tag ( "textarea" , :attributes => { :name => "post[body]" } )
106
+ body . should have_tag ( "textarea" , :attributes => { :name => "post[body]" } )
106
107
end
107
108
end
108
109
109
110
context "with semantic fields for" do
110
- before do
111
+ let :body do
111
112
build_form do |f |
112
113
f . inputs do
113
114
f . input :title
@@ -122,7 +123,7 @@ def build_form(options = {}, &block)
122
123
end
123
124
end
124
125
it "should generate a nested text input once" do
125
- response . body . scan ( "post_author_attributes_first_name_input" ) . size . should == 1
126
+ body . scan ( "post_author_attributes_first_name_input" ) . size . should == 1
126
127
end
127
128
end
128
129
@@ -133,30 +134,30 @@ def build_form(options = {}, &block)
133
134
end
134
135
135
136
describe "as select" do
136
- before do
137
+ let :body do
137
138
build_form do |f |
138
139
f . input :author
139
140
end
140
141
end
141
142
it "should create 2 options" do
142
- response . body . scan ( /\< option/ ) . size . should == 3
143
+ body . scan ( /\< option/ ) . size . should == 3
143
144
end
144
145
end
145
146
146
147
describe "as radio buttons" do
147
- before do
148
+ let :body do
148
149
build_form do |f |
149
150
f . input :author , :as => :radio
150
151
end
151
152
end
152
153
it "should create 2 radio buttons" do
153
- response . body . scan ( /type=\" radio\" / ) . size . should == 2
154
+ body . scan ( /type=\" radio\" / ) . size . should == 2
154
155
end
155
156
end
156
157
end
157
158
158
159
context "with inputs 'for'" do
159
- before do
160
+ let :body do
160
161
build_form do |f |
161
162
f . inputs do
162
163
f . input :title
@@ -171,19 +172,19 @@ def build_form(options = {}, &block)
171
172
end
172
173
end
173
174
it "should generate a nested text input once" do
174
- response . body . scan ( "post_author_attributes_first_name_input" ) . size . should == 1
175
+ body . scan ( "post_author_attributes_first_name_input" ) . size . should == 1
175
176
end
176
177
it "should add an author first name field" do
177
- response . body . should have_tag ( "input" , :attributes => { :name => "post[author_attributes][first_name]" } )
178
+ body . should have_tag ( "input" , :attributes => { :name => "post[author_attributes][first_name]" } )
178
179
end
179
180
end
180
181
181
182
context "with wrapper html" do
182
183
it "should set a class" do
183
- build_form do |f |
184
+ body = build_form do |f |
184
185
f . input :title , :wrapper_html => { :class => "important" }
185
186
end
186
- response . body . should have_tag ( "li" , :attributes => { :class => "string optional important" } )
187
+ body . should have_tag ( "li" , :attributes => { :class => "string optional important" } )
187
188
end
188
189
end
189
190
@@ -196,26 +197,26 @@ def build_form(options = {}, &block)
196
197
"input :created_at, :as => :date" => /id\= \" post_created_at_2i\" / ,
197
198
} . each do |source , regex |
198
199
it "should properly buffer #{ source } " do
199
- build_form do |f |
200
+ body = build_form do |f |
200
201
f . inputs do
201
202
f . instance_eval ( source )
202
203
f . instance_eval ( source )
203
204
end
204
205
end
205
- response . body . scan ( regex ) . size . should == 2
206
+ body . scan ( regex ) . size . should == 2
206
207
end
207
208
end
208
209
209
210
describe "datepicker input" do
210
- before do
211
+ let :body do
211
212
build_form do |f |
212
213
f . inputs do
213
214
f . input :created_at , :as => :datepicker
214
215
end
215
216
end
216
217
end
217
218
it "should generate a text input with the class of datepicker" do
218
- response . body . should have_tag ( "input" , :attributes => { :type => "text" ,
219
+ body . should have_tag ( "input" , :attributes => { :type => "text" ,
219
220
:class => "datepicker" ,
220
221
:name => "post[created_at]" } )
221
222
end
0 commit comments