mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-18 04:39:14 +00:00
Merge pull request #12574 from rioug/refactor-products-renderer
[Product Refactor] Refactor products renderer
This commit is contained in:
@@ -14,7 +14,7 @@ FactoryBot.define do
|
||||
primary_taxon { Spree::Taxon.first || FactoryBot.create(:taxon) }
|
||||
supplier { Enterprise.is_primary_producer.first || FactoryBot.create(:supplier_enterprise) }
|
||||
|
||||
# createing a product here will end up creating an extra variant, as creating product will
|
||||
# creating a product here will end up creating an extra variant, as creating product will
|
||||
# create a "standard variant" by default. We could try to pass the variant instance we
|
||||
# are creating but it fails because then the variant instance gets saved and it fails because
|
||||
# the product isn't associated yet. It's a chicken and egg problem.
|
||||
|
||||
@@ -3,7 +3,14 @@
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe OrderCycles::DistributedProductsService do
|
||||
describe "#products_supplier_relation" do
|
||||
# NOTE: product_relation_incl_supplier is tested via ProductsRenderer specs:
|
||||
# spec/services/products_renderer_spec.rb
|
||||
|
||||
describe "#products_relation" do
|
||||
subject(:products_relation) {
|
||||
described_class.new(distributor, order_cycle, customer).products_relation
|
||||
}
|
||||
|
||||
let(:distributor) { create(:distributor_enterprise) }
|
||||
let(:product) { create(:product) }
|
||||
let(:variant) { product.variants.first }
|
||||
@@ -16,16 +23,12 @@ RSpec.describe OrderCycles::DistributedProductsService do
|
||||
supplier = create(:supplier_enterprise)
|
||||
variant.update(supplier: )
|
||||
create(:variant, product:, supplier: )
|
||||
expect(
|
||||
described_class.new(distributor, order_cycle, customer).products_supplier_relation
|
||||
).to eq([product])
|
||||
expect(products_relation).to eq([product])
|
||||
end
|
||||
|
||||
describe "product distributed by distributor in the OC" do
|
||||
it "returns products" do
|
||||
expect(
|
||||
described_class.new(distributor, order_cycle, customer).products_supplier_relation
|
||||
).to eq([product])
|
||||
expect(products_relation).to eq([product])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -39,9 +42,7 @@ RSpec.describe OrderCycles::DistributedProductsService do
|
||||
end
|
||||
|
||||
it "does not return product" do
|
||||
expect(
|
||||
described_class.new(distributor, order_cycle, customer).products_supplier_relation
|
||||
).not_to include product
|
||||
expect(products_relation).not_to include product
|
||||
end
|
||||
end
|
||||
|
||||
@@ -52,25 +53,20 @@ RSpec.describe OrderCycles::DistributedProductsService do
|
||||
end
|
||||
|
||||
it "does not return product" do
|
||||
expect(
|
||||
described_class.new(distributor, order_cycle, customer).products_supplier_relation
|
||||
).not_to include product
|
||||
expect(products_relation).not_to include product
|
||||
end
|
||||
end
|
||||
|
||||
describe "filtering products that are out of stock" do
|
||||
context "with regular variants" do
|
||||
it "returns product when variant is in stock" do
|
||||
expect(
|
||||
described_class.new(distributor, order_cycle, customer).products_supplier_relation
|
||||
).to include product
|
||||
expect(products_relation).to include product
|
||||
end
|
||||
|
||||
it "does not return product when variant is out of stock" do
|
||||
variant.update_attribute(:on_hand, 0)
|
||||
expect(
|
||||
described_class.new(distributor, order_cycle, customer).products_supplier_relation
|
||||
).not_to include product
|
||||
|
||||
expect(products_relation).not_to include product
|
||||
end
|
||||
end
|
||||
|
||||
@@ -80,20 +76,80 @@ RSpec.describe OrderCycles::DistributedProductsService do
|
||||
}
|
||||
|
||||
it "does not return product when an override is out of stock" do
|
||||
expect(
|
||||
described_class.new(distributor, order_cycle, customer).products_supplier_relation
|
||||
).not_to include product
|
||||
expect(products_relation).not_to include product
|
||||
end
|
||||
|
||||
it "returns product when an override is in stock" do
|
||||
variant.update_attribute(:on_hand, 0)
|
||||
override.update_attribute(:count_on_hand, 10)
|
||||
expect(
|
||||
described_class.new(distributor, order_cycle, customer).products_supplier_relation
|
||||
).to include product
|
||||
|
||||
expect(products_relation).to include product
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "sorting" do
|
||||
let(:order_cycle) do
|
||||
create(:simple_order_cycle, distributors: [distributor])
|
||||
end
|
||||
let(:exchange) { order_cycle.exchanges.to_enterprises(distributor).outgoing.first }
|
||||
|
||||
let(:fruits) { create(:taxon) }
|
||||
let(:cakes) { create(:taxon) }
|
||||
let(:fruits_supplier) { create(:supplier_enterprise) }
|
||||
let(:cakes_supplier) { create(:supplier_enterprise) }
|
||||
let!(:product_apples) {
|
||||
create(:product, name: "apples", primary_taxon_id: fruits.id,
|
||||
supplier_id: fruits_supplier.id, inherits_properties: true)
|
||||
}
|
||||
let!(:product_banana_bread) {
|
||||
create(:product, name: "banana bread", primary_taxon_id: cakes.id,
|
||||
supplier_id: cakes_supplier.id, inherits_properties: true)
|
||||
}
|
||||
let!(:product_cherries) {
|
||||
create(:product, name: "cherries", primary_taxon_id: fruits.id,
|
||||
supplier_id: fruits_supplier.id, inherits_properties: true)
|
||||
}
|
||||
let!(:product_doughnuts) {
|
||||
create(:product, name: "doughnuts", primary_taxon_id: cakes.id,
|
||||
supplier_id: cakes_supplier.id, inherits_properties: true)
|
||||
}
|
||||
|
||||
before do
|
||||
exchange.variants << product_apples.variants.first
|
||||
exchange.variants << product_banana_bread.variants.first
|
||||
exchange.variants << product_cherries.variants.first
|
||||
exchange.variants << product_doughnuts.variants.first
|
||||
end
|
||||
|
||||
it "sorts products by the distributor's preferred taxon list" do
|
||||
allow(distributor)
|
||||
.to receive(:preferred_shopfront_product_sorting_method) { "by_category" }
|
||||
allow(distributor)
|
||||
.to receive(:preferred_shopfront_taxon_order) { "#{cakes.id},#{fruits.id}" }
|
||||
|
||||
expect(products_relation)
|
||||
.to eq([product_banana_bread, product_doughnuts, product_apples, product_cherries])
|
||||
end
|
||||
|
||||
it "sorts products by the distributor's preferred producer list" do
|
||||
allow(distributor)
|
||||
.to receive(:preferred_shopfront_product_sorting_method) { "by_producer" }
|
||||
allow(distributor).to receive(:preferred_shopfront_producer_order) {
|
||||
"#{cakes_supplier.id},#{fruits_supplier.id}"
|
||||
}
|
||||
|
||||
expect(products_relation)
|
||||
.to eq([product_banana_bread, product_doughnuts, product_apples, product_cherries])
|
||||
end
|
||||
|
||||
it "alphabetizes products by name when taxon list is not set" do
|
||||
allow(distributor).to receive(:preferred_shopfront_taxon_order) { "" }
|
||||
|
||||
expect(products_relation)
|
||||
.to eq([product_apples, product_banana_bread, product_cherries, product_doughnuts])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#variants_relation" do
|
||||
|
||||
@@ -38,34 +38,6 @@ RSpec.describe ProductsRenderer do
|
||||
exchange.variants << product_doughnuts.variants.first
|
||||
end
|
||||
|
||||
describe "sorting" do
|
||||
it "sorts products by the distributor's preferred taxon list" do
|
||||
allow(distributor)
|
||||
.to receive(:preferred_shopfront_taxon_order) { "#{cakes.id},#{fruits.id}" }
|
||||
products = products_renderer.send(:products)
|
||||
expect(products)
|
||||
.to eq([product_banana_bread, product_doughnuts, product_apples, product_cherries])
|
||||
end
|
||||
|
||||
it "sorts products by the distributor's preferred producer list" do
|
||||
allow(distributor)
|
||||
.to receive(:preferred_shopfront_product_sorting_method) { "by_producer" }
|
||||
allow(distributor).to receive(:preferred_shopfront_producer_order) {
|
||||
"#{cakes_supplier.id},#{fruits_supplier.id}"
|
||||
}
|
||||
products = products_renderer.send(:products)
|
||||
expect(products)
|
||||
.to eq([product_banana_bread, product_doughnuts, product_apples, product_cherries])
|
||||
end
|
||||
|
||||
it "alphabetizes products by name when taxon list is not set" do
|
||||
allow(distributor).to receive(:preferred_shopfront_taxon_order) { "" }
|
||||
products = products_renderer.send(:products)
|
||||
expect(products)
|
||||
.to eq([product_apples, product_banana_bread, product_cherries, product_doughnuts])
|
||||
end
|
||||
end
|
||||
|
||||
context "filtering" do
|
||||
it "filters products by name_or_meta_keywords_or_variants_display_as_or_" \
|
||||
"variants_display_name_or_variants_supplier_name_cont" do
|
||||
@@ -111,7 +83,6 @@ RSpec.describe ProductsRenderer do
|
||||
expect(products).to eq([product_apples, product_cherries])
|
||||
end
|
||||
|
||||
# TODO this is a bit flaky due to banana bread having two supplier
|
||||
it "filters products with a product property or a producer property" do
|
||||
cakes_supplier.producer_properties.create!({ property_id: property_organic.id,
|
||||
value: '1', position: 1 })
|
||||
|
||||
Reference in New Issue
Block a user