Skip to content
Browse files

added specs

  • Loading branch information...
1 parent eab0d61 commit 7afcb1fd7acff4c1e1b616868cc58545af487d95 Nico committed May 25, 2012
View
BIN .DS_Store
Binary file not shown.
View
4 .gitignore
@@ -15,4 +15,6 @@ spec/reports
test/tmp
test/version_tmp
tmp
-.idea
+.idea
+spec/test_nested_form_fields/log
+spec/test_nested_form_fields/tmp
View
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
5 nested_form_fields.gemspec
@@ -15,4 +15,9 @@ Gem::Specification.new do |gem|
gem.name = "nested_form_fields"
gem.require_paths = ["lib"]
gem.version = NestedFormFields::VERSION
+
+ gem.add_development_dependency 'rspec-rails'
+ gem.add_development_dependency 'assert_difference'
+ gem.add_development_dependency 'capybara'
+ gem.add_development_dependency 'selenium-webdriver'
end
View
81 spec/integration/nested_form_fields_spec.rb
@@ -0,0 +1,81 @@
+require 'spec_helper'
+
+describe 'a form with nested projects with nested todos', :js => true do
+
+ it 'creates a user with nested projects with nested todos' do
+ visit '/'
+
+ fill_in 'user_name', with: 'user name'
+
+ click_link 'Add Project'
+ page.should have_css('fieldset.nested_user_projects')
+ fill_in 'user_projects_attributes_0_name', with: 'p1 name'
+ fill_in 'user_projects_attributes_0_description', with: 'p1 description'
+
+ click_link 'Add Todo'
+ page.should have_css('fieldset.nested_user_projects_0_todos')
+ fill_in 'user_projects_attributes_0_todos_attributes_0_description', with: 'todo text'
+
+ click_link 'Add Project'
+ fill_in 'user_projects_attributes_1_name', with: 'p2 name'
+ fill_in 'user_projects_attributes_1_description', with: 'p2 description'
+
+ assert_difference 'User.count', +1 do
+ click_on 'Create User'
+ end
+
+ user = User.last
+ projects = user.projects
+ todos = projects.first.todos
+ projects.count.should == 2
+ todos.count.should == 1
+ user.name.should == 'user name'
+ projects[0].name.should == 'p1 name'
+ projects[0].description.should == 'p1 description'
+ projects[1].name.should == 'p2 name'
+ projects[1].description.should == 'p2 description'
+ todos[0].description.should == 'todo text'
+ end
+
+ let(:user) do
+ user = User.create(name: 'user name')
+ user.projects.create(name: 'p1 name', description: 'p1 description')
+ user.projects.create(name: 'p2 name', description: 'p2 description')
+ user.projects.first.todos.create(description: 'todo text')
+ user
+ end
+
+ it 'edits a user with nested projects with nested todos' do
+ Capybara.ignore_hidden_elements = true
+
+ visit edit_user_path(user)
+
+ page.should have_css('fieldset.nested_user_projects_0_todos')
+ page.find('.nested_user_projects_0_todos .remove_nested_fields_link').click
+ page.should_not have_css('fieldset.nested_user_projects_0_todos')
+
+ page.all('.nested_user_projects .remove_nested_fields_link').count.should == 2
+ page.all('.nested_user_projects .remove_nested_fields_link').last.click
+ page.all('.nested_user_projects .remove_nested_fields_link').count.should == 1
+
+ fill_in 'user_projects_attributes_0_name', with: 'new name'
+
+ click_link 'Add Todo'
+ page.should have_css('fieldset.nested_user_projects_0_todos')
+
+ fill_in 'user_projects_attributes_0_todos_attributes_1_description', with: 'new text'
+
+ click_on 'Update User'
+
+ projects = user.projects
+ todos = projects.first.todos
+ projects.count.should == 1
+ todos.count.should == 1
+ projects[0].name.should == 'new name'
+ todos[0].description.should == 'new text'
+
+ Capybara.ignore_hidden_elements = false
+ end
+
+
+end
View
39 spec/spec_helper.rb
@@ -0,0 +1,39 @@
+# This file was generated by the `rspec --init` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# Require this file using `require "spec_helper.rb"` to ensure that it is only
+# loaded once.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+
+ENV["RAILS_ENV"] = 'test'
+require File.expand_path("../test_nested_form_fields/config/environment", __FILE__)
+require 'rspec/rails'
+require 'assert_difference'
+
+# Add this to load Capybara integration:
+require 'capybara/rspec'
+require 'capybara/rails'
+
+Capybara.default_selector = :css
+
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.include AssertDifference
+ config.use_transactional_fixtures = true
+end
+
+
+class ActiveRecord::Base
+ mattr_accessor :shared_connection
+ @@shared_connection = nil
+
+ def self.connection
+ @@shared_connection || retrieve_connection
+ end
+end
+
+# Forces all threads to share the same connection. This works on
+# Capybara because it starts the web server in a thread.
+ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
+
1 spec/test_nested_form_fields
@@ -0,0 +1 @@
+Subproject commit 95f96c29facccbb5abbed0b5f97ea11a2b123c84

0 comments on commit 7afcb1f

Please sign in to comment.
Something went wrong with that request. Please try again.