spec/unit/form_builder_spec.rb in activeadmin-0.1.1 vs spec/unit/form_builder_spec.rb in activeadmin-0.2.0

- old
+ new

@@ -1,120 +1,129 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe_with_render ActiveAdmin::FormBuilder do +describe ActiveAdmin::FormBuilder do + include Arbre::HTML + let(:assigns){ {} } - def build_form(&block) - Admin::PostsController.form(&block) - get :new + # Setup an ActionView::Base object which can be used for + # generating the form for. + let(:helpers) do + view = 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 + + view end + def build_form(options = {}, &block) + active_admin_form_for Post.new, options, &block + end + context "in general" do - before do + let :body do build_form do |f| f.inputs do f.input :title f.input :body end - f.label :title, "My Super Title" - f.text_field :title f.buttons do - f.submit "My Submit Button" f.commit_button "Submit Me" f.commit_button "Another Button" end end end + it "should generate a text input" do - response.should have_tag("input", :attributes => { :type => "text", + body.should have_tag("input", :attributes => { :type => "text", :name => "post[title]" }) end - it "should generate a label using the default form methods" do - response.should have_tag("label", "My Super Title") - end it "should generate a textarea" do - response.should have_tag("textarea", :attributes => { :name => "post[body]" }) + body.should have_tag("textarea", :attributes => { :name => "post[body]" }) end it "should only generate the form once" do - response.body.scan(/My Super Title/).size.should == 1 + body.scan(/Title/).size.should == 1 end it "should generate buttons" do - response.should have_tag("input", :attributes => { :type => "submit", + body.should have_tag("input", :attributes => { :type => "submit", :value => "Submit Me" }) - response.should have_tag("input", :attributes => { :type => "submit", + body.should have_tag("input", :attributes => { :type => "submit", :value => "Another Button" }) end end describe "passing in options" do - before do - Admin::PostsController.form :html => { :multipart => true } do |f| + let :body do + build_form :html => { :multipart => true } do |f| f.inputs :title f.buttons end - get :new end it "should pass the options on to the form" do - response.should have_tag("form", :attributes => { :enctype => "multipart/form-data" }) + body.should have_tag("form", :attributes => { :enctype => "multipart/form-data" }) end end - context "with default settings" do - before do - Admin::PostsController.reset_form_config! - get :new - end - it "should generate one post title field" do - response.body.scan('id="post_title"').size.should == 1 - end - end - context "with buttons" do it "should generate the form once" do - build_form do |f| + body = build_form do |f| f.inputs do f.input :title end f.buttons end - response.body.scan(/id=\"post_title\"/).size.should == 1 + body.scan(/id=\"post_title\"/).size.should == 1 end - it "should generate one button" do - build_form do |f| + it "should generate one button and a cancel link" do + body = build_form do |f| f.buttons end - response.body.scan(/type=\"submit\"/).size.should == 1 + body.scan(/type=\"submit\"/).size.should == 1 + body.scan(/class=\"cancel\"/).size.should == 1 end it "should generate multiple buttons" do - build_form do |f| + body = build_form do |f| f.buttons do - f.submit "Create" f.commit_button "Create & Continue" f.commit_button "Create & Edit" end end - response.body.scan(/type=\"submit\"/).size.should == 3 + body.scan(/type=\"submit\"/).size.should == 2 + body.scan(/class=\"cancel\"/).size.should == 0 end end context "without passing a block to inputs" do - before do + let :body do build_form do |f| f.inputs :title, :body end end it "should have a title input" do - response.should have_tag("input", :attributes => { :type => "text", + body.should have_tag("input", :attributes => { :type => "text", :name => "post[title]" }) end it "should have a body textarea" do - response.should have_tag("textarea", :attributes => { :name => "post[body]" }) + body.should have_tag("textarea", :attributes => { :name => "post[body]" }) end end context "with semantic fields for" do - before do + let :body do build_form do |f| f.inputs do f.input :title f.input :body end @@ -125,45 +134,45 @@ author.inputs :first_name, :last_name end end end it "should generate a nested text input once" do - response.body.scan("post_author_attributes_first_name_input").size.should == 1 + body.scan("post_author_attributes_first_name_input").size.should == 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 - before do + let :body do build_form do |f| f.input :author end end it "should create 2 options" do - response.body.scan(/\<option/).size.should == 3 + body.scan(/\<option/).size.should == 3 end end describe "as radio buttons" do - before do + let :body do build_form do |f| f.input :author, :as => :radio end end it "should create 2 radio buttons" do - response.body.scan(/type=\"radio\"/).size.should == 2 + body.scan(/type=\"radio\"/).size.should == 2 end end end context "with inputs 'for'" do - before do + let :body do build_form do |f| f.inputs do f.input :title f.input :body end @@ -174,63 +183,54 @@ author.inputs :first_name, :last_name end end end it "should generate a nested text input once" do - response.body.scan("post_author_attributes_first_name_input").size.should == 1 + body.scan("post_author_attributes_first_name_input").size.should == 1 end it "should add an author first name field" do - response.body.should have_tag("input", :attributes => { :name => "post[author_attributes][first_name]"}) + body.should have_tag("input", :attributes => { :name => "post[author_attributes][first_name]"}) end end context "with wrapper html" do it "should set a class" do - build_form do |f| + body = build_form do |f| f.input :title, :wrapper_html => { :class => "important" } end - response.should have_tag("li", :attributes => {:class => "string optional important"}) + body.should have_tag("li", :attributes => {:class => "string optional important"}) end end - # This checks that each input can be added via the standard - # rails method as well as using the form builder's #input method. { "input :title, :as => :string" => /id\=\"post_title\"/, - "text_field :title" => /id\=\"post_title\"/, "input :title, :as => :text" => /id\=\"post_title\"/, - "text_area :title" => /id\=\"post_title\"/, "input :created_at, :as => :time" => /id\=\"post_created_at_2i\"/, - "time_select :created_at" => /id\=\"post_created_at_2i\"/, "input :created_at, :as => :datetime" => /id\=\"post_created_at_2i\"/, - "datetime_select :created_at" => /id\=\"post_created_at_2i\"/, "input :created_at, :as => :date" => /id\=\"post_created_at_2i\"/, - "date_select :created_at" => /id\=\"post_created_at_2i\"/, - "radio_button :title, 'title'" => /id\=\"post_title_title\"/, - "file_field :title" => /id\=\"post_title\"/, - "hidden_field :title" => /id\=\"post_title\"/, - "label :title" => /for\=\"post_title\"/, }.each do |source, regex| it "should properly buffer #{source}" do - build_form do |f| - f.instance_eval(source) - f.instance_eval(source) + body = build_form do |f| + f.inputs do + f.instance_eval(source) + f.instance_eval(source) + end end - response.body.scan(regex).size.should == 2 + body.scan(regex).size.should == 2 end end describe "datepicker input" do - before do + let :body do build_form do |f| f.inputs do f.input :created_at, :as => :datepicker end end end it "should generate a text input with the class of datepicker" do - response.should have_tag("input", :attributes => { :type => "text", + body.should have_tag("input", :attributes => { :type => "text", :class => "datepicker", :name => "post[created_at]" }) end end