require 'spec_helper' describe "companies/new" do context "default selection list" do before do @company = Factory.build(:company) assign(:company, @company) @parent_10 = Factory(:selection, :system_code => "state") @child_11 = Factory(:selection, :parent => @parent_10) @child_12 = Factory(:selection, :parent => @parent_10, :is_default => true) @child_13 = Factory(:selection, :parent => @parent_10) @child_14 = Factory(:selection, :parent => @parent_10) @parent_20 = Factory(:selection, :system_code => "priority") @child_21 = Factory(:selection, :parent => @parent_20, :is_default => true) @child_22 = Factory(:selection, :parent => @parent_21) end it "should display error when field does not match system_code" do assign(:field, :not_match) render assert_select "form", "Invalid system_code" end context "standard" do before(:each) do assign(:field, :state) assign(:options, nil) assign(:html_options, nil) end it "selection with default item" do render assert_select "form", :action => companies_path, :method => "post" do assert_select "select#company_state_id[name='company[state_id]']" do selected_options(@parent_10.children, @child_12, false) end end end it "selection with no default set" do @child_12.update_attribute(:is_default, false) render selected_options(@parent_10.children, nil, true) end it "selection with archived item" do @child_13.update_attribute(:archived, true) render selected_options(@parent_10.children-[@child_13], @child_12, false) end it "selection with archived default item" do @child_12.update_attribute(:archived, true) render selected_options(@parent_10.children-[@child_12], nil, true) end it "should have blank selection when :include_blank is true" do assign(:options, {:include_blank => true}) render selected_options(@parent_10.children, @child_12, true) end it "should not have blank when no default and :include_blank is false" do assign(:options, {:include_blank => false}) @child_12.update_attribute(:is_default, false) render selected_options(@parent_10.children, nil, false) end it "it should use fieldname which does not match a system_code" do assign(:options, {:system_code => "state"}) assign(:field, :genre) render selected_options(@parent_10.children, @child_12, false) end it "should use the value in state if set (and ignore default) - when page not saved and reloads" do @company.state = @child_14 render selected_options(@parent_10.children, @child_14, false) end end end end