Fix error saving data on new Calculator

This commit is contained in:
James Wu
2023-01-26 16:07:26 +09:00
committed by David Cook
parent 41ce4fbc16
commit ae5f2cc19d
2 changed files with 77 additions and 0 deletions

View File

@@ -182,6 +182,11 @@ module Spree
end
end
# Ensure the calculator to be updated is the correct type
if params_for_update["calculator_type"] && params_for_update["calculator_attributes"]
add_type_to_calculator_attributes(params_for_update)
end
params_for_update
end
end
@@ -191,6 +196,10 @@ module Spree
Rails.cache.delete(@payment_method.calculator.preference_cache_key(key))
end
end
def add_type_to_calculator_attributes(hash)
hash["calculator_attributes"]["type"] = hash["calculator_type"]
end
end
end
end

View File

@@ -415,4 +415,72 @@ describe '
expect(page).not_to have_unchecked_field "payment_method_distributor_ids_#{@distributors[0].id}"
end
end
context 'when updating payment method with invalid data and changing calculator type' do
let(:payment_method) { create(:payment_method, :flat_rate, amount: 10) }
# Persist preferences to test that when preferred values are not found in the cache,
# they are fetched from the database and displayed correctly
before do
Spree::Preferences::Store.instance.persistence = true
login_as_admin_and_visit spree.edit_admin_payment_method_path payment_method
fill_in 'payment_method_name', with: ''
fill_in 'payment_method_description', with: 'Edited description'
uncheck "payment_method_distributor_ids_#{@distributors[0].id}"
select2_select 'Flat Percent', from: 'calc_type'
click_button 'Update'
end
after do
Spree::Preferences::Store.instance.persistence = false
end
it 'displays the number of errors' do
expect(page).to have_content('2 errors')
end
it 'displays error messages' do
expect(page).to have_content("Name can't be blank")
expect(page).to have_content('At least one hub must be selected')
end
it 'displays the new calculator fields' do
expect(page).to have_field 'Flat Percent'
end
it 'highlights invalid fields' do
within '.field_with_errors:has(#payment_method_name)' do
expect(page).to have_field 'payment_method_name'
end
within '#hubs' do
expect(page).to have_selector('.red')
end
end
it 'keeps information that was just edited, even after multiple unsuccessful updates' do
expect(page).to have_field 'payment_method_name', with: ''
expect(page).to have_field 'payment_method_description', with: 'Edited description'
expect(page).to have_unchecked_field "payment_method_distributor_ids_#{@distributors[0].id}"
fill_in 'payment_method_name', with: 'Test name'
fill_in 'Flat Percent', with: 'invalid string'
click_button 'Update'
expect(page).to have_field 'payment_method_name', with: 'Test name'
expect(page).to have_field 'Flat Percent', with: 'invalid string'
expect(page).to have_field 'payment_method_description', with: 'Edited description'
expect(page).to have_unchecked_field "payment_method_distributor_ids_#{@distributors[0].id}"
end
it 'displays data fetched from the database after navigating away from the page' do
click_link 'Back To Payment Methods List'
click_link href: /#{spree.edit_admin_payment_method_path(payment_method)}/
expect(page).to have_field 'Amount', with: '10.0'
expect(page).not_to have_field 'payment_method_name', with: ''
expect(page).not_to have_field 'payment_method_description', with: 'Edited description'
expect(page).not_to have_unchecked_field "payment_method_distributor_ids_#{@distributors[0].id}"
end
end
end