From 674974f868114bffbc24b2eddc9331d54588c003 Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Wed, 22 Nov 2023 10:03:35 +0000 Subject: [PATCH] Moves block within test description This way, we don't need to use it as a global method Clarifies context naming --- spec/system/admin/orders_spec.rb | 72 ++++++++++++++++---------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/spec/system/admin/orders_spec.rb b/spec/system/admin/orders_spec.rb index 3582f198e3..5dcb793f45 100644 --- a/spec/system/admin/orders_spec.rb +++ b/spec/system/admin/orders_spec.rb @@ -510,7 +510,7 @@ describe ' visit spree.admin_orders_path end - context "bulk print invoices" do + context "can bulk send invoices per email" do before do Spree::Config[:enable_invoices?] = true Spree::Config[:enterprise_number_required_on_invoices?] = false @@ -524,7 +524,7 @@ describe ' order5.update(state: "payment") end - it "can bulk print invoices but only for the 'complete' or 'resumed' ones" do + it "can bulk send invoices per email, but only for the 'complete' or 'resumed' ones" do within "#listing_orders" do page.find("input[name='bulk_ids[]'][value='#{order2.id}']").click page.find("input[name='bulk_ids[]'][value='#{order3.id}']").click @@ -572,37 +572,48 @@ describe ' expect(page).to have_content "Confirmation emails sent for 2 orders." end - it "can bulk print invoices from 2 orders" do - page.find("#listing_orders tbody tr:nth-child(1) input[name='bulk_ids[]']").click - page.find("#listing_orders tbody tr:nth-child(2) input[name='bulk_ids[]']").click - - page.find("span.icon-reorder", text: "ACTIONS").click - within ".ofn-drop-down .menu" do - expect { - page.find("span", text: "Print Invoices").click # Prints invoices in bulk - }.to enqueue_job(BulkInvoiceJob).exactly(:once) + context "can bulk print invoices" do + def extract_pdf_content + pdf_href = page.all('a').pluck('href') + page.find(class: "button", text: "VIEW FILE").click + invoice_file_number = pdf_href[0][45..59] + invoice_path = "tmp/invoices/#{invoice_file_number}.pdf" + reader = PDF::Reader.new(invoice_path) + invoice_content = reader.pages.map(&:text) end - expect(page).to have_content "Compiling Invoices" - expect(page).to have_content "Please wait until the PDF is ready " \ - "before closing this modal." + it "bulk prints invoices in pdf format" do + page.find("#listing_orders tbody tr:nth-child(1) input[name='bulk_ids[]']").click + page.find("#listing_orders tbody tr:nth-child(2) input[name='bulk_ids[]']").click - # we don't run Sidekiq in test environment, so we need to manually run enqueued jobs - # to generate PDF files, and change the modal accordingly - perform_enqueued_jobs(only: BulkInvoiceJob) + page.find("span.icon-reorder", text: "ACTIONS").click + within ".ofn-drop-down .menu" do + expect { + page.find("span", text: "Print Invoices").click # Prints invoices in bulk + }.to enqueue_job(BulkInvoiceJob).exactly(:once) + end - expect(page).to have_content "Bulk Invoice created" + expect(page).to have_content "Compiling Invoices" + expect(page).to have_content "Please wait until the PDF is ready " \ + "before closing this modal." - within ".modal-content" do - expect(page).to have_link(class: "button", text: "VIEW FILE", href: /invoices/) + # we don't run Sidekiq in test environment, so we need to manually run enqueued jobs + # to generate PDF files, and change the modal accordingly + perform_enqueued_jobs(only: BulkInvoiceJob) - extract_pdf_content # extracts content do variable invoice_content + expect(page).to have_content "Bulk Invoice created" - expect(@invoice_content).to have_content("TAX INVOICE", count: 2) - expect(@invoice_content).to have_content("TAX INVOICE: #{order4.number}") - expect(@invoice_content).to have_content("TAX INVOICE: #{order5.number}") - expect(@invoice_content).to have_content("From: #{distributor4.name}") - expect(@invoice_content).to have_content("From: #{distributor5.name}") + within ".modal-content" do + expect(page).to have_link(class: "button", text: "VIEW FILE", href: /invoices/) + + invoice_content = extract_pdf_content # extracts content do variable invoice_content + + expect(invoice_content).to have_content("TAX INVOICE", count: 2) + expect(invoice_content).to have_content("TAX INVOICE: #{order4.number}") + expect(invoice_content).to have_content("TAX INVOICE: #{order5.number}") + expect(invoice_content).to have_content("From: #{distributor4.name}") + expect(invoice_content).to have_content("From: #{distributor5.name}") + end end end @@ -933,12 +944,3 @@ describe ' end end end - -def extract_pdf_content - pdf_href = page.all('a').pluck('href') - page.find(class: "button", text: "VIEW FILE").click - invoice_file_number = pdf_href[0][45..59] - invoice_path = "tmp/invoices/#{invoice_file_number}.pdf" - reader = PDF::Reader.new(invoice_path) - @invoice_content = reader.pages.map(&:text) -end