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
Add test for helper method.
Matthew Kennedyeb08377eaf4c
diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rbindex 2e0e9f4d3e..888118878a 100644--- a/core/app/helpers/spree/base_helper.rb+++ b/core/app/helpers/spree/base_helper.rb@@ -25,24 +25,21 @@ module Spreeto_htmlend- def localized_item_link(item)+ def localized_item_link(item, locale)return if item.destination.nil?- output_locale = if current_locale == current_store.default_locale- ''+ output_locale = if locale == current_store.default_locale+ nilelse- "/#{current_locale}"+ "/#{locale}"endif Spree::MenuItem::DYNAMIC_RESOURCE_TYPE.include? item.linked_resource_type- output_locale.to_s + item.destination.to_s+ output_locale.to_s + item.destination+ elsif item.linked_resource_type == 'Home Page'+ output_locale || item.destinationelse- case item.linked_resource_type- when 'URL'- item.destination- when 'Home Page'- output_locale- end+ item.destinationendenddiff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rbindex 715176113b..ea2e4a7c18 100644--- a/core/app/models/spree/menu_item.rb+++ b/core/app/models/spree/menu_item.rb@@ -32,29 +32,26 @@ module Spreeprivatedef build_path- if DYNAMIC_RESOURCE_TYPE.include? linked_resource_type+ case linked_resource_type+ when 'Spree::Taxon'return if linked_resource.nil?- case linked_resource_type- when 'Spree::Taxon'- self.destination = if frontend_available?- Spree::Core::Engine.routes.url_helpers.nested_taxons_path(linked_resource.permalink)- else- "/#{Spree::Config[:api_taxon_path]}/#{linked_resource.permalink}"- end- when 'Spree::Product'- self.destination = if frontend_available?- Spree::Core::Engine.routes.url_helpers.product_path(linked_resource)- else- "/#{Spree::Config[:api_product_path]}/#{linked_resource.slug}"- end-- end- else- case linked_resource_type- when 'Home Page'- self.destination = '/'- end+ self.destination = if frontend_available?+ Spree::Core::Engine.routes.url_helpers.nested_taxons_path(linked_resource.permalink)+ else+ "/#{Spree::Config[:api_taxon_path]}/#{linked_resource.permalink}"+ end+ when 'Spree::Product'+ return if linked_resource.nil?++ self.destination = if frontend_available?+ Spree::Core::Engine.routes.url_helpers.product_path(linked_resource)+ else+ "/#{Spree::Config[:api_product_path]}/#{linked_resource.slug}"+ end++ when 'Home Page'+ self.destination = '/'endenddiff --git a/core/spec/helpers/base_helper_spec.rb b/core/spec/helpers/base_helper_spec.rbindex 38267788cd..7a4d81e580 100644--- a/core/spec/helpers/base_helper_spec.rb+++ b/core/spec/helpers/base_helper_spec.rb@@ -9,6 +9,56 @@ describe Spree::BaseHelper, type: :helper doallow(controller).to receive(:controller_name).and_return('test')end+ context 'localized_item_link' do+ let(:menu) { create(:menu, unique_code: 'main-123') }+ let(:product) { create(:product) }+ let(:taxon) { create(:taxon) }+ let(:item_url) { create(:menu_item, name: 'URL To Random Site', item_type: 'Link', menu_id: menu.id, parent_id: menu.root.id, linked_resource_type: 'URL', destination: 'https://some-other-website.com') }+ let(:item_home) { create(:menu_item, name: 'Home', item_type: 'Link', menu_id: menu.id, parent_id: menu.root.id, linked_resource_type: 'Home Page') }+ let(:item_product) { create(:menu_item, name: product.name, item_type: 'Link', menu_id: menu.id, parent_id: menu.root.id, linked_resource_type: 'Spree::Product') }+ let(:item_taxon) { create(:menu_item, name: taxon.name, item_type: 'Link', menu_id: menu.id, parent_id: menu.root.id, linked_resource_type: 'Spree::Taxon') }++ context 'when the default language is passed in' do+ it 'returns / for home page' do+ expect(localized_item_link(item_home, 'en')).to eq('/')+ end++ it 'returns /t/taxonomy-1/taxon-1 for a taxon' do+ item_taxon.update(linked_resource_id: taxon.id)+ expect(localized_item_link(item_taxon, 'en')).to eq("#{item_taxon.destination}")+ end++ it 'returns /products/product-18-4225 for a product' do+ item_product.update(linked_resource_id: product.id)+ expect(localized_item_link(item_product, 'en')).to eq("#{item_product.destination}")+ end++ it 'returns https://some-other-website.com for a URL' do+ expect(localized_item_link(item_url, 'en')).to eq("#{item_url.destination}")+ end+ end++ context 'with the none default locale passed in (:fr)' do+ it 'returns /fr for home page' do+ expect(localized_item_link(item_home, :fr)).to eq('/fr')+ end++ it 'returns /fr/t/taxonomy-1/taxon-1 for a taxon' do+ item_taxon.update(linked_resource_id: taxon.id)+ expect(localized_item_link(item_taxon, :fr)).to eq("/fr#{item_taxon.destination}")+ end++ it 'returns /fr/products/product-18-4225 for a product' do+ item_product.update(linked_resource_id: product.id)+ expect(localized_item_link(item_product, 'fr')).to eq("/fr#{item_product.destination}")+ end++ it 'returns https://some-other-website.com for a URL' do+ expect(localized_item_link(item_url, 'fr')).to eq("#{item_url.destination}")+ end+ end+ end+context 'available_countries' dolet(:country) { create(:country) }diff --git a/frontend/app/views/spree/shared/_footer.html.erb b/frontend/app/views/spree/shared/_footer.html.erbindex fa28d10cb7..4dc7b85d9a 100644--- a/frontend/app/views/spree/shared/_footer.html.erb+++ b/frontend/app/views/spree/shared/_footer.html.erb@@ -55,13 +55,13 @@<% footer_menu_root.children.each do |root| %><div class="col-3"><div class="footer-spree-label">- <%= link_to root.name, localized_item_link(root) %>+ <%= link_to root.name, localized_item_link(root, current_locale) %></div><% root.children.each do |child| %><% if child.code == 'category' && child.item_type == 'Container' %><% if child.children.present? %><% child.children.each do |category_link| %>- <div class="pt-2"><%= link_to category_link.name, localized_item_link(category_link) %></div>+ <div class="pt-2"><%= link_to category_link.name, localized_item_link(category_link, current_locale) %></div><% end %><% end %><% end %>diff --git a/frontend/app/views/spree/shared/_main_nav_bar.html.erb b/frontend/app/views/spree/shared/_main_nav_bar.html.erbindex c7a78d733b..af3bd82572 100644--- a/frontend/app/views/spree/shared/_main_nav_bar.html.erb+++ b/frontend/app/views/spree/shared/_main_nav_bar.html.erb@@ -11,7 +11,7 @@<% dropdown_toggle_class = has_dropdown ? "dropdown-toggle" : "" %><% data_attr = has_dropdown ? { toggle: "dropdown" } : {} %><% aria_attr = has_dropdown ? { haspopup: true, expanded: false } : {} %>- <%= link_to(root.name, localized_item_link(root), class: "nav-link main-nav-bar-item main-nav-bar-category-button #{dropdown_toggle_class}", data: data_attr, aria: aria_attr) %>+ <%= link_to(root.name, localized_item_link(root, current_locale), class: "nav-link main-nav-bar-item main-nav-bar-category-button #{dropdown_toggle_class}", data: data_attr, aria: aria_attr) %><% if has_dropdown %><div class="dropdown-menu w-100 shadow main-nav-bar-category-dropdown"><div class="container p-0 d-flex justify-content-xl-around mx-auto">@@ -28,7 +28,7 @@<ul class="pl-0"><% child.children.each do |category_link| %><li>- <%= link_to category_link.name, localized_item_link(category_link), class: "text-uppercase main-nav-bar-category-links dropdown-item truncate" %>+ <%= link_to category_link.name, localized_item_link(category_link, current_locale), class: "text-uppercase main-nav-bar-category-links dropdown-item truncate" %></li><% end %></ul>@@ -40,7 +40,7 @@<% if child.code == promo && child.item_type == 'Container' %><% if child.children.present? %><% child.children.each do |promo_banner| %>- <%= link_to localized_item_link(promo_banner), class: "dropdown-item" do %>+ <%= link_to localized_item_link(promo_banner, current_locale), class: "dropdown-item" do %><div class="category-image text-center my-4 mx-3"><% if promo_banner.has_attached_image %><%= image_tag main_app.url_for(promo_banner.image_asset), title: promo_banner.subtitle %>diff --git a/frontend/app/views/spree/shared/_mobile_navigation.html.erb b/frontend/app/views/spree/shared/_mobile_navigation.html.erbindex 5fd532914e..f56931a40d 100644--- a/frontend/app/views/spree/shared/_mobile_navigation.html.erb+++ b/frontend/app/views/spree/shared/_mobile_navigation.html.erb@@ -22,7 +22,7 @@<ul class="list-unstyled position-relative h-100 mobile-navigation-list"><% main_menu_root.children.each do |root| %><li class="d-flex justify-content-between align-items-center mobile-navigation-list-item">- <%= link_to root.name, localized_item_link(root), class: 'w-75' %>+ <%= link_to root.name, localized_item_link(root, current_locale), class: 'w-75' %><% if root.children.present? %><a class="w-25 text-right mobile-navigation-category-link" data-category="<%= root.name.parameterize %>" href="#" aria-label="<%= Spree.t('go_to_category') %>"><%= icon(name: 'arrow-right',@@ -41,7 +41,7 @@<% if child.children.present? %><% child.children.each do |category_link| %><li class="d-flex justify-content-between align-items-center mobile-navigation-list-item">- <%= link_to category_link.name, localized_item_link(category_link), class: 'w-75' %>+ <%= link_to category_link.name, localized_item_link(category_link, current_locale), class: 'w-75' %></li><% end %><% end %>