summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorandrea longhi <andrea@spaghetticode.it>2020-01-13 20:31:47 +0100
committerandrea longhi <andrea@spaghetticode.it>2020-01-14 11:10:04 +0100
commitc7bcc30f9aa142694e84195063a6093f428950b4 (patch)
treef887729c05e9868b32b9a2c1915d40d13412fdfe /spec
parent044bb66e25a440388431de27c67d437fe6cebfb6 (diff)
Add tax rate calculator class
This new calculator allows to retrieve tax rate information starting from a `Spree::Address` model. It shares some logic with the tax calculator, for example the ability to handle exceptions using `SolidusTaxJar.exception_handler` lambda.
Diffstat (limited to 'spec')
-rw-r--r--spec/super_good/solidus_taxjar/tax_rate_calculator_spec.rb97
1 files changed, 97 insertions, 0 deletions
diff --git a/spec/super_good/solidus_taxjar/tax_rate_calculator_spec.rb b/spec/super_good/solidus_taxjar/tax_rate_calculator_spec.rb
new file mode 100644
index 0000000..9a4e2b8
--- /dev/null
+++ b/spec/super_good/solidus_taxjar/tax_rate_calculator_spec.rb
@@ -0,0 +1,97 @@
+require 'spec_helper'
+
+RSpec.describe ::SuperGood::SolidusTaxJar::TaxRateCalculator do
+ describe "#calculate" do
+ subject { calculator.calculate }
+
+ let(:calculator) { described_class.new(address, api: dummy_api) }
+
+ let(:dummy_api) do
+ instance_double ::SuperGood::SolidusTaxJar::API
+ end
+
+ let(:dummy_tax_rate) { BigDecimal(0) }
+
+ let(:incomplete_address) do
+ ::Spree::Address.new(
+ first_name: "Ronnie James",
+ zipcode: nil,
+ address1: nil,
+ city: "Beverly Hills",
+ state_name: "California",
+ country: ::Spree::Country.new(iso: "US")
+ )
+ end
+
+ let(:complete_address) do
+ incomplete_address.tap do |address|
+ address.zipcode = "90210"
+ address.address1 = "9900 Wilshire Blvd"
+ end
+ end
+
+ shared_examples "returns the dummy tax rate" do
+ it { expect(subject).to eq dummy_tax_rate }
+ end
+
+ context "when the address is not complete" do
+ let(:address) { incomplete_address }
+
+ it_behaves_like "returns the dummy tax rate"
+ end
+
+ context "when the address is complete" do
+ let(:address) { complete_address }
+
+ context "when the address is not taxable" do
+ before do
+ allow(SuperGood::SolidusTaxJar.taxable_address_check)
+ .to receive(:call).with(address)
+ .and_return(false)
+ end
+
+ it_behaves_like "returns the dummy tax rate"
+ end
+
+ context "when the address is taxable" do
+ let(:tax_rate) { 0.03 }
+
+ before do
+ allow(dummy_api).to receive(:tax_rate_for) { tax_rate }
+ end
+
+ it "returns the expected tax rate" do
+ expect(subject).to eq tax_rate
+ end
+ end
+ end
+
+ context "when the API encounters an error" do
+ let(:address) { complete_address }
+
+ before do
+ allow(dummy_api).to receive(:tax_rate_for).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_behaves_like "returns the dummy tax rate"
+ end
+
+ context "when test_mode is set" do
+ let(:address) { complete_address }
+
+ before { SuperGood::SolidusTaxJar.test_mode = true }
+ after { SuperGood::SolidusTaxJar.test_mode = false }
+
+ it_behaves_like "returns the dummy tax rate"
+ end
+ end
+end