|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe Spree::ItemTotal do |
| 6 | + describe "#recalculate!" do |
| 7 | + subject { described_class.new(item).recalculate! } |
| 8 | + |
| 9 | + let!(:item) { create :line_item, adjustments: } |
| 10 | + |
| 11 | + let(:tax_rate) { create(:tax_rate) } |
| 12 | + |
| 13 | + let(:arbitrary_adjustment) { create :adjustment, amount: 1, source: nil } |
| 14 | + let(:included_tax_adjustment) { create :adjustment, amount: 2, source: tax_rate, included: true } |
| 15 | + let(:additional_tax_adjustment) { create :adjustment, amount: 3, source: tax_rate, included: false } |
| 16 | + |
| 17 | + context "with multiple types of adjustments" do |
| 18 | + let(:marked_for_destruction_included_tax_adjustment) { create(:adjustment, amount: 5, source: tax_rate, included: true) } |
| 19 | + let(:marked_for_destruction_additional_tax_adjustment) { create(:adjustment, amount: 7, source: tax_rate, included: false) } |
| 20 | + |
| 21 | + let(:adjustments) { |
| 22 | + [ |
| 23 | + arbitrary_adjustment, |
| 24 | + included_tax_adjustment, |
| 25 | + additional_tax_adjustment, |
| 26 | + marked_for_destruction_included_tax_adjustment, |
| 27 | + marked_for_destruction_additional_tax_adjustment |
| 28 | + ] |
| 29 | + } |
| 30 | + |
| 31 | + before do |
| 32 | + [marked_for_destruction_included_tax_adjustment, marked_for_destruction_additional_tax_adjustment] |
| 33 | + .each(&:mark_for_destruction) |
| 34 | + end |
| 35 | + |
| 36 | + it "updates item totals" do |
| 37 | + expect { |
| 38 | + subject |
| 39 | + }.to change(item, :adjustment_total).from(0).to(4). |
| 40 | + and change { item.included_tax_total }.from(0).to(2). |
| 41 | + and change { item.additional_tax_total }.from(0).to(3) |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + context "with only an arbitrary adjustment" do |
| 46 | + let(:adjustments) { [arbitrary_adjustment] } |
| 47 | + |
| 48 | + it "updates the adjustment total" do |
| 49 | + expect { |
| 50 | + subject |
| 51 | + }.to change { item.adjustment_total }.from(0).to(1) |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context "with only tax adjustments" do |
| 56 | + let(:adjustments) { [included_tax_adjustment, additional_tax_adjustment] } |
| 57 | + |
| 58 | + it "updates the adjustment total" do |
| 59 | + expect { |
| 60 | + subject |
| 61 | + }.to change { item.adjustment_total }.from(0).to(3). |
| 62 | + and change { item.included_tax_total }.from(0).to(2). |
| 63 | + and change { item.additional_tax_total }.from(0).to(3) |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | +end |
0 commit comments