Skip to content

Commit

Permalink
Per review, clean up voucher specs
Browse files Browse the repository at this point in the history
Add explicit 'order.item_total' to make specs more readable
  • Loading branch information
rioug committed Aug 11, 2023
1 parent 77a15b1 commit e9aa1da
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions spec/factories/voucher_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

FactoryBot.define do
factory :voucher, class: Voucher do
code { "new_code" }
enterprise { build(:distributor_enterprise) }
amount { 10 }
end
Expand Down
20 changes: 13 additions & 7 deletions spec/models/vouchers/flat_rate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
require 'spec_helper'

describe Vouchers::FlatRate do
let(:enterprise) { build(:enterprise) }

describe 'validations' do
subject { build(:voucher_flat_rate, code: 'new_code', enterprise:) }
subject { build(:voucher_flat_rate) }

it { is_expected.to validate_presence_of(:amount) }
it { is_expected.to validate_numericality_of(:amount).is_greater_than(0) }
Expand All @@ -15,29 +13,37 @@
describe '#compute_amount' do
let(:order) { create(:order_with_totals) }

before do
order.update_columns(item_total: 15)
end

context 'when order total is more than the voucher' do
subject { create(:voucher_flat_rate, code: 'new_code', enterprise:, amount: 5) }
subject { create(:voucher_flat_rate, amount: 5) }

it 'uses the voucher total' do
expect(subject.compute_amount(order).to_f).to eq(-5)
end
end

context 'when order total is less than the voucher' do
subject { create(:voucher_flat_rate, code: 'new_code', enterprise:, amount: 20) }
subject { create(:voucher_flat_rate, amount: 20) }

it 'matches the order total' do
expect(subject.compute_amount(order).to_f).to eq(-10)
expect(subject.compute_amount(order).to_f).to eq(-15)
end
end
end

describe "#rate" do
subject do
create(:voucher_flat_rate, code: 'new_code', enterprise:, amount: 5)
create(:voucher_flat_rate, code: 'new_code', amount: 5)
end
let(:order) { create(:order_with_totals) }

before do
order.update_columns(item_total: 10)
end

it "returns the voucher rate" do
# rate = -voucher_amount / order.pre_discount_total
# -5 / 10 = -0.5
Expand Down
14 changes: 8 additions & 6 deletions spec/models/vouchers/percentage_rate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
require 'spec_helper'

describe Vouchers::PercentageRate do
let(:enterprise) { build(:enterprise) }

describe 'validations' do
subject { build(:voucher_percentage_rate, code: 'new_code', enterprise:) }
subject { build(:voucher_percentage_rate) }

it { is_expected.to validate_presence_of(:amount) }
it do
Expand All @@ -18,18 +16,22 @@

describe '#compute_amount' do
subject do
create(:voucher_percentage_rate, code: 'new_code', enterprise:, amount: 10)
create(:voucher_percentage_rate, amount: 10)
end
let(:order) { create(:order_with_totals) }

before do
order.update_columns(item_total: 15)
end

it 'returns percentage of the order total' do
expect(subject.compute_amount(order)).to eq(-order.pre_discount_total * 0.1)
expect(subject.compute_amount(order)).to eq(-1.5)
end
end

describe "#rate" do
subject do
create(:voucher_percentage_rate, code: 'new_code', enterprise:, amount: 50)
create(:voucher_percentage_rate, amount: 50)
end

it "returns the voucher percentage rate" do
Expand Down

0 comments on commit e9aa1da

Please sign in to comment.