summaryrefslogtreecommitdiff
path: root/spec/super_good/solidus_taxjar/tax_calculator_spec.rb
blob: f7a792920bba67f84fbdb46952b3a012d0aee563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
require 'spec_helper'

RSpec.describe ::SuperGood::SolidusTaxJar::TaxCalculator do
  describe "#calculate" do
    subject { calculator.calculate }

    let(:calculator) { described_class.new(order, api: dummy_api) }

    let(:dummy_api) do
      instance_double ::SuperGood::SolidusTaxJar::API
    end

    let(:order) do
      ::Spree::Order.new(
        id: 10,
        store: store,
        ship_address: address,
        line_items: line_items,
        shipments: [
          boring_shipment,
          shipment_with_adjustment,
          shipment_with_existing_tax
        ]
      )
    end

    let(:line_items) { [::Spree::LineItem.new] }

    let(:boring_shipment) do
      ::Spree::Shipment.new(id: 1, cost: 7)
    end

    let(:shipment_with_adjustment) do
      ::Spree::Shipment.new(
        id: 2,
        cost: 20,
        adjustments: [
          ::Spree::Adjustment.new(amount: -7)
        ]
      )
    end

    let(:shipment_with_existing_tax) do
      ::Spree::Shipment.new(
        id: 3,
        cost: 10,
        additional_tax_total: 3,
        adjustments: [
          ::Spree::Adjustment.new(
            amount: 3,
            source_type: "Spree::TaxRate"
          )
        ]
      )
    end

    let(:store) do
      ::Spree::Store.new(
        name: "Default Store",
        url: "https://store.example.com",
        code: "store",
        mail_from_address: "contact@example.com",
        cart_tax_country_iso: "US"
      )
    end

    context "when the order has an empty tax address" do
      let(:address) { nil }

      it "returns no taxes" do
        expect(subject.order_id).to eq order.id
        expect(subject.shipment_taxes).to be_empty
        expect(subject.line_item_taxes).to be_empty
      end
    end

    context "when the order has no line items" do
      let(:address) do
        ::Spree::Address.new(
          first_name: "Ronnie James",
          country: ::Spree::Country.new(iso: "US")
        )
      end

      let(:line_items) { [] }

      it "returns no taxes" do
        expect(subject.order_id).to eq order.id
        expect(subject.shipment_taxes).to be_empty
        expect(subject.line_item_taxes).to be_empty
      end
    end

    context "when the API encounters an error" do
      let(:address) do
        ::Spree::Address.new(
          first_name: "Ronnie James",
          country: ::Spree::Country.new(iso: "US")
        )
      end

      before do
        allow(dummy_api).to receive(:tax_for).with(order).and_raise("A bad thing happened.")
      end

      it "calls the configured error handler" do
        expect(SuperGood::SolidusTaxJar.exception_handler).to receive(:call) do |e|
          expect(e).to be_a StandardError
          expect(e.message).to eq "A bad thing happened."
        end

        subject
      end

      it "returns no taxes" do
        expect(subject.order_id).to eq order.id
        expect(subject.shipment_taxes).to be_empty
        expect(subject.line_item_taxes).to be_empty
      end
    end

    context "when the order has a non-empty tax address" do
      let(:address) do
        ::Spree::Address.new(
          first_name: "Ronnie James",
          country: ::Spree::Country.new(iso: "US")
        )
      end

      before do
        allow(dummy_api).to receive(:tax_for).with(order).and_return(
          instance_double(
            ::Taxjar::Tax,
            breakdown: breakdown
          )
        )
      end

      context "and there is tax" do
        let!(:tax_rate) do
          ::Spree::TaxRate.create!(
            name: "Sales Tax",
            amount: 0.5,
            calculator: ::Spree::Calculator.new
          )
        end

        let(:breakdown) do
          instance_double ::Taxjar::Breakdown,
            line_items: [taxjar_line_item],
            shipping?: !!shipping_tax_breakdown,
            shipping: shipping_tax_breakdown
        end

        let(:taxjar_line_item) do
          instance_double ::Taxjar::BreakdownLineItem, id: "33", tax_collectable: 6.66
        end

        let(:shipping_tax_breakdown) { nil }

        it "returns the taxes" do
          expect(subject.order_id).to eq order.id
          expect(subject.shipment_taxes).to be_empty
          expect(subject.line_item_taxes.length).to eq 1

          item_tax = subject.line_item_taxes.first
          aggregate_failures do
            expect(item_tax.item_id).to eq 33
            expect(item_tax.label).to eq "Sales Tax"
            expect(item_tax.tax_rate).to eq tax_rate
            expect(item_tax.amount).to eq 6.66
            expect(item_tax.included_in_price).to eq false
          end
        end

        context "but the taxable address check returns false" do
          before do
            allow(SuperGood::SolidusTaxJar.taxable_address_check)
              .to receive(:call).with(address)
              .and_return(false)
          end

          it "returns no taxes" do
            expect(subject.order_id).to eq order.id
            expect(subject.shipment_taxes).to be_empty
            expect(subject.line_item_taxes).to be_empty
          end
        end

        context "when there are shipping taxes" do
          let(:shipping_tax_breakdown) do
            instance_double ::Taxjar::Shipping, tax_collectable: 10.00
          end

          it "returns the shipping taxes" do
            shipment_taxes = subject.shipment_taxes
            expect(shipment_taxes.length).to eq 3

            aggregate_failures do
              expect(shipment_taxes[0].item_id).to eq 1
              expect(shipment_taxes[0].label).to eq "Sales Tax"
              expect(shipment_taxes[0].tax_rate).to eq tax_rate
              expect(shipment_taxes[0].amount).to eq 2.33
              expect(shipment_taxes[0].included_in_price).to eq false

              expect(shipment_taxes[1].item_id).to eq 2
              expect(shipment_taxes[1].label).to eq "Sales Tax"
              expect(shipment_taxes[1].tax_rate).to eq tax_rate
              expect(shipment_taxes[1].amount).to eq 4.33
              expect(shipment_taxes[1].included_in_price).to eq false

              expect(shipment_taxes[2].item_id).to eq 3
              expect(shipment_taxes[2].label).to eq "Sales Tax"
              expect(shipment_taxes[2].tax_rate).to eq tax_rate
              expect(shipment_taxes[2].amount).to eq 3.34
              expect(shipment_taxes[2].included_in_price).to eq false
            end
          end
        end

        context "when test_mode is set" do
          before { SuperGood::SolidusTaxJar.test_mode = true }
          after { SuperGood::SolidusTaxJar.test_mode = false }

          it "returns no taxes" do
            expect(subject.order_id).to eq order.id
            expect(subject.shipment_taxes).to be_empty
            expect(subject.line_item_taxes).to be_empty
          end
        end
      end

      context "and there is not a breakdown" do
        let(:breakdown) { nil }

        it "returns no taxes" do
          expect(subject.order_id).to eq order.id
          expect(subject.shipment_taxes).to be_empty
          expect(subject.line_item_taxes).to be_empty
        end
      end
    end
  end
end