diff --git a/spec/features/add_budget_spec.rb b/spec/features/add_budget_spec.rb index bc2cd16e4a..348db6a4ba 100644 --- a/spec/features/add_budget_spec.rb +++ b/spec/features/add_budget_spec.rb @@ -53,6 +53,8 @@ describe 'adding a new budget', type: :feature, js: true do FactoryGirl.create :cost_type, name: 'Post-war', unit: 'cap', unit_plural: 'caps' end + let(:new_budget_page) { Pages::NewBudget.new project.identifier } + before do project.add_member! user, FactoryGirl.create(:role) @@ -61,53 +63,28 @@ describe 'adding a new budget', type: :feature, js: true do end it 'creates the budget including the given cost items' do - visit new_projects_cost_object_path(project) + new_budget_page.visit! fill_in 'Subject', with: 'First Aid' - mat_id = 'cost_object_new_material_budget_item_attributes' - fill_in "#{mat_id}_0_units", with: 3 - fill_in "#{mat_id}_0_comments", with: 'RadAway' - - within('#material_budget_items_fieldset') do - find('a', text: 'Add planned costs').native.send_keys(:return) - end - - fill_in "#{mat_id}_1_units", with: 2 - fill_in "#{mat_id}_1_comments", with: 'Rad-X' - - lab_id = 'cost_object_new_labor_budget_item_attributes' - fill_in "#{lab_id}_0_hours", with: 5 - select user.name, from: "#{lab_id}_0_user_id" - fill_in "#{lab_id}_0_comments", with: 'treatment' + new_budget_page.add_unit_costs! 3, comment: 'RadAway' + new_budget_page.add_unit_costs! 2, comment: 'Rad-X' - within('#labor_budget_items_fieldset') do - find('a', text: 'Add planned costs').native.send_keys(:return) - end - - fill_in "#{lab_id}_1_hours", with: 2 - select user.name, from: "#{lab_id}_1_user_id" - fill_in "#{lab_id}_1_comments", with: 'attendance' + new_budget_page.add_labor_costs! 5, user_name: user.name, comment: 'treatment' + new_budget_page.add_labor_costs! 2, user_name: user.name, comment: 'attendance' click_on 'Create' - - units = find('fieldset', text: 'UNITS') - units.click - expect(page).to have_content('Successful creation') - div = find('h4', text: 'Planned unit costs').find(:xpath, '..') - currency = div.first('tbody td.currency') - expect(currency).to have_content('150.00 EUR') - expect(div.all('tbody td.currency').last).to have_content('100.00 EUR') - expect(div.first('tfoot td.currency')).to have_content('250.00 EUR') - - find('fieldset', text: 'LABOR').click + new_budget_page.toggle_unit_costs! + expect(new_budget_page.unit_costs_at(1)).to have_content '150.00 EUR' + expect(new_budget_page.unit_costs_at(2)).to have_content '100.00 EUR' + expect(new_budget_page.overall_unit_costs).to have_content '250.00 EUR' - labor = find('h4', text: 'Planned labor costs').find(:xpath, '..') - expect(labor.first('tbody td.currency')).to have_content('125.00 EUR') - expect(labor.all('tbody td.currency').last).to have_content('50.00 EUR') - expect(labor.first('tfoot td.currency')).to have_content('175.00 EUR') + new_budget_page.toggle_labor_costs! + expect(new_budget_page.labor_costs_at(1)).to have_content '125.00 EUR' + expect(new_budget_page.labor_costs_at(2)).to have_content '50.00 EUR' + expect(new_budget_page.overall_labor_costs).to have_content '175.00 EUR' end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0d73234cef..7e82cf03d0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -21,3 +21,5 @@ require 'spec_helper' require File.join(File.dirname(__FILE__), 'plugin_spec_helper') + +Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f } diff --git a/spec/support/pages/new_budget.rb b/spec/support/pages/new_budget.rb new file mode 100644 index 0000000000..c043659d16 --- /dev/null +++ b/spec/support/pages/new_budget.rb @@ -0,0 +1,128 @@ +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See doc/COPYRIGHT.rdoc for more details. +#++ + +require 'support/pages/page' + +module Pages + class NewBudget < Page + attr_reader :project_identifier + + def initialize(project_identifier) + @project_identifier = project_identifier + end + + def path + "/projects/#{project_identifier}/cost_objects/new" + end + + ## + # Adds planned unit costs with the default cost type. + def add_unit_costs!(num_units, comment: nil) + fill_in "#{unit_cost_attr_id}_#{unit_rows}_units", with: num_units + fill_in "#{unit_cost_attr_id}_#{unit_rows}_comments", with: comment if comment.present? + + add_unit_costs_row! + end + + ## + # Adds planned labor costs with the default cost type. + def add_labor_costs!(num_hours, user_name:, comment: nil) + fill_in "#{labor_cost_attr_id}_#{labor_rows}_hours", with: num_hours + select user_name, from: "#{labor_cost_attr_id}_#{labor_rows}_user_id" + fill_in "#{labor_cost_attr_id}_#{labor_rows}_comments", with: comment if comment.present? + + add_labor_costs_row! + end + + def add_unit_costs_row! + link = find('#material_budget_items_fieldset a', text: 'Add planned costs') + link.native.send_keys :return + + @unit_rows = unit_rows + 1 + end + + def add_labor_costs_row! + link = find('#labor_budget_items_fieldset a', text: 'Add planned costs') + link.native.send_keys :return + + @labor_rows = labor_rows + 1 + end + + def unit_costs_at(num_row) + unit_costs_container.all('tbody td.currency')[num_row - 1] + end + + def overall_unit_costs + unit_costs_container.first('tfoot td.currency') + end + + def labor_costs_at(num_row) + labor_costs_container.all('tbody td.currency')[num_row - 1] + end + + def overall_labor_costs + labor_costs_container.first('tfoot td.currency') + end + + def unit_costs_container + find_container('Planned unit costs') + end + + def labor_costs_container + find_container('Planned labor costs') + end + + def find_container(title) + find('h4', text: title).find(:xpath, '..') + end + + def toggle_unit_costs! + find('fieldset', text: 'UNITS').click + end + + def toggle_labor_costs! + find('fieldset', text: 'LABOR').click + end + + def unit_cost_attr_id + 'cost_object_new_material_budget_item_attributes' + end + + def labor_cost_attr_id + 'cost_object_new_labor_budget_item_attributes' + end + + def unit_rows + @unit_rows ||= 0 + end + + def labor_rows + @labor_rows ||= 0 + end + end +end