Skip to content

Commit b60e19a

Browse files
Greg Bellgregbell
authored andcommitted
Fixed form builder specs
1 parent d02d37e commit b60e19a

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def mock_action_view(assigns = {})
7979
controller = ActionView::TestCase::TestController.new
8080
ActionView::Base.send :include, ActionView::Helpers
8181
ActionView::Base.send :include, ActiveAdmin::ViewHelpers
82+
ActionView::Base.send :include, Rails.application.routes.url_helpers
8283
ActionView::Base.new(ActionController::Base.view_paths, assigns, controller)
8384
end
8485
alias_method :action_view, :mock_action_view

spec/unit/form_builder_spec.rb

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
22

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
416

517
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
1019
end
1120

1221
context "in general" do
13-
before do
22+
let :body do
1423
build_form do |f|
1524
f.inputs do
1625
f.input :title
@@ -22,92 +31,84 @@ def build_form(options = {}, &block)
2231
end
2332
end
2433
end
34+
2535
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",
2737
:name => "post[title]" })
2838
end
2939
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]" })
3141
end
3242
it "should only generate the form once" do
33-
response.body.scan(/Title/).size.should == 1
43+
body.scan(/Title/).size.should == 1
3444
end
3545
it "should generate buttons" do
36-
response.body.should have_tag("input", :attributes => { :type => "submit",
46+
body.should have_tag("input", :attributes => { :type => "submit",
3747
:value => "Submit Me" })
38-
response.body.should have_tag("input", :attributes => { :type => "submit",
48+
body.should have_tag("input", :attributes => { :type => "submit",
3949
:value => "Another Button" })
4050
end
4151
end
4252

4353
describe "passing in options" do
44-
before do
54+
let :body do
4555
build_form :html => { :multipart => true } do |f|
4656
f.inputs :title
4757
f.buttons
4858
end
4959
end
5060
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" })
6162
end
6263
end
6364

6465
context "with buttons" do
6566
it "should generate the form once" do
66-
build_form do |f|
67+
body = build_form do |f|
6768
f.inputs do
6869
f.input :title
6970
end
7071
f.buttons
7172
end
72-
response.body.scan(/id=\"post_title\"/).size.should == 1
73+
body.scan(/id=\"post_title\"/).size.should == 1
7374
end
7475
it "should generate one button and a cancel link" do
75-
build_form do |f|
76+
body = build_form do |f|
7677
f.buttons
7778
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
8081
end
8182
it "should generate multiple buttons" do
82-
build_form do |f|
83+
body = build_form do |f|
8384
f.buttons do
8485
f.commit_button "Create & Continue"
8586
f.commit_button "Create & Edit"
8687
end
8788
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
9091
end
9192

9293
end
9394

9495
context "without passing a block to inputs" do
95-
before do
96+
let :body do
9697
build_form do |f|
9798
f.inputs :title, :body
9899
end
99100
end
100101
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",
102103
:name => "post[title]" })
103104
end
104105
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]" })
106107
end
107108
end
108109

109110
context "with semantic fields for" do
110-
before do
111+
let :body do
111112
build_form do |f|
112113
f.inputs do
113114
f.input :title
@@ -122,7 +123,7 @@ def build_form(options = {}, &block)
122123
end
123124
end
124125
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
126127
end
127128
end
128129

@@ -133,30 +134,30 @@ def build_form(options = {}, &block)
133134
end
134135

135136
describe "as select" do
136-
before do
137+
let :body do
137138
build_form do |f|
138139
f.input :author
139140
end
140141
end
141142
it "should create 2 options" do
142-
response.body.scan(/\<option/).size.should == 3
143+
body.scan(/\<option/).size.should == 3
143144
end
144145
end
145146

146147
describe "as radio buttons" do
147-
before do
148+
let :body do
148149
build_form do |f|
149150
f.input :author, :as => :radio
150151
end
151152
end
152153
it "should create 2 radio buttons" do
153-
response.body.scan(/type=\"radio\"/).size.should == 2
154+
body.scan(/type=\"radio\"/).size.should == 2
154155
end
155156
end
156157
end
157158

158159
context "with inputs 'for'" do
159-
before do
160+
let :body do
160161
build_form do |f|
161162
f.inputs do
162163
f.input :title
@@ -171,19 +172,19 @@ def build_form(options = {}, &block)
171172
end
172173
end
173174
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
175176
end
176177
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]"})
178179
end
179180
end
180181

181182
context "with wrapper html" do
182183
it "should set a class" do
183-
build_form do |f|
184+
body = build_form do |f|
184185
f.input :title, :wrapper_html => { :class => "important" }
185186
end
186-
response.body.should have_tag("li", :attributes => {:class => "string optional important"})
187+
body.should have_tag("li", :attributes => {:class => "string optional important"})
187188
end
188189
end
189190

@@ -196,26 +197,26 @@ def build_form(options = {}, &block)
196197
"input :created_at, :as => :date" => /id\=\"post_created_at_2i\"/,
197198
}.each do |source, regex|
198199
it "should properly buffer #{source}" do
199-
build_form do |f|
200+
body = build_form do |f|
200201
f.inputs do
201202
f.instance_eval(source)
202203
f.instance_eval(source)
203204
end
204205
end
205-
response.body.scan(regex).size.should == 2
206+
body.scan(regex).size.should == 2
206207
end
207208
end
208209

209210
describe "datepicker input" do
210-
before do
211+
let :body do
211212
build_form do |f|
212213
f.inputs do
213214
f.input :created_at, :as => :datepicker
214215
end
215216
end
216217
end
217218
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",
219220
:class => "datepicker",
220221
:name => "post[created_at]" })
221222
end

0 commit comments

Comments
 (0)