Skip to content

Commit

Permalink
Fix non-realistic specs for updated tax code
Browse files Browse the repository at this point in the history
We had several specs that manually created tax adjustments rather than
setting up a real tax rate to be applied to the order.  This fixes
that.
  • Loading branch information
jordan-brough committed Sep 28, 2016
1 parent 3b7aa63 commit 1f20ce8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 36 deletions.
53 changes: 28 additions & 25 deletions backend/spec/features/admin/orders/adjustments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@
describe "Adjustments", type: :feature do
stub_authorization!

let!(:order) { create(:completed_order_with_totals, line_items_count: 5) }
let!(:line_item) do
line_item = order.line_items.first
# so we can be sure of a determinate price in our assertions
line_item.update_column(:price, 10)
line_item
let!(:ship_address) { create(:address) }
let!(:tax_zone) { create(:global_zone) } # will include the above address
let!(:tax_rate) { create(:tax_rate, amount: 0.20, zone: tax_zone, tax_category: tax_category) }

let!(:order) do
create(
:completed_order_with_totals,
line_items_attributes: [{ price: 10, variant: variant }] * 5,
ship_address: ship_address,
)
end
let!(:line_item) { order.line_items[0] }

let!(:tax_adjustment) do
create(:tax_adjustment,
adjustable: line_item,
finalized: true,
order: order,
label: "VAT 5%",
amount: 10)
end
let(:tax_category) { create(:tax_category) }
let(:variant) { create(:variant, tax_category: tax_category) }

let!(:adjustment) { order.adjustments.create!(order: order, label: 'Rebate', amount: 10) }

before(:each) do
# To ensure the order totals are correct
order.update_totals
order.persist_totals
order.update!

visit spree.admin_path
click_link "Orders"
Expand All @@ -35,9 +32,9 @@

context "admin managing adjustments" do
it "should display the correct values for existing order adjustments" do
within_row(1) do
expect(column_text(2)).to eq("VAT 5%")
expect(column_text(3)).to eq("$10.00")
within first('table tr', text: 'Tax') do
expect(column_text(2)).to match(/TaxCategory - \d+ 20\.000%/)
expect(column_text(3)).to eq("$2.00")
end
end

Expand Down Expand Up @@ -76,7 +73,9 @@

context "admin editing an adjustment" do
before(:each) do
within_row(2) { click_icon :edit }
within('table tr', text: 'Rebate') do
click_icon :edit
end
end

context "successfully" do
Expand Down Expand Up @@ -106,15 +105,19 @@
end

context "deleting an adjustment" do
it "should not be possible if adjustment is closed" do
within_row(1) do
expect(page).not_to have_css('.fa-trash')
context 'when the adjustment is finalized' do
let!(:adjustment) { super().tap(&:finalize!) }

it 'should not be possible' do
within('table tr', text: 'Rebate') do
expect(page).not_to have_css('.fa-trash')
end
end
end

it "should update the total", js: true do
accept_alert do
within_row(2) do
within('table tr', text: 'Rebate') do
click_icon(:trash)
end
end
Expand Down
4 changes: 1 addition & 3 deletions core/spec/models/spree/order/updating_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require 'spec_helper'

describe Spree::Order, type: :model do
let(:order) { stub_model(Spree::Order) }
let(:order) { create(:order) }

context "#update!" do
let(:line_items) { [mock_model(Spree::LineItem, amount: 5)] }

context "when there are update hooks" do
before { Spree::Order.register_update_hook :foo }
after { Spree::Order.update_hooks.clear }
Expand Down
10 changes: 6 additions & 4 deletions core/spec/models/spree/order_cancellations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,19 @@
let(:line_item) { order.line_items.to_a.first }
let(:inventory_unit_1) { line_item.inventory_units[0] }
let(:inventory_unit_2) { line_item.inventory_units[1] }
let(:promotion) { create(:promotion, :with_line_item_adjustment) }
let(:promotion_action) { promotion.actions[0] }

before do
order.contents.add(line_item.variant)

# make the total $1.67 so it divides unevenly
line_item.adjustments.create!(
source_type: 'Spree::TaxRate',
order: order,
amount: 0.01,
label: 'some fake tax',
finalized: true
label: 'some promo',
source: promotion_action,
finalized: true,
)
order.update!
end
Expand All @@ -154,7 +156,7 @@
order.cancellations.short_ship([inventory_unit_2])
expect(line_item.adjustments.map(&:amount)).to match_array(
[
0.01, # tax adjustment
0.01, # promo adjustment
-0.84, # short ship 1
-0.83, # short ship 2
]
Expand Down
1 change: 0 additions & 1 deletion core/spec/models/spree/order_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ def create_adjustment(label, amount)
let(:order) do
create(
:order_with_line_items,
line_items_count: 1,
line_items_attributes: [{ price: 10, variant: variant }],
ship_address: ship_address,
)
Expand Down
19 changes: 16 additions & 3 deletions core/spec/models/spree/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,23 @@
end

context "#item_cost" do
let(:shipment) { order.shipments[0] }

let(:order) do
create(
:order_ready_to_ship,
line_items_attributes: [{ price: 10, variant: variant }],
ship_address: ship_address,
)
end

let!(:ship_address) { create(:address) }
let!(:tax_zone) { create(:global_zone) } # will include the above address
let!(:tax_rate) { create(:tax_rate, amount: 0.1, zone: tax_zone, tax_category: tax_category) }
let(:tax_category) { create(:tax_category) }
let(:variant) { create(:variant, tax_category: tax_category) }

it 'should equal line items final amount with tax' do
shipment = create(:shipment, order: create(:order_with_totals))
create :tax_adjustment, adjustable: shipment.order.line_items.first, order: shipment.order
shipment.order.update!
expect(shipment.item_cost).to eql(11.0)
end
end
Expand Down

0 comments on commit 1f20ce8

Please sign in to comment.