Moves tests to shared examples file

This commit is contained in:
filipefurtad0
2023-10-09 18:10:35 +01:00
parent 8b249ee050
commit 8a1a14112b
2 changed files with 89 additions and 17 deletions

View File

@@ -0,0 +1,89 @@
# 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
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"
end
describe "detecting non-relevant" do
pending('A new invoice should not be generated upon order state change') do
it_behaves_like "attribute changes - order state: cancelled", false, "non-relevant"
end
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"
end
end
end
end

View File

@@ -18,29 +18,12 @@ describe OrderInvoiceComparator do
context "changes on the order object" do
describe "detecting relevant attribute changes" do
it "returns true if a relevant attribute changes" do
Spree::Order.where(id: order.id).update_all(payment_total: order.payment_total + 10)
order.reload
expect(subject).to be true
end
it "returns true if a relevant attribute changes" do
Spree::Order.where(id: order.id).update_all(total: order.total + 10)
order.reload
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,