diff --git a/app/views/targets/_form.html.erb b/app/views/targets/_form.html.erb index 383ca45..30f2d79 100644 --- a/app/views/targets/_form.html.erb +++ b/app/views/targets/_form.html.erb @@ -20,12 +20,12 @@ <%= link_to t(".button_delete_target"), '#', class: 'icon icon-del', style: (@targets.many? ? "" : "display:none"), - onclick: "deleteTarget(); return false;" %> + onclick: "deleteTarget(event); return false;" %>
<% end %><%= link_to t(".button_new_target"), '#', class: 'icon icon-add', - onclick: 'newTarget(); return false;' %> + onclick: 'newTarget(event); return false;' %>
<%= javascript_tag do %> @@ -37,7 +37,7 @@ $('p.target select:first-child[id$=__quantity_id]').trigger(jQuery.Event('change')); }) - function newTarget() { + function newTarget(event) { var form = $(event.target).closest('form'); var row = form.find('p.target:visible:last'); var new_row = row.clone().insertAfter(row); @@ -48,7 +48,7 @@ form.find('p.target:visible a.icon-del').show(); } - function deleteTarget() { + function deleteTarget(event) { var form = $(event.target).closest('form'); var row = $(event.target).closest('p.target'); if (row.find('input[id$=__id]').val()) { diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index 0e9fd48..420195b 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -6,6 +6,8 @@ ENV["RACK_ENV"] = "test" require File.expand_path('../../../../test/application_system_test_case', __FILE__) class BodyTrackingSystemTestCase < ApplicationSystemTestCase + #Selenium::WebDriver.logger.level = :debug + profile = Selenium::WebDriver::Firefox::Profile.new profile['browser.download.dir'] = DOWNLOADS_PATH profile['browser.download.folderList'] = 2 diff --git a/test/system/targets_test.rb b/test/system/targets_test.rb index 3321e4e..3ee9d1a 100644 --- a/test/system/targets_test.rb +++ b/test/system/targets_test.rb @@ -169,12 +169,13 @@ class TargetsTest < BodyTrackingSystemTestCase def test_create_multiple_targets end + def test_create_form_retains_data_on_failure + end + def test_create_duplicate_for_persisted_target_should_fail # TODO: extend with item + scope source = @project.targets.sample - threshold_quantity = @project.quantities.target.roots.sample - threshold_value = rand(-2000.0..2000.0).to_d(4) - threshold_unit = @project.units.sample + msg = t('activerecord.errors.models.target.attributes.base.duplicated_record') assert_no_difference 'Target.count' do visit goal_targets_path(source.goal) @@ -183,18 +184,48 @@ class TargetsTest < BodyTrackingSystemTestCase fill_in t(:field_effective_from), with: source.effective_from within 'p.target' do select source.quantity.name - select threshold_quantity.name - fill_in with: threshold_value - select threshold_unit.shortname + select @project.quantities.target.roots.sample.name + fill_in with: rand(-2000.0..2000.0).to_d(4) + select @project.units.sample.shortname end click_on t(:button_create) + assert_selector :xpath, '//p[@class="target"]//preceding-sibling::div', text: msg end end - msg = I18n.t('activerecord.errors.models.target.attributes.base.duplicated_record') - assert_selector :xpath, '//form//p[@class="target"]//preceding-sibling::div', text: msg end def test_create_duplicated_targets_should_fail + date = Date.current + rand(-10..10).days + quantity = @project.quantities.except_targets + .joins("LEFT OUTER JOIN targets ON targets.quantity_id = quantities.id \ + AND targets.effective_from = #{date}") + .where(targets: {id: nil}).sample + assert quantity + msg = t('activerecord.errors.models.target.attributes.base.duplicated_record') + + assert_no_difference 'Target.count' do + visit goal_targets_path(@project.goals.binding) + click_link t('targets.contextual.link_new_target') + within 'form#new-target-form' do + fill_in t(:field_effective_from), with: date + within :xpath, '//p[@class="target"][1]' do + select quantity.name + select @project.quantities.target.roots.sample.name + fill_in with: rand(-2000.0..2000.0).to_d(4) + select @project.units.sample.shortname + end + click_link t('targets.form.button_new_target') + within :xpath, '//p[@class="target"][2]' do + select quantity.name + select @project.quantities.target.roots.sample.name + fill_in with: rand(-2000.0..2000.0).to_d(4) + select @project.units.sample.shortname + end + click_on t(:button_create) + assert_selector :xpath, '//p[@class="target"][last()]//preceding-sibling::div', + text: msg + end + end end def test_create_and_simultaneously_remove_persisted_duplicate @@ -212,7 +243,7 @@ class TargetsTest < BodyTrackingSystemTestCase within find('td', text: target.effective_from).ancestor('tr') do click_link t(:button_edit) - within find(:xpath, 'following-sibling::tr//form') do + within :xpath, 'following-sibling::tr//form' do assert has_field?(t(:field_effective_from), with: target.effective_from) # NOTE: this assumes target.quantity is unique which is not enforced now