Sha256: 4e006268bc161550b4bbedb94622a5f4c284265dd9d3bb6a28fe62d284babeec

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

require 'spec_helper'

describe Spree::Calculator::TieredPercent, type: :model do
  let(:calculator) { Spree::Calculator::TieredPercent.new }

  describe "#valid?" do
    subject { calculator.valid? }
    context "when base percent is less than zero" do
      before { calculator.preferred_base_percent = -1 }
      it { is_expected.to be false }
    end
    context "when base percent is greater than 100" do
      before { calculator.preferred_base_percent = 110 }
      it { is_expected.to be false }
    end
    context "when tiers is not a hash" do
      before { calculator.preferred_tiers = ["nope", 0] }
      it { is_expected.to be false }
    end
    context "when tiers is a hash" do
      context "and one of the keys is not a positive number" do
        before { calculator.preferred_tiers = { "nope" => 20 } }
        it { is_expected.to be false }
      end
      context "and one of the values is not a percent" do
        before { calculator.preferred_tiers = { 10 => 110 } }
        it { is_expected.to be false }
      end
    end
  end

  describe "#compute" do
    let(:line_item) { mock_model Spree::LineItem, amount: amount }
    before do
      calculator.preferred_base_percent = 10
      calculator.preferred_tiers = {
        100 => 15,
        200 => 20
      }
    end
    subject { calculator.compute(line_item) }
    context "when amount falls within the first tier" do
      let(:amount) { 50 }
      it { is_expected.to eq 5 }
    end
    context "when amount falls within the second tier" do
      let(:amount) { 150 }
      it { is_expected.to eq 22 }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spree_core-3.3.0.rc1 spec/models/spree/calculator/tiered_percent_spec.rb
spree_core-3.2.1 spec/models/spree/calculator/tiered_percent_spec.rb
spree_core-3.2.0 spec/models/spree/calculator/tiered_percent_spec.rb
spree_core-3.2.0.rc3 spec/models/spree/calculator/tiered_percent_spec.rb
spree_core-3.2.0.rc2 spec/models/spree/calculator/tiered_percent_spec.rb
spree_core-3.2.0.rc1 spec/models/spree/calculator/tiered_percent_spec.rb