Skip to content

armgit / spree

Public Imported from GitHub
0

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.

add specs for Product#with_property_values

Łukasz Adamczyk4d36abff1080
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index 510db85940..76009d8951 100644
--- a/core/app/models/concerns/spree/product_scopes.rb
+++ b/core/app/models/concerns/spree/product_scopes.rb
@@ -110,7 +110,7 @@ module Spree
add_search_scope :with_property_values do |property_filter_param, property_values|
joins(product_properties: :property).
where(Property.table_name => { filter_param: property_filter_param }).
- where(ProductProperty.table_name => { filter_param: property_values })
+ where(ProductProperty.table_name => { filter_param: property_values.map(&:parameterize) })
end
add_search_scope :with_option do |option|
diff --git a/core/lib/spree/testing_support/factories/property_factory.rb b/core/lib/spree/testing_support/factories/property_factory.rb
index 592fba7f51..2d6c148992 100644
--- a/core/lib/spree/testing_support/factories/property_factory.rb
+++ b/core/lib/spree/testing_support/factories/property_factory.rb
@@ -2,5 +2,11 @@ FactoryBot.define do
factory :property, class: Spree::Property do
name { 'baseball_cap_color' }
presentation { 'cap color' }
+
+ trait :brand do
+ name { 'brand' }
+ presentation { 'Brand' }
+ filter_param { 'brand' }
+ end
end
end
diff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rb
index 9494925aa3..98e4e801f2 100644
--- a/core/spec/models/spree/product/scopes_spec.rb
+++ b/core/spec/models/spree/product/scopes_spec.rb
@@ -80,17 +80,18 @@ describe 'Product scopes', type: :model do
end
context 'property scopes' do
- let(:name) { 'A proper tee' }
- let(:value) { 'A proper value' }
- let!(:property) { create(:property, name: name) }
+ let(:name) { property.name }
+ let(:value) { 'Alpha' }
+
+ let(:product_property) { create(:product_property, property: property, value: value) }
+ let(:property) { create(:property, :brand) }
before do
- product.properties << property
- product.product_properties.find_by(property: property).update_column(:value, value)
+ product.product_properties << product_property
end
context 'with_property' do
- let(:with_property) { Spree::Product.method(:with_property) }
+ subject(:with_property) { Spree::Product.method(:with_property) }
it "finds by a property's name" do
expect(with_property.call(name).count).to eq(1)
@@ -114,7 +115,7 @@ describe 'Product scopes', type: :model do
end
context 'with_property_value' do
- let(:with_property_value) { Spree::Product.method(:with_property_value) }
+ subject(:with_property_value) { Spree::Product.method(:with_property_value) }
it "finds by a property's name" do
expect(with_property_value.call(name, value).count).to eq(1)
@@ -148,6 +149,32 @@ describe 'Product scopes', type: :model do
expect(with_property_value.call(property.id, 'fake').count).to eq(0)
end
end
+
+ context 'with_property_values' do
+ subject(:with_property_values) { Spree::Product.method(:with_property_values) }
+
+ let!(:product_2) { create(:product, product_properties: [product_2_property]) }
+ let(:product_2_property) { create(:product_property, property: property, value: value_2) }
+ let(:value_2) { 'Beta 10%' }
+
+ before do
+ create(:product, product_properties: [create(:product_property, property: property, value: '20% Gamma')])
+ end
+
+ it 'finds by property values' do
+ expect(with_property_values.call(name, [value, value_2, 'non_existent'])).to contain_exactly(
+ product, product_2
+ )
+ end
+
+ it 'cannot find with an invalid property name' do
+ expect(with_property_values.call('fake', [value, value_2])).to be_empty
+ end
+
+ it 'cannot find with invalid property values' do
+ expect(with_property_values.call(name, ['fake'])).to be_empty
+ end
+ end
end
context '#add_simple_scopes' do