From ae8f14a6719e5c4a2f994eb10a51eed6e126ae0f Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 26 May 2022 16:52:31 +1000 Subject: [PATCH] Avoid style warnings in spec and reduce indent Defining a spec within a module is not necessary. It's better to use the standard describe syntax. --- ...order_cycle_customer_totals_report_spec.rb | 222 +++++++++--------- 1 file changed, 108 insertions(+), 114 deletions(-) diff --git a/spec/lib/reports/orders_and_fulfillment/order_cycle_customer_totals_report_spec.rb b/spec/lib/reports/orders_and_fulfillment/order_cycle_customer_totals_report_spec.rb index 8472a8ae28..7c93e6cfbe 100644 --- a/spec/lib/reports/orders_and_fulfillment/order_cycle_customer_totals_report_spec.rb +++ b/spec/lib/reports/orders_and_fulfillment/order_cycle_customer_totals_report_spec.rb @@ -2,136 +2,130 @@ require "spec_helper" -module Reporting - module Reports - module OrdersAndFulfillment - describe OrderCycleCustomerTotals do - let!(:distributor) { create(:distributor_enterprise, name: "Apple Market") } - let!(:customer) { create(:customer, enterprise: distributor, user: user, code: "JHN") } - let(:user) { create(:user, email: "john@example.net") } - let(:current_user) { distributor.owner } - let(:params) { { display_summary_row: true } } - let(:report) { OrderCycleCustomerTotals.new(current_user, params) } +describe Reporting::Reports::OrdersAndFulfillment::OrderCycleCustomerTotals do + let!(:distributor) { create(:distributor_enterprise, name: "Apple Market") } + let!(:customer) { create(:customer, enterprise: distributor, user: user, code: "JHN") } + let(:user) { create(:user, email: "john@example.net") } + let(:current_user) { distributor.owner } + let(:params) { { display_summary_row: true } } + let(:report) { described_class.new(current_user, params) } - let(:report_table) do - report.table_rows - end + let(:report_table) do + report.table_rows + end - context "viewing the report" do - let!(:order) do - create( - :completed_order_with_totals, - number: "R644360121", - line_items_count: 1, - user: customer.user, - customer: customer, - distributor: distributor, - completed_at: Date.parse("2022-05-26"), - ).tap do |order| - order.line_items[0].product.supplier.update(name: "Apple Farmer") - order.line_items[0].product.update(name: "Apples") - order.line_items[0].variant.update(sku: "APP") - end - end - let(:comparison_report) do - File.read(Rails.root.join(report_file_name)) - end - let(:report_file_name) do - "spec/fixtures/reports/orders_and_fulfillment/order_cycle_customer_totals_report.csv" - end + context "viewing the report" do + let!(:order) do + create( + :completed_order_with_totals, + number: "R644360121", + line_items_count: 1, + user: customer.user, + customer: customer, + distributor: distributor, + completed_at: Date.parse("2022-05-26"), + ).tap do |order| + order.line_items[0].product.supplier.update(name: "Apple Farmer") + order.line_items[0].product.update(name: "Apples") + order.line_items[0].variant.update(sku: "APP") + end + end + let(:comparison_report) do + File.read(Rails.root.join(report_file_name)) + end + let(:report_file_name) do + "spec/fixtures/reports/orders_and_fulfillment/order_cycle_customer_totals_report.csv" + end - it "generates the report" do - expect(report_table.length).to eq(2) + it "generates the report" do + expect(report_table.length).to eq(2) - expect(report.render_as(:csv)).to eq comparison_report - end + expect(report.render_as(:csv)).to eq comparison_report + end - it "has a line item row" do - distributor_name_field = report_table.first[0] - expect(distributor_name_field).to eq distributor.name + it "has a line item row" do + distributor_name_field = report_table.first[0] + expect(distributor_name_field).to eq distributor.name - customer_name_field = report_table.first[1] - expect(customer_name_field).to eq order.bill_address.full_name - end + customer_name_field = report_table.first[1] + expect(customer_name_field).to eq order.bill_address.full_name + end - it 'includes the order number and date in item rows' do - expect(report.rows.first.order_number).to eq order.number - expect(report.rows.first.date).to eq order.completed_at.strftime("%F %T") - end + it 'includes the order number and date in item rows' do + expect(report.rows.first.order_number).to eq order.number + expect(report.rows.first.date).to eq order.completed_at.strftime("%F %T") + end - it 'includes the summary row' do - expect(report.rows.second.quantity).to eq "TOTAL" - expect(report.rows.second.date).to eq order.completed_at.strftime("%F %T") - end - end + it 'includes the summary row' do + expect(report.rows.second.quantity).to eq "TOTAL" + expect(report.rows.second.date).to eq order.completed_at.strftime("%F %T") + end + end - context "loading shipping methods" do - let!(:shipping_method1) { - create(:shipping_method, distributors: [distributor], name: "First") - } - let!(:shipping_method2) { - create(:shipping_method, distributors: [distributor], name: "Second") - } - let!(:shipping_method3) { - create(:shipping_method, distributors: [distributor], name: "Third") - } - let!(:order) do - create(:completed_order_with_totals, line_items_count: 1, user: customer.user, - customer: customer, distributor: distributor) - end - - before do - order.shipments.each(&:refresh_rates) - order.select_shipping_method(shipping_method2.id) - end - - it "displays the correct shipping_method" do - expect(report.rows.first.shipping).to eq shipping_method2.name - end - end - - context "displaying payment fees" do - context "with both failed and completed payments present" do - let!(:order) { - create(:order_ready_to_ship, user: customer.user, + context "loading shipping methods" do + let!(:shipping_method1) { + create(:shipping_method, distributors: [distributor], name: "First") + } + let!(:shipping_method2) { + create(:shipping_method, distributors: [distributor], name: "Second") + } + let!(:shipping_method3) { + create(:shipping_method, distributors: [distributor], name: "Third") + } + let!(:order) do + create(:completed_order_with_totals, line_items_count: 1, user: customer.user, customer: customer, distributor: distributor) - } - let(:completed_payment) { order.payments.completed.first } - let!(:failed_payment) { create(:payment, order: order, state: "failed") } + end - before do - completed_payment.adjustment.update amount: 123.00 - failed_payment.adjustment.update amount: 456.00, eligible: false, state: "finalized" - end + before do + order.shipments.each(&:refresh_rates) + order.select_shipping_method(shipping_method2.id) + end - it "shows the correct payment fee amount for the order" do - allow(report).to receive(:raw_render?).and_return(true) - expect(report.rows.last.pay_fee_price).to eq completed_payment.adjustment.amount - end - end - end + it "displays the correct shipping_method" do + expect(report.rows.first.shipping).to eq shipping_method2.name + end + end - context 'when a variant override applies' do - let!(:order) do - create(:completed_order_with_totals, line_items_count: 1, user: customer.user, - customer: customer, distributor: distributor) - end - let(:overidden_sku) { 'magical_sku' } + context "displaying payment fees" do + context "with both failed and completed payments present" do + let!(:order) { + create(:order_ready_to_ship, user: customer.user, + customer: customer, distributor: distributor) + } + let(:completed_payment) { order.payments.completed.first } + let!(:failed_payment) { create(:payment, order: order, state: "failed") } - before do - create( - :variant_override, - hub: distributor, - variant: order.line_items.first.variant, - sku: overidden_sku - ) - end + before do + completed_payment.adjustment.update amount: 123.00 + failed_payment.adjustment.update amount: 456.00, eligible: false, state: "finalized" + end - it 'uses the sku from the variant override' do - expect(report.rows.first.sku).to eq overidden_sku - end - end + it "shows the correct payment fee amount for the order" do + allow(report).to receive(:raw_render?).and_return(true) + expect(report.rows.last.pay_fee_price).to eq completed_payment.adjustment.amount end end end + + context 'when a variant override applies' do + let!(:order) do + create(:completed_order_with_totals, line_items_count: 1, user: customer.user, + customer: customer, distributor: distributor) + end + let(:overidden_sku) { 'magical_sku' } + + before do + create( + :variant_override, + hub: distributor, + variant: order.line_items.first.variant, + sku: overidden_sku + ) + end + + it 'uses the sku from the variant override' do + expect(report.rows.first.sku).to eq overidden_sku + end + end end