Skip to content

Commit

Permalink
Update order factory to respect supplied completed_at timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
DianeLooney committed Sep 14, 2021
1 parent 8436b81 commit cf9eca3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
10 changes: 7 additions & 3 deletions core/lib/spree/testing_support/factories/order_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@

factory :completed_order_with_promotion do
transient do
completed_at { Time.current }
promotion { nil }
end

Expand All @@ -79,7 +80,7 @@
order.order_promotions.create!(promotion: promotion, promotion_code: promotion_code)

# Complete the order after the promotion has been activated
order.update_column(:completed_at, Time.current)
order.update_column(:completed_at, evaluator.completed_at)
order.update_column(:state, "complete")
end
end
Expand All @@ -104,13 +105,16 @@
end

factory :completed_order_with_totals do
transient do
completed_at { Time.current }
end
state { 'complete' }

after(:create) do |order|
after(:create) do |order, evaluator|
order.shipments.each do |shipment|
shipment.inventory_units.update_all state: 'on_hand', pending: false
end
order.update_column(:completed_at, Time.current)
order.update_column(:completed_at, evaluator.completed_at)
end

factory :completed_order_with_pending_payment do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@
end
end
end

RSpec.shared_examples 'supplied completed_at is respected' do
context 'when passed a completed_at timestamp' do
let(:completed_at) { 2.days.ago }
let(:order) { create(factory, completed_at: completed_at) }

it 'respects the timestamp' do
expect(order.completed_at).to be_within(1.second).of(completed_at)
end
end

context 'when no completed_at timestamp is passed' do
let(:order) { create(factory) }

it 'defaults to the current time' do
expect(order.completed_at).to be_within(1.second).of(Time.current)
end
end
end

RSpec.describe 'order factory' do
let(:factory_class) { Spree::Order }

Expand Down Expand Up @@ -200,6 +220,7 @@
it_behaves_like 'a working factory'
it_behaves_like 'an order with line items factory', "complete", "on_hand"
it_behaves_like 'shipping methods are assigned'
it_behaves_like 'supplied completed_at is respected'

it "has the expected attributes" do
order = create(factory)
Expand Down Expand Up @@ -255,6 +276,7 @@
it_behaves_like 'a working factory'
it_behaves_like 'an order with line items factory', "complete", "on_hand"
it_behaves_like 'shipping methods are assigned'
it_behaves_like 'supplied completed_at is respected'

it "has the expected attributes" do
order = create(factory)
Expand All @@ -278,6 +300,7 @@
it_behaves_like 'a working factory'
it_behaves_like 'an order with line items factory', "complete", "on_hand"
it_behaves_like 'shipping methods are assigned'
it_behaves_like 'supplied completed_at is respected'

it "has the expected attributes" do
order = create(factory)
Expand All @@ -304,6 +327,7 @@
it_behaves_like 'a working factory'
it_behaves_like 'an order with line items factory', "complete", "on_hand"
it_behaves_like 'shipping methods are assigned'
it_behaves_like 'supplied completed_at is respected'

it "has the expected attributes" do
order = create(factory)
Expand Down Expand Up @@ -345,6 +369,7 @@
it_behaves_like 'a working factory'
it_behaves_like 'an order with line items factory', "complete", "shipped"
it_behaves_like 'shipping methods are assigned'
it_behaves_like 'supplied completed_at is respected'

it "has the expected attributes" do
order = create(factory)
Expand Down

0 comments on commit cf9eca3

Please sign in to comment.