test enterprise fees creation with tax category inheritance flag

This commit is contained in:
Mohamed ABDELLANI
2023-04-08 07:19:51 +01:00
parent a22fe9f948
commit 6771d2e7be
2 changed files with 42 additions and 13 deletions

View File

@@ -2,10 +2,10 @@
class DisableInheritsTaxCategoryFromPerOrderEnterpriseFees < ActiveRecord::Migration[6.1]
class EnterpriseFee < ApplicationRecord
has_one :calculator, as: :calculable, class_name: "Spree::Calculator", dependent: :destroy
has_many :calculator, class_name: "Spree::Calculator", foreign_key: :calculable_id
end
class Spree
module Spree
class Calculator < ApplicationRecord
self.table_name = 'spree_calculators'
end
@@ -18,7 +18,7 @@ class DisableInheritsTaxCategoryFromPerOrderEnterpriseFees < ActiveRecord::Migra
end
def calculators
Spree::Calculator.where(type: per_order_calculators)
Spree::Calculator.where(type: per_order_calculators, calculable_type: 'EnterpriseFee')
end
def per_order_calculators

View File

@@ -10,17 +10,46 @@ describe EnterpriseFee do
describe "validations" do
it { is_expected.to validate_presence_of(:name) }
it "requires a per-item calculator to inherit tax" do
subject = build(
:enterprise_fee,
inherits_tax_category: true,
calculator: Calculator::FlatRate.new
)
describe "requires a per-item calculator to inherit tax" do
let(:per_order_calculators){
[
Calculator::FlatRate,
Calculator::FlexiRate,
Calculator::PriceSack,
]
}
expect(subject.save).to eq false
expect(subject.errors.full_messages.first).to eq(
"Inheriting the tax categeory requires a per-item calculator."
)
let(:per_item_calculators){
[
Calculator::PerItem,
Calculator::FlatPercentPerItem
]
}
it "is valid when inheriting tax and using a per-item calculator" do
per_item_calculators.each do |calculator|
subject = build(
:enterprise_fee,
inherits_tax_category: true,
calculator: calculator.new
)
expect(subject.save).to eq true
end
end
it "is invalid when inheriting tax and using a per-order calculator" do
per_order_calculators.each do |calculator|
subject = build(
:enterprise_fee,
inherits_tax_category: true,
calculator: calculator.new
)
expect(subject.save).to eq false
expect(subject.errors.full_messages.first).to eq(
"Inheriting the tax categeory requires a per-item calculator."
)
end
end
end
end