-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
exchange_spec.rb
74 lines (61 loc) 路 2.7 KB
/
exchange_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'spec_helper'
module Spree
describe Exchange, type: :model do
let(:order) { Spree::Order.new }
let(:return_item_1) { build(:exchange_return_item) }
let(:return_item_2) { build(:exchange_return_item) }
let(:return_items) { [return_item_1, return_item_2] }
let(:exchange) { Exchange.new(order, return_items) }
describe "#description" do
before do
allow(return_item_1).to receive(:variant) { double(options_text: "foo") }
allow(return_item_1).to receive(:exchange_variant) { double(options_text: "bar") }
allow(return_item_2).to receive(:variant) { double(options_text: "baz") }
allow(return_item_2).to receive(:exchange_variant) { double(options_text: "qux") }
end
it "describes the return items' change in options" do
expect(exchange.description).to match /foo => bar/
expect(exchange.description).to match /baz => qux/
end
end
describe "#display_amount" do
it "is the total amount of all return items" do
expect(exchange.display_amount).to eq Spree::Money.new(0.0)
end
end
describe "#perform!" do
let(:return_item) { create(:exchange_return_item, inventory_unit: inventory_unit) }
let(:return_items) { [return_item] }
let(:inventory_unit) { create(:inventory_unit, order: order, line_item: line_item) }
let(:order) { create(:shipped_order, line_items_count: 1) }
let(:line_item) { order.line_items.first }
subject { exchange.perform! }
before { return_item.exchange_variant.stock_items.first.adjust_count_on_hand(20) }
it "creates shipments for the order with the return items exchange inventory units" do
expect { subject }.to change { order.shipments.count }.by(1)
new_shipment = order.shipments.last
expect(new_shipment).to be_ready
new_inventory_units = new_shipment.inventory_units
expect(new_inventory_units.count).to eq 1
expect(new_inventory_units.first.original_return_item).to eq return_item
expect(new_inventory_units.first.line_item).to eq return_item.inventory_unit.line_item
end
context "when it cannot create shipments for all items" do
before do
StockItem.where(variant_id: return_item.exchange_variant_id).destroy_all
end
it 'raises an UnableToCreateShipments error' do
expect {
subject
}.to raise_error(Spree::Exchange::UnableToCreateShipments)
end
end
end
describe "#to_key" do # for dom_id
it { expect(Exchange.new(nil, nil).to_key).to be_nil }
end
describe ".param_key" do # for dom_id
it { expect(Exchange.param_key).to eq "spree_exchange" }
end
end
end