From ae5f2cc19d950d10dd11c733879afdb792e00bb2 Mon Sep 17 00:00:00 2001 From: James Wu Date: Thu, 26 Jan 2023 16:07:26 +0900 Subject: [PATCH] Fix error saving data on new Calculator --- .../spree/admin/payment_methods_controller.rb | 9 +++ spec/system/admin/payment_method_spec.rb | 68 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/app/controllers/spree/admin/payment_methods_controller.rb b/app/controllers/spree/admin/payment_methods_controller.rb index a6114266de..9fc0207d77 100644 --- a/app/controllers/spree/admin/payment_methods_controller.rb +++ b/app/controllers/spree/admin/payment_methods_controller.rb @@ -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 diff --git a/spec/system/admin/payment_method_spec.rb b/spec/system/admin/payment_method_spec.rb index c4343619bb..e079252694 100644 --- a/spec/system/admin/payment_method_spec.rb +++ b/spec/system/admin/payment_method_spec.rb @@ -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