@@ -69,28 +69,39 @@ module Spree
6969 end
7070
7171 describe 'tax recalculation' do
72- let! ( :ship_address ) { create ( :address ) }
73- let! ( :tax_zone ) { create ( :global_zone ) } # will include the above address
74- let! ( :tax_rate ) { create ( :tax_rate , zone : tax_zone , tax_categories : [ tax_category ] ) }
72+ let ( :tax_category ) { create ( :tax_category ) }
73+ let ( :ship_address ) { create ( :address , state : new_york ) }
74+ let ( :new_york ) { create ( :state , state_code : "NY" ) }
75+ let ( :tax_zone ) { create ( :zone , states : [ new_york ] ) }
76+
77+ let! ( :tax_rate ) do
78+ create (
79+ :tax_rate ,
80+ name : "New York Sales Tax" ,
81+ tax_categories : [ tax_category ] ,
82+ zone : tax_zone ,
83+ included_in_price : false ,
84+ amount : 0.1
85+ )
86+ end
7587
7688 let ( :order ) do
7789 create (
7890 :order_with_line_items ,
79- line_items_attributes : [ { price : 10 , variant : } ] ,
80- ship_address :,
91+ line_items_attributes : [ { price : 10 , variant : variant } ] ,
92+ ship_address : ship_address ,
8193 )
8294 end
8395 let ( :line_item ) { order . line_items [ 0 ] }
8496
8597 let ( :variant ) { create ( :variant , tax_category :) }
86- let ( :tax_category ) { create ( :tax_category ) }
8798
8899 context 'when the item quantity has changed' do
89100 before do
90101 line_item . update! ( quantity : 2 )
91102 end
92103
93- it 'updates the promotion amount ' do
104+ it 'updates the additional_tax_total ' do
94105 expect {
95106 order . recalculate
96107 } . to change {
@@ -99,6 +110,60 @@ module Spree
99110 end
100111 end
101112
113+ context 'when the address has changed to a different state' do
114+ let ( :new_shipping_address ) { create ( :address ) }
115+
116+ before do
117+ order . ship_address = new_shipping_address
118+ end
119+
120+ it 'removes the old taxes' do
121+ expect {
122+ order . recalculate
123+ } . to change {
124+ order . all_adjustments . tax . count
125+ } . from ( 1 ) . to ( 0 )
126+
127+ expect ( order . additional_tax_total ) . to eq 0
128+ expect ( order . adjustment_total ) . to eq 0
129+ end
130+ end
131+
132+ context "with an order-level tax adjustment" do
133+ let ( :colorado ) { create ( :state , state_code : "CO" ) }
134+ let ( :colorado_tax_zone ) { create ( :zone , states : [ colorado ] ) }
135+ let ( :ship_address ) { create ( :address , state : colorado ) }
136+
137+ let! ( :colorado_delivery_fee ) do
138+ create (
139+ :tax_rate ,
140+ amount : 0.27 ,
141+ calculator : Spree ::Calculator ::FlatFee . new ,
142+ level : "order" ,
143+ name : "Colorado Delivery Fee" ,
144+ tax_categories : [ tax_category ] ,
145+ zone : colorado_tax_zone
146+ )
147+ end
148+
149+ before { order . recalculate }
150+
151+ it "updates the order-level tax adjustment" do
152+ expect {
153+ order . ship_address = create ( :address )
154+ order . recalculate
155+ } . to change { order . additional_tax_total } . from ( 0.27 ) . to ( 0 ) .
156+ and change { order . adjustment_total } . from ( 0.27 ) . to ( 0 )
157+ end
158+
159+ it "deletes the order-level tax adjustments when it persists the order" do
160+ expect {
161+ order . ship_address = create ( :address )
162+ order . recalculate
163+ } . to change { order . all_adjustments . count } . from ( 1 ) . to ( 0 )
164+ end
165+ end
166+
102167 context 'with a custom tax_calculator_class' do
103168 let ( :custom_calculator_class ) { double }
104169 let ( :custom_calculator_instance ) { double }
0 commit comments