From 2e5c5261701f00bbac44eaa3a68a835a5dd32696 Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Thu, 22 Aug 2024 17:12:47 -0600 Subject: [PATCH] Adds basic coverage on report file download Moves file download into report helper Removes pdf file assertation Removes test on PDF file on sales tax report Removes PDF testing from helper --- spec/support/reports_helper.rb | 16 ++++++ .../reports/orders_and_distributors_spec.rb | 55 +++++++++++-------- .../sales_tax_totals_by_order_spec.rb | 18 +----- 3 files changed, 51 insertions(+), 38 deletions(-) diff --git a/spec/support/reports_helper.rb b/spec/support/reports_helper.rb index d8f6df5f46..2927fee405 100644 --- a/spec/support/reports_helper.rb +++ b/spec/support/reports_helper.rb @@ -12,4 +12,20 @@ module ReportsHelper expect(page).not_to have_selector ".loading" expect(page).to have_button "Go", disabled: false end + + def generate_report + run_report + click_on "Download Report" + wait_for_download + end + + def load_file_txt(extension, downloaded_filename) + case extension + when "csv" + CSV.read(downloaded_filename).join(" ") + when "xlsx" + xlsx = Roo::Excelx.new(downloaded_filename) + xlsx.map(&:to_a).join(" ") + end + end end diff --git a/spec/system/admin/reports/orders_and_distributors_spec.rb b/spec/system/admin/reports/orders_and_distributors_spec.rb index ecacbeec15..37c3267c31 100644 --- a/spec/system/admin/reports/orders_and_distributors_spec.rb +++ b/spec/system/admin/reports/orders_and_distributors_spec.rb @@ -5,6 +5,7 @@ require "system_helper" RSpec.describe "Orders And Distributors" do include AuthenticationHelper include WebHelper + include ReportsHelper describe "Orders And Distributors" do let!(:distributor) { create(:distributor_enterprise, name: "By Bike") } @@ -19,6 +20,13 @@ RSpec.describe "Orders And Distributors" do } context "as an enterprise user" do + let(:header) { + ["Order date", "Order Id", "Customer Name", "Customer Email", "Customer Phone", + "Customer City", "SKU", "Item name", "Variant", "Quantity", "Max Quantity", + "Cost", "Shipping Cost", "Payment Method", "Distributor", "Distributor address", + "Distributor city", "Distributor postcode", "Shipping Method", + "Shipping instructions"] + } let(:line_item1) { [completed_at, order.id, "John Doe", order.email, "123-456-7890", "Herndon", "ABC", Spree::Product.first.name.to_s, "1g", "1", "none", "10.0", "none", "Check", @@ -56,28 +64,7 @@ RSpec.describe "Orders And Distributors" do rows = find("table.report__table").all("thead tr") table_headers = rows.map { |r| r.all("th").map { |c| c.text.strip } } - expect(table_headers).to eq([ - ['Order date', - 'Order Id', - 'Customer Name', - 'Customer Email', - 'Customer Phone', - 'Customer City', - 'SKU', - 'Item name', - 'Variant', - 'Quantity', - 'Max Quantity', - 'Cost', - 'Shipping Cost', - 'Payment Method', - 'Distributor', - 'Distributor address', - 'Distributor city', - 'Distributor postcode', - 'Shipping Method', - 'Shipping instructions'] - ]) + expect(table_headers).to eq([header]) expect(all('table.report__table tbody tr').count).to eq( Spree::LineItem.where( @@ -99,6 +86,30 @@ RSpec.describe "Orders And Distributors" do expect(table).to have_content(line_item4) expect(table).to have_content(line_item5) end + + describe "downloading the report" do + shared_examples "reports generated as" do |output_type, extension| + context output_type.to_s do + it "downloads the #{output_type} file" do + select output_type, from: "report_format" + + expect { generate_report }.to change { downloaded_filenames.length }.from(0).to(1) + + expect(downloaded_filename).to match(/.*\.#{extension}/) + + downloaded_file_txt = load_file_txt(extension, downloaded_filename) + + expect(downloaded_file_txt).to have_text header.join(" ") + expect(downloaded_file_txt).to have_text( + "By Bike 10 Lovely Street Herndon 20170 UPS Ground", count: 5 + ) + end + end + end + + it_behaves_like "reports generated as", "CSV", "csv" + it_behaves_like "reports generated as", "Spreadsheet", "xlsx" + end end context "as admin" do diff --git a/spec/system/admin/reports/sales_tax/sales_tax_totals_by_order_spec.rb b/spec/system/admin/reports/sales_tax/sales_tax_totals_by_order_spec.rb index 7377869a9f..690d2df0e7 100644 --- a/spec/system/admin/reports/sales_tax/sales_tax_totals_by_order_spec.rb +++ b/spec/system/admin/reports/sales_tax/sales_tax_totals_by_order_spec.rb @@ -3,6 +3,8 @@ require 'system_helper' RSpec.describe "Sales Tax Totals By order" do + include ReportsHelper + # Scenarion 1: added tax # 1 producer # 1 distributor @@ -467,20 +469,4 @@ RSpec.describe "Sales Tax Totals By order" do report_subtype: :sales_tax_totals_by_order ) end - - def generate_report - run_report - click_on "Download Report" - wait_for_download - end - - def load_file_txt(extension, downloaded_filename) - case extension - when "csv" - CSV.read(downloaded_filename).join(" ") - when "xlsx" - xlsx = Roo::Excelx.new(downloaded_filename) - xlsx.map(&:to_a).join(" ") - end - end end