From bb0cf65ecceedb92d4e0a6f4107883105ba6fded Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 31 Mar 2021 13:20:34 +0100 Subject: [PATCH] Handle OrderBalance comparison operators These operators get used on the object returned by order_balance in a few places, and were not working correctly. --- app/models/order_balance.rb | 2 +- spec/models/order_balance_spec.rb | 38 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/app/models/order_balance.rb b/app/models/order_balance.rb index 5dea2a1cea..4b7f27d2f8 100644 --- a/app/models/order_balance.rb +++ b/app/models/order_balance.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class OrderBalance - delegate :zero?, :abs, :to_s, :to_f, :to_d, to: :amount + delegate :zero?, :abs, :to_s, :to_f, :to_d, :<, :>, to: :amount def initialize(order) @order = order diff --git a/spec/models/order_balance_spec.rb b/spec/models/order_balance_spec.rb index ecff1cbc2f..f024969074 100644 --- a/spec/models/order_balance_spec.rb +++ b/spec/models/order_balance_spec.rb @@ -392,4 +392,42 @@ describe OrderBalance do end end end + + context "with comparison operators" do + context 'when the customer_balance feature is disabled' do + before do + allow(OpenFoodNetwork::FeatureToggle) + .to receive(:enabled?).with(:customer_balance, user) { false } + end + + before do + allow(order).to receive(:old_outstanding_balance) { 10 } + end + + it 'correctly returns true or false' do + expect(order_balance > 5).to eq true + expect(order_balance > 20).to eq false + expect(order_balance < 15).to eq true + expect(order_balance < 5).to eq false + end + end + + context 'when the customer_balance feature is enabled' do + before do + allow(OpenFoodNetwork::FeatureToggle) + .to receive(:enabled?).with(:customer_balance, user) { true } + end + + before do + allow(order).to receive(:new_outstanding_balance) { 10 } + end + + it 'correctly returns true or false' do + expect(order_balance > 5).to eq true + expect(order_balance > 20).to eq false + expect(order_balance < 15).to eq true + expect(order_balance < 5).to eq false + end + end + end end