diff --git a/spec/factories/order_factory.rb b/spec/factories/order_factory.rb index 04885c0965..aec931b752 100644 --- a/spec/factories/order_factory.rb +++ b/spec/factories/order_factory.rb @@ -9,7 +9,7 @@ FactoryBot.define do user bill_address completed_at { nil } - email { user.email } + email { user&.email || customer.email } factory :order_with_totals do after(:create) do |order| diff --git a/spec/services/customers_with_balance_spec.rb b/spec/services/customers_with_balance_spec.rb index 687ab5900a..c0b5a9bb00 100644 --- a/spec/services/customers_with_balance_spec.rb +++ b/spec/services/customers_with_balance_spec.rb @@ -12,14 +12,28 @@ describe CustomersWithBalance do let(:total) { 200.00 } let(:order_total) { 100.00 } - before do - create(:order, customer: customer, total: order_total, payment_total: 0) - create(:order, customer: customer, total: order_total, payment_total: 0) + context 'when non-guest order' do + before do + create(:order, customer: customer, total: order_total, payment_total: 0) + create(:order, customer: customer, total: order_total, payment_total: 0) + end + + it 'returns the customer balance' do + customer = customers_with_balance.query.first + expect(customer.balance_value).to eq(-total) + end end - it 'returns the customer balance' do - customer = customers_with_balance.query.first - expect(customer.balance_value).to eq(-total) + context 'when guest order' do + before do + create(:order, customer: customer, user: nil, total: order_total, payment_total: 0) + create(:order, customer: customer, user: nil, total: order_total, payment_total: 0) + end + + it 'returns the customer balance' do + customer = customers_with_balance.query.first + expect(customer.balance_value).to eq(-total) + end end end @@ -28,14 +42,34 @@ describe CustomersWithBalance do let(:order_total) { 100.00 } let(:payment_total) { order_total } - before do - create(:order, customer: customer, total: order_total, payment_total: 0) - create(:order, customer: customer, total: order_total, payment_total: payment_total) + context 'when non-guest order' do + before do + create(:order, customer: customer, total: order_total, payment_total: 0) + create(:order, customer: customer, total: order_total, payment_total: payment_total) + end + + it 'returns the customer balance' do + customer = customers_with_balance.query.first + expect(customer.balance_value).to eq(payment_total - total) + end end - it 'returns the customer balance' do - customer = customers_with_balance.query.first - expect(customer.balance_value).to eq(payment_total - total) + context 'when guest order' do + before do + create(:order, customer: customer, user: nil, total: order_total, payment_total: 0) + create( + :order, + customer: customer, + user: nil, + total: order_total, + payment_total: payment_total + ) + end + + it 'returns the customer balance' do + customer = customers_with_balance.query.first + expect(customer.balance_value).to eq(payment_total - total) + end end end @@ -45,17 +79,44 @@ describe CustomersWithBalance do let(:payment_total) { 100.00 } let(:complete_orders_total) { order_total } - before do - create(:order, customer: customer, total: order_total, payment_total: 0) - create( - :order, - customer: customer, - total: order_total, - payment_total: order_total, - state: 'canceled' - ) + context 'when non-guest order' do + before do + create(:order, customer: customer, total: order_total, payment_total: 0) + create( + :order, + customer: customer, + total: order_total, + payment_total: order_total, + state: 'canceled' + ) + end + + it 'returns the customer balance' do + customer = customers_with_balance.query.first + expect(customer.balance_value).to eq(payment_total - complete_orders_total) + end end + context 'when guest order' do + before do + create(:order, customer: customer, user: nil, total: order_total, payment_total: 0) + create( + :order, + customer: customer, + user: nil, + total: order_total, + payment_total: order_total, + state: 'canceled' + ) + end + + it 'returns the customer balance' do + customer = customers_with_balance.query.first + expect(customer.balance_value).to eq(payment_total - complete_orders_total) + end + end + end + it 'returns the customer balance' do customer = customers_with_balance.query.first expect(customer.balance_value).to eq(payment_total - complete_orders_total)