Skip to content

Commit

Permalink
Merge pull request #5672 from mamhoff/adjustment-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen authored Feb 28, 2024
2 parents 928781f + 14531c0 commit ddabeef
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions core/spec/models/spree/adjustment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,107 @@
end
end
end

describe "#calculate_eligibility" do
subject { adjustment.calculate_eligibility }

around do |example|
Spree.deprecator.silence do
example.run
end
end

context "when the adjustment is not a promotion adjustment" do
let(:adjustment) { build(:adjustment, eligible: true, source: nil) }

it { is_expected.to eq true }
end
end

describe "#finalize" do
let(:adjustable) { create(:order) }
let(:adjustment) { build(:adjustment, finalized: false, adjustable: adjustable) }

subject { adjustment.finalize }

it "sets the adjustment as finalized" do
expect { subject }.to change { adjustment.finalized }.from(false).to(true)
end

it "persists the adjustment" do
expect { subject }.to change { adjustment.persisted? }.from(false).to(true)
end

context "for an invalid adjustment" do
let(:adjustment) { build(:adjustment, finalized: false, amount: nil, adjustable: adjustable) }

it "raises no error, returns false, does not persist the adjustment" do
expect { subject }.not_to change { adjustment.persisted? }.from(false)
expect(subject).to eq false
end
end
end

describe "#unfinalize" do
let(:adjustable) { create(:order) }
let(:adjustment) { build(:adjustment, finalized: true, adjustable: adjustable) }

subject { adjustment.unfinalize }

it "sets the adjustment as finalized" do
expect { subject }.to change { adjustment.finalized }.from(true).to(false)
end

it "persists the adjustment" do
expect { subject }.to change { adjustment.persisted? }.from(false).to(true)
end

context "for an invalid adjustment" do
let(:adjustment) { build(:adjustment, finalized: false, amount: nil, adjustable: adjustable) }

it "raises no error, returns false, does not persist the adjustment" do
expect { subject }.not_to change { adjustment.persisted? }.from(false)
expect(subject).to eq false
end
end
end

describe "#unfinalize!" do
let(:adjustable) { create(:order) }
let(:adjustment) { build(:adjustment, finalized: true, adjustable: adjustable) }

subject { adjustment.unfinalize! }

it "sets the adjustment as finalized" do
expect { subject }.to change { adjustment.finalized }.from(true).to(false)
end

it "persists the adjustment" do
expect { subject }.to change { adjustment.persisted? }.from(false).to(true)
end

context "for an invalid adjustment" do
let(:adjustment) { build(:adjustment, finalized: false, amount: nil, adjustable: adjustable) }

it "raises an error" do
expect { subject }.to raise_exception(ActiveRecord::RecordInvalid)
end
end
end

describe "#cancellation?" do
subject { adjustment.cancellation? }

context "when the adjustment is a cancellation" do
let(:adjustment) { build(:adjustment, source_type: "Spree::UnitCancel") }

it { is_expected.to eq true }
end

context "when the adjustment is not a cancellation" do
let(:adjustment) { build(:adjustment) }

it { is_expected.to eq false }
end
end
end

0 comments on commit ddabeef

Please sign in to comment.