Skip to content

Commit

Permalink
Moves tests to shared examples file
Browse files Browse the repository at this point in the history
  • Loading branch information
filipefurtad0 committed Oct 9, 2023
1 parent fec2ca2 commit b9bcaa0
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
90 changes: 90 additions & 0 deletions spec/services/order_invoice_comparator_shared_examples_spec.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 0 additions & 11 deletions spec/services/order_invoice_comparator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b9bcaa0

Please sign in to comment.