From 9c7086702d25a51c67aabbe52076b786ff20ca40 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Thu, 10 Oct 2024 05:54:06 +0500 Subject: [PATCH] update specs to have more line items --- .../admin/reports/pay_your_suppliers_spec.rb | 131 +++++++++++++----- 1 file changed, 95 insertions(+), 36 deletions(-) diff --git a/spec/system/admin/reports/pay_your_suppliers_spec.rb b/spec/system/admin/reports/pay_your_suppliers_spec.rb index 8bc1760c9bc..a7b7ac99667 100644 --- a/spec/system/admin/reports/pay_your_suppliers_spec.rb +++ b/spec/system/admin/reports/pay_your_suppliers_spec.rb @@ -5,21 +5,34 @@ RSpec.describe "Pay Your Suppliers Report" do include ReportsHelper - let(:hub) { create(:distributor_enterprise) } - let(:order_cycle) { create(:open_order_cycle, distributors: [hub]) } - let(:product) { order.products.first } - let(:variant) { product.variants.first } - let(:supplier) { variant.supplier } - let(:current_user) { hub.owner } - let!(:order) do - create(:completed_order_with_totals, distributor: hub, order_cycle:, line_items_count: 1) + let(:owner) { create(:user) } + let(:hub1) { create(:enterprise, owner:) } + let(:order_cycle1) { create(:open_order_cycle, distributors: [hub1]) } + let!(:order1) do + create( + :completed_order_with_totals, + distributor: hub1, + order_cycle: order_cycle1, + line_items_count: 2 + ) + end + + let(:hub2) { create(:enterprise, owner:) } + let(:product2) { order1.products.first } + let(:variant2) { product2.variants.first } + let(:supplier2) { variant2.supplier } + let(:order_cycle2) { create(:open_order_cycle, distributors: [hub2]) } + let!(:order2) do + create( + :completed_order_with_totals, + distributor: hub2, + order_cycle: order_cycle2, + line_items_count: 3 + ) end - let(:params) { { display_summary_row: true } } - let(:report) { Reporting::Reports::Suppliers::Base.new(current_user, { q: params }) } - let(:report_table_rows) { report.rows } before do - login_as current_user + login_as owner visit admin_reports_path end @@ -53,30 +66,76 @@ "Total ($)" ].join(" ")) - line = page.find('table.report__table tbody tr').text - expect(line).to have_content([ - supplier.name, - supplier.address.full_address, - "none", - "none", - hub.name, - hub.address.full_address, - "none", - order.number, - order.completed_at.to_date.to_s, - order_cycle.name, - order_cycle.orders_open_at.to_date.to_s, - order_cycle.orders_close_at.to_date.to_s, - product.name, - variant.full_name, - '1', - '10.0', - '10.0', - '0.0', - '0.0', - '0.0', - '10.0', - ].compact.join(" ")) + lines = page.all('table.report__table tbody tr').map(&:text) + # 5 line_item rows + 1 summary row = 6 rows + expect(lines.count).to be(6) + + hub1_rows = lines.select { |line| line.include?(hub1.name) } + order1.line_items.each_with_index do |line_item, index| + variant = line_item.variant + supplier = line_item.supplier + product = line_item.variant.product + line = hub1_rows[index] + + expect(line).to have_content([ + supplier.name, + supplier.address.full_address, + "none", + "none", + hub1.name, + hub1.address.full_address, + "none", + order1.number, + order1.completed_at.to_date.to_s, + order_cycle1.name, + order_cycle1.orders_open_at.to_date.to_s, + order_cycle1.orders_close_at.to_date.to_s, + product.name, + variant.full_name, + 1, + 10.0, + 10.0, + 0.0, + 0.0, + 0.0, + 10.0, + ].compact.join(" ")) + end + + hub2_rows = lines.select { |line| line.include?(hub2.name) } + order2.line_items.each_with_index do |line_item, index| + variant = line_item.variant + supplier = line_item.supplier + product = line_item.variant.product + line = hub2_rows[index] + + expect(line).to have_content([ + supplier.name, + supplier.address.full_address, + "none", + "none", + hub2.name, + hub2.address.full_address, + "none", + order2.number, + order2.completed_at.to_date.to_s, + order_cycle2.name, + order_cycle2.orders_open_at.to_date.to_s, + order_cycle2.orders_close_at.to_date.to_s, + product.name, + variant.full_name, + 1, + 10.0, + 10.0, + 0.0, + 0.0, + 0.0, + 10.0, + ].compact.join(" ")) + end + + # summary row + expect(lines.last).to have_content("TOTAL 50.0 50.0 0.0 0.0 0.0 50.0") end end end