From b9bcaa05481befa053ad9c0c6e451cdbcb1f701a Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Mon, 9 Oct 2023 18:10:35 +0100 Subject: [PATCH] Moves tests to shared examples file --- ...invoice_comparator_shared_examples_spec.rb | 90 +++++++++++++++++++ .../services/order_invoice_comparator_spec.rb | 11 --- 2 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 spec/services/order_invoice_comparator_shared_examples_spec.rb diff --git a/spec/services/order_invoice_comparator_shared_examples_spec.rb b/spec/services/order_invoice_comparator_shared_examples_spec.rb new file mode 100644 index 000000000000..cfd742f4997a --- /dev/null +++ b/spec/services/order_invoice_comparator_shared_examples_spec.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +require 'spec_helper' + +shared_examples "attribute changes - payment total" do |boolean, type| + before do + Spree::Order.where(id: order.id).update_all(payment_total: order.payment_total + 10) + end + + it "returns #{boolean} if a #{type} attribute changes" do + order.reload + expect(subject).to be boolean + end +end + +shared_examples "attribute changes - order total" do |boolean, type| + before do + Spree::Order.where(id: order.id).update_all(total: order.total + 10) + end + + it "returns #{boolean} if a #{type} attribute changes" do + order.reload + expect(subject).to be boolean + end +end + +shared_examples "attribute changes - order state: cancelled" do |boolean, type| + before do + order.cancel! + end + + it "returns #{boolean} if a #{type} attribute changes" do + pending("A new invoice should not be generated upon order state change") + order.reload + expect(subject).to be boolean + end +end + +describe OrderInvoiceComparator do + describe '#can_generate_new_invoice?' do + # this passes 'order' as argument to the invoice comparator + let(:order) { create(:completed_order_with_fees) } + let!(:invoice_data_generator){ InvoiceDataGenerator.new(order) } + let!(:invoice){ + create(:invoice, + order:, + data: invoice_data_generator.serialize_for_invoice) + } + let(:subject) { + OrderInvoiceComparator.new(order).can_generate_new_invoice? + } + + context "changes on the order object" do + describe "detecting relevant" do + it_behaves_like "attribute changes - payment total", true, "relevant" + it_behaves_like "attribute changes - order total", true, "relevant" + it_behaves_like "attribute changes - order state: cancelled", true, "relevant" + end + + describe "detecting non-relevant" do + it_behaves_like "attribute changes - order state: cancelled", false, "non-relevant" + end + end + end + + describe '#can_update_latest_invoice?' do + let!(:order) { create(:completed_order_with_fees) } + let!(:invoice_data_generator){ InvoiceDataGenerator.new(order) } + let!(:invoice){ + create(:invoice, + order:, + data: invoice_data_generator.serialize_for_invoice) + } + let(:subject) { + OrderInvoiceComparator.new(order).can_update_latest_invoice? + } + + context "changes on the order object" do + describe "detecting relevant" do + it_behaves_like "attribute changes - order state: cancelled", true, "relevant" + end + + describe "detecting non-relevant" do + it_behaves_like "attribute changes - payment total", false, "non-relevant" + it_behaves_like "attribute changes - order total", false, "non-relevant" + it_behaves_like "attribute changes - order state: cancelled", true, "non-relevant" + end + end + end +end diff --git a/spec/services/order_invoice_comparator_spec.rb b/spec/services/order_invoice_comparator_spec.rb index c3cd679179a2..48a76c14122c 100644 --- a/spec/services/order_invoice_comparator_spec.rb +++ b/spec/services/order_invoice_comparator_spec.rb @@ -30,17 +30,6 @@ expect(subject).to be true end - it "returns true if a relevant attribute changes - order state: cancelled" do - order.cancel! - expect(subject).to be true - end - - it "returns true if a relevant attribute changes - order state: resumed" do - order.cancel! - order.resume! - expect(subject).to be true - end - context "additional tax total changes" do let(:order) do create(:order_with_taxes, product_price: 110, tax_rate_amount: 0.1,