spec/acceptance_spec.rb in freeform-1.0.2 vs spec/acceptance_spec.rb in freeform-1.0.3

- old
+ new

@@ -5,11 +5,11 @@ klass = Class.new(FreeForm::Form) do form_input_key :task form_model :task validate_models allow_destroy_on_save - + property :name, :on => :task property :start_date, :on => :task property :end_date, :on => :task end # This wrapper just avoids CONST warnings @@ -21,22 +21,22 @@ let(:form_class) do klass = Class.new(FreeForm::Form) do form_input_key :company form_models :company - child_model :project do + child_model :project do company.project.present? ? company.project : company.build_project end validate_models allow_destroy_on_save - + property :name, :on => :company, :as => :company_name property :name, :on => :project, :as => :project_name property :due_date, :on => :project has_many :tasks, :class => Module::TaskForm, :default_initializer => :default_task_initializer - + def default_task_initializer { :task => Task.new(:project => project) } end end # This wrapper just avoids CONST warnings @@ -49,11 +49,11 @@ let(:form) do f = form_class.new( :company => Company.new ) f.build_task f end - + describe "form initialization", :initialization => true do it "initializes with Company model" do form.company.should be_a(Company) end @@ -63,11 +63,11 @@ it "initializes with Task model" do form.tasks.first.task.should be_a(Task) end - it "initializes with Task with project parent", :failing => true do + it "initializes with Task with project parent" do task = form.tasks.first.task task.project.should eq(form.project) end end @@ -107,15 +107,15 @@ "end_date(2i)" => "12", "end_date(3i)" => "15", } } } end - + before(:each) do form.fill(attributes) end - + it "assigns company name" do form.company_name.should eq("dummycorp") form.company.name.should eq("dummycorp") end @@ -171,16 +171,16 @@ "end_date(2i)" => "12", "end_date(3i)" => "15", } } } end - + before(:each) do form.fill(attributes) form.valid? end - + it "should be invalid" do form.should_not be_valid end it "should have errors on project name" do @@ -212,25 +212,63 @@ "end_date(2i)" => "12", "end_date(3i)" => "15", } } } end - + before(:each) do form.fill(attributes) form.valid? end - + it "should be invalid" do form.should_not be_valid end it "should have errors on last tasks's start_date" do form.tasks.last.errors[:start_date].should eq(["can't be blank"]) end end + context "with invalid, marked for destruction nested model", :failing => true do + let(:attributes) do { + :company_name => "dummycorp", + :project_name => "rails app", + "due_date(1i)" => "2014", + "due_date(2i)" => "10", + "due_date(3i)" => "30", + :tasks_attributes => { + "0" => { + :name => "task_1", + "start_date(1i)" => "2012", + "start_date(2i)" => "1", + "start_date(3i)" => "2", + }, + "1" => { + :name => "task_2", + "end_date(1i)" => "2011", + "end_date(2i)" => "12", + "end_date(3i)" => "15", + :_destroy => "1" + } + } } + end + + before(:each) do + form.fill(attributes) + form.valid? + end + + it "should be valid" do + form.should be_valid + end + + it "should not have errors" do + form.errors.should be_empty + end + end + context "with valid attributes" do let(:attributes) do { :company_name => "dummycorp", :project_name => "railsapp", "due_date(1i)" => "2014", @@ -252,15 +290,15 @@ "end_date(2i)" => "12", "end_date(3i)" => "15", } } } end - + before(:each) do form.fill(attributes) end - + it "should be valid" do form.should be_valid end end end @@ -286,15 +324,15 @@ "end_date(2i)" => "12", "end_date(3i)" => "15", } } } end - + before(:each) do form.fill(attributes) end - + it "should return false on 'save'" do form.save.should be_false end it "should raise error on 'save!'" do @@ -325,15 +363,15 @@ "end_date(2i)" => "12", "end_date(3i)" => "15", } } } end - + before(:each) do form.fill(attributes) end - + it "should return true on 'save', and call save on other models" do form.company.should_receive(:save).and_return(true) form.project.should_receive(:save).and_return(true) form.tasks.first.task.should_receive(:save).and_return(true) form.tasks.last.task.should_receive(:save).and_return(true) @@ -356,11 +394,11 @@ form.project.should_receive(:destroy).and_return(true) form.tasks.first.task.should_receive(:save).and_return(true) form.tasks.last.task.should_receive(:save).and_return(true) form.save end - + it "destroys models on save if set through attribute" do form.fill({:_destroy => "1"}) form.company.should_receive(:destroy).and_return(true) form.project.should_receive(:destroy).and_return(true) form.tasks.first.task.should_receive(:save).and_return(true) @@ -375,21 +413,21 @@ form.tasks.first.task.should_receive(:destroy).and_return(true) form.tasks.last.task.should_receive(:save).and_return(true) form.save end end - + describe "save!" do it "destroys models on save! if set" do form._destroy = true form.company.should_receive(:destroy).and_return(true) form.project.should_receive(:destroy).and_return(true) form.tasks.first.task.should_receive(:save!).and_return(true) form.tasks.last.task.should_receive(:save!).and_return(true) form.save! end - + it "destroys models on save! if set" do form.fill({:_destroy => "1"}) form.company.should_receive(:destroy).and_return(true) form.project.should_receive(:destroy).and_return(true) form.tasks.first.task.should_receive(:save!).and_return(true) @@ -407,6 +445,6 @@ end end end end end -end \ No newline at end of file +end