Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fixed `ReportLineItems::ProductsPerformance` (#12371)
Damian Legawiec318f50e342f6
diff --git a/core/app/models/spree/report_line_items/products_performance.rb b/core/app/models/spree/report_line_items/products_performance.rbindex 7185d37fec..6c9eaed1e3 100644--- a/core/app/models/spree/report_line_items/products_performance.rb+++ b/core/app/models/spree/report_line_items/products_performance.rb@@ -19,23 +19,23 @@ module Spreedelegate :sku, :name, to: :recorddef vendor- record.vendor_name+ record.try(:vendor_name)enddef brand- record.brand_name+ record.try(:brand_name)enddef category_lvl0- mapped_categories[0]+ mapped_categories&.firstenddef category_lvl1- mapped_categories[1]+ mapped_categories&.secondenddef category_lvl2- mapped_categories[2]+ mapped_categories&.thirdenddef price@@ -63,7 +63,7 @@ module Spreeenddef weeks_online- (Time.current - record.activated_at.in_time_zone(store.preferred_timezone)).seconds.in_weeks.to_i+ (Time.current - record.available_on.in_time_zone(store.preferred_timezone)).seconds.in_weeks.to_ienddef mapped_categoriesdiff --git a/core/spec/models/spree/report_line_items/products_performance_spec.rb b/core/spec/models/spree/report_line_items/products_performance_spec.rbnew file mode 100644index 0000000000..b6f2f2b696--- /dev/null+++ b/core/spec/models/spree/report_line_items/products_performance_spec.rb@@ -0,0 +1,84 @@+require 'spec_helper'++RSpec.describe Spree::ReportLineItems::ProductsPerformance do+ let(:store) { Spree::Store.default }+ let(:report) { create(:products_performance_report, store: store) }+ let(:order) { create(:completed_order_with_totals, store: store, currency: report.currency) }++ let!(:product) { order.line_items.first.product }+ let!(:variant) { order.line_items.first.variant }++ subject { report.line_items.first }++ describe '#vendor' do+ it 'returns vendor name from record' do+ expect(subject.vendor).to eq(variant.try(:vendor_name))+ end+ end++ describe '#brand' do+ it 'returns brand name from record' do+ expect(subject.brand).to eq(variant.try(:brand_name))+ end+ end++ describe '#category_levels' do+ let(:taxonomy) { store.taxonomies.first }+ let(:taxon) { create(:taxon, name: 'Shoes', taxonomy: taxonomy) }++ context 'when product has taxons' do+ before do+ product.taxons << taxon+ product.save!+ end++ it 'returns mapped category levels' do+ expect(subject.category_lvl0).to eq('Categories')+ expect(subject.category_lvl1).to eq('Shoes')+ expect(subject.category_lvl2).to be_nil+ end+ end++ context 'when product has no taxons' do+ it 'returns nil for all category levels' do+ expect(subject.category_lvl0).to be_nil+ expect(subject.category_lvl1).to be_nil+ expect(subject.category_lvl2).to be_nil+ end+ end+ end++ describe '#price' do+ it 'returns formatted price in report currency' do+ expect(subject.price).to eq(variant.price_in(report.currency).display_amount)+ end+ end++ describe '#weeks_online' do+ before do+ product.update(available_on: 2.weeks.ago)+ end++ it 'returns number of weeks since product activation' do+ expect(subject.weeks_online).to eq(2)+ end+ end++ describe 'money amounts' do+ let(:line_item) { report.line_items.first }++ it 'returns formatted money amounts' do+ expect(subject.pre_tax_amount).to eq(Spree::Money.new(line_item.pre_tax_amount, currency: report.currency))+ expect(subject.tax_total).to eq(Spree::Money.new(line_item.tax_total, currency: report.currency))+ expect(subject.promo_total).to eq(Spree::Money.new(line_item.promo_total, currency: report.currency))+ expect(subject.total).to eq(Spree::Money.new(line_item.total, currency: report.currency))+ end+ end++ describe '#quantity' do+ it 'returns line item quantity' do+ expect(subject.quantity).to eq(report.line_items.first.quantity)+ end+ end++end