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.

API and finder specs

Damian Kaczmarczyk24238a5120ae
diff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
index 9506caa18a..7e6219d656 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -253,6 +253,30 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
end
+ context 'sorting by name' do
+ context 'A-Z' do
+ before { get '/api/v2/storefront/products?sort=name' }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by name' do
+ expect(json_response['data'].count).to eq Spree::Product.available.count
+ expect(json_response['data'].pluck(:id)).to eq Spree::Product.available.order(:name).map(&:id).map(&:to_s)
+ end
+ end
+
+ context 'Z-A' do
+ before { get '/api/v2/storefront/products?sort=-name' }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by name with descending order' do
+ expect(json_response['data'].count).to eq Spree::Product.available.count
+ expect(json_response['data'].pluck(:id)).to eq Spree::Product.available.order(name: :desc).map(&:id).map(&:to_s)
+ end
+ end
+ end
+
context 'sorting by updated_at' do
context 'ascending order' do
before { get '/api/v2/storefront/products?sort=updated_at' }
diff --git a/core/spec/finders/spree/products/find_spec.rb b/core/spec/finders/spree/products/find_spec.rb
index 6102c68240..f2e646c0f7 100644
--- a/core/spec/finders/spree/products/find_spec.rb
+++ b/core/spec/finders/spree/products/find_spec.rb
@@ -415,7 +415,7 @@ module Spree
current_currency: 'USD'
).execute.map(&:id)
- expect(product_ids).to match_array Spree::Product.available.order(available_on: :desc).map(&:id)
+ expect(product_ids).to eq Spree::Product.available.order(available_on: :desc).map(&:id)
end
it 'returns products in price-high-to-low order' do
@@ -429,7 +429,7 @@ module Spree
current_currency: 'USD'
).execute.to_a
- expect(products).to match_array [product_2, product_3, product]
+ expect(products).to eq [product_2, product_3, product]
end
it 'returns products in price-low-to-high order' do
@@ -443,7 +443,35 @@ module Spree
current_currency: 'USD'
).execute
- expect(products).to match_array [product, product_3, product_2]
+ expect(products).to eq [product, product_3, product_2]
+ end
+
+ it 'returns products in name-a-z order' do
+ params = {
+ sort_by: 'name-a-z'
+ }
+
+ products = described_class.new(
+ scope: Spree::Product.all,
+ params: params,
+ current_currency: 'USD'
+ ).execute
+
+ expect(products).to eq [product, product_2, product_3]
+ end
+
+ it 'returns products in name-z-a order' do
+ params = {
+ sort_by: 'name-z-a'
+ }
+
+ products = described_class.new(
+ scope: Spree::Product.all,
+ params: params,
+ current_currency: 'USD'
+ ).execute
+
+ expect(products).to eq [product_3, product_2, product]
end
end
end