Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage for Spree::Adjustment to 100% #5672

Merged
merged 5 commits into from
Feb 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading