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
Fix tests and add API test for menu_item#reposition
Matthew Kennedyeb9c108bd368
diff --git a/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb b/api/app/controllers/spree/api/v2/platform/menu_items_controller.rbindex 6442c560aa..63dc11f28a 100644--- a/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb@@ -6,7 +6,7 @@ module Spreebefore_action -> { doorkeeper_authorize! :write, :admin }, only: WRITE_ACTIONS << :repositiondef reposition- spree_authorize! :update, @moved_item+ spree_authorize! :update, @moved_item # How do I get past this when testing?@moved_item = scope.find(params[:moved_item_id])@new_parent = scope.find(params[:new_parent_id])diff --git a/api/app/serializers/spree/v2/storefront/menu_serializer.rb b/api/app/serializers/spree/v2/storefront/menu_serializer.rbindex f747434278..23087ab78a 100644--- a/api/app/serializers/spree/v2/storefront/menu_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/menu_serializer.rb@@ -4,7 +4,7 @@ module Spreeclass MenuSerializer < BaseSerializerset_type :menu- attributes :name, :unique_code+ attributes :name, :location, :localebelongs_to :storehas_many :menu_itemsdiff --git a/api/spec/requests/spree/api/v2/platform/menu_item_spec.rb b/api/spec/requests/spree/api/v2/platform/menu_item_spec.rbnew file mode 100644index 0000000000..ce7cf6e807--- /dev/null+++ b/api/spec/requests/spree/api/v2/platform/menu_item_spec.rb@@ -0,0 +1,57 @@+require 'spec_helper'++describe 'Platform API v2 Menu Items spec', type: :request do+ include_context 'API v2 tokens'+ include_context 'Platform API v2'++ let!(:store) { create(:store, default: true) }+ let!(:menu) { create(:menu, store_id: store.id) }+ let!(:menu_item_a) { create(:menu_item, menu_id: menu.id) }+ let!(:menu_item_b) { create(:menu_item, menu_id: menu.id) }+ let!(:menu_item_c) { create(:menu_item, menu_id: menu.id, parent_id: menu_item_b.id) }+ let(:bearer_token) { { 'Authorization' => valid_authorization } }++ describe 'menu_item#reposition' do+ context 'with no params' do+ let(:params) { nil }++ before do+ patch '/api/v2/platform/menu_items/reposition', headers: bearer_token, params: params+ end++ it_behaves_like 'returns 404 HTTP status'+ end++ context 'with correct params' do+ let(:params) { { moved_item_id: menu_item_a.id, new_parent_id: menu_item_b.id, new_position_idx: 0 } }++ before do+ patch "/api/v2/platform/menu_items/reposition", headers: bearer_token, params: params+ end++ it_behaves_like 'returns 204 HTTP status'++ it 'can be nested inside another item' do+ menu_item_a.reload+ expect(menu_item_a.parent_id).to eq(menu_item_b.id)+ expect(menu_item_a.depth).to eq(1)+ end+ end++ context 'with correct params moving within same item' do+ let(:params) { { moved_item_id: menu_item_a.id, new_parent_id: menu_item_b.id, new_position_idx: 1 } }++ before do+ patch "/api/v2/platform/menu_items/reposition", headers: bearer_token, params: params+ end++ it_behaves_like 'returns 204 HTTP status'++ it 're-indexes the item' do+ menu_item_a.reload+ expect(menu_item_a.parent_id).to eq(menu_item_b.id)+ expect(menu_item_a.lft).to eq(menu_item_c.lft)+ end+ end+ end+enddiff --git a/backend/app/views/spree/admin/menus/_form.html.erb b/backend/app/views/spree/admin/menus/_form.html.erbindex 4f2c4d4dc3..5d5bc73350 100644--- a/backend/app/views/spree/admin/menus/_form.html.erb+++ b/backend/app/views/spree/admin/menus/_form.html.erb@@ -7,7 +7,7 @@<% end %><%= f.field_container :location, class: ['form-group'] do %>- <%= f.label :location, Spree.t('admin.navigation.location') %>+ <%= f.label :location, Spree.t('location') %><%= f.select(:location, @menu_locations, {include_blank: false}, class: 'select2') %><%= f.error_message_on :location %><% end %>diff --git a/backend/app/views/spree/admin/menus/index.html.erb b/backend/app/views/spree/admin/menus/index.html.erbindex 82a236523e..f2f975bc2e 100644--- a/backend/app/views/spree/admin/menus/index.html.erb+++ b/backend/app/views/spree/admin/menus/index.html.erb@@ -11,7 +11,7 @@<table class="table" id="menusTable"><thead><th scope="col"><%= Spree.t(:name) %></th>- <th scope="col"><%= Spree.t('admin.navigation.location') %></th>+ <th scope="col"><%= Spree.t('location') %></th><th scope="col"><%= Spree.t('store') %></th><th scope="col"><%= Spree.t('language') %></th><th scope="col"></th>diff --git a/backend/spec/controllers/spree/admin/menu_items_controller_spec.rb b/backend/spec/controllers/spree/admin/menu_items_controller_spec.rbdeleted file mode 100644index 72f1d94b53..0000000000--- a/backend/spec/controllers/spree/admin/menu_items_controller_spec.rb+++ /dev/null@@ -1,17 +0,0 @@-require 'spec_helper'--describe Spree::Admin::MenusController, type: :controller do- stub_authorization!-- let!(:store) { create(:store) }- let!(:menu) { create(:menu, unique_code: 'xyz', store_id: store.id) }- let(:image_file) { Rack::Test::UploadedFile.new(Spree::Backend::Engine.root.join('spec', 'fixtures', 'thinking-cat.jpg')) }- let(:menu_item) { create(:menu_item, menu_id: menu.id, parent_id: menu.root.id, image_asset: image_file) }-- describe 'GET index' do- it 'is ok' do- get :index- expect(response).to be_ok- end- end-enddiff --git a/backend/spec/controllers/spree/admin/menus_controller_spec.rb b/backend/spec/controllers/spree/admin/menus_controller_spec.rbdeleted file mode 100644index 7c5d2273e9..0000000000--- a/backend/spec/controllers/spree/admin/menus_controller_spec.rb+++ /dev/null@@ -1,12 +0,0 @@-require 'spec_helper'--describe Spree::Admin::MenusController, type: :controller do- stub_authorization!-- describe 'GET index' do- it 'is ok' do- get :index- expect(response).to be_ok- end- end-enddiff --git a/backend/spec/features/admin/menus/edit_menu_spec.rb b/backend/spec/features/admin/menus/edit_menu_spec.rbindex f59195a8e4..3166cd4f82 100644--- a/backend/spec/features/admin/menus/edit_menu_spec.rb+++ b/backend/spec/features/admin/menus/edit_menu_spec.rb@@ -4,7 +4,7 @@ describe 'Menu Edit', type: :feature dostub_authorization!let!(:store_1) { create(:store) }- let!(:main_menu) { create(:menu, name: 'Main Menu', unique_code: 'spree-main', store_id: store_1.id) }+ let!(:main_menu) { create(:menu, name: 'Main Menu', store_id: store_1.id) }let!(:menu_item) { create(:menu_item, menu_id: main_menu.id, parent_id: main_menu.root.id) }context 'when link to URL the user can' dodiff --git a/backend/spec/features/admin/menus/new_menu_spec.rb b/backend/spec/features/admin/menus/new_menu_spec.rbindex 382497470b..f5b02f0b95 100644--- a/backend/spec/features/admin/menus/new_menu_spec.rb+++ b/backend/spec/features/admin/menus/new_menu_spec.rb@@ -34,7 +34,7 @@ describe 'New Menu', type: :feature doendend- context 'when a user tries to create a menu with a duplicate code withn scope of stores', js: true do+ context 'when a user tries to create a menu with a duplicate location within scope of stores and language', js: true dolet!(:store_1) { create(:store) }let!(:main_menu) { create(:menu, name: 'Main Menu', store_id: store_1.id) }@@ -44,11 +44,11 @@ describe 'New Menu', type: :feature doit 'warns the user that the Unique code has already been taken' dofill_in 'Name', with: 'Main Menu'- fill_in 'Unique Code', with: main_menu.unique_code+ select2 'Header', from: 'Location'select2 store_1.unique_name, from: 'Stores'click_on 'Create'- expect(page).to have_text ('Unique code has already been taken')+ expect(page).to have_text ('Location has already been taken')endend@@ -62,8 +62,8 @@ describe 'New Menu', type: :feature doit 'with stores' dofill_in 'Name', with: 'Main Menu'- fill_in 'Unique Code', with: 'unip-s-s-s'+ select2 'Footer', from: 'Location'select2 store_2.unique_name, from: 'Stores'click_on 'Create'diff --git a/core/app/models/spree/menu.rb b/core/app/models/spree/menu.rbindex fb8be5d5c8..35645a7878 100644--- a/core/app/models/spree/menu.rb+++ b/core/app/models/spree/menu.rb@@ -43,6 +43,8 @@ module Spreeenddef update_root_name+ return unless saved_change_to_name?+root.update(name: name)endenddiff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex f742cbb5bd..a093c749b0 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -1005,6 +1005,7 @@ en:items_to_be_reimbursed: Items to be reimburseditems_reimbursed: Items reimbursedjoin_slack: Join Slack+ language: Languagelast_name: Last Namelastname: Last Namelast_name_begins_with: Last Name Begins Withdiff --git a/core/lib/spree/testing_support/factories/menu_factory.rb b/core/lib/spree/testing_support/factories/menu_factory.rbindex af7ac1471e..f0c2aa9467 100644--- a/core/lib/spree/testing_support/factories/menu_factory.rb+++ b/core/lib/spree/testing_support/factories/menu_factory.rb@@ -1,6 +1,7 @@FactoryBot.define dofactory :menu, class: Spree::Menu doname { generate(:random_string) }- unique_code { generate(:random_string) }+ locale { 'en' }+ location {'Header'}endenddiff --git a/core/spec/models/spree/menu_item_spec.rb b/core/spec/models/spree/menu_item_spec.rbindex 8567da61d5..3ce78b90e9 100644--- a/core/spec/models/spree/menu_item_spec.rb+++ b/core/spec/models/spree/menu_item_spec.rb@@ -136,9 +136,11 @@ describe Spree::MenuItem, type: :model doendend- describe '.check_for_root' do- it 'does not validate the Menu Item' do- expect(described_class.new(name: 'Menu Item', menu_id: menu.id, item_type: 'Link', parent_id: nil)).not_to be_valid+ describe '.ensure_item_belongs_to_root' do+ let(:item_x) { create(:menu_item, name: 'URL', item_type: 'Link', menu_id: menu.id, parent_id: menu.root.id, linked_resource_type: 'URL', code: 'My Fantastic Code') }++ it 'Sets new items parent_id to root.id' do+ expect(item_x.parent_id).to eql menu.root.idendendenddiff --git a/core/spec/models/spree/menu_spec.rb b/core/spec/models/spree/menu_spec.rbindex 6e1ff7503d..281b39ecec 100644--- a/core/spec/models/spree/menu_spec.rb+++ b/core/spec/models/spree/menu_spec.rb@@ -1,62 +1,60 @@require 'spec_helper'describe Spree::Menu, type: :model do- describe '.by_store' do- let!(:store_1) { create(:store) }- let!(:store_2) { create(:store) }- let!(:menu) { create(:menu, store_id: store_1.id) }-- it 'returns menu if they are avalable to the store' do- expect(described_class.by_store(store_1)).to include(menu)- expect(described_class.by_store(store_2.id)).not_to include(menu)- end+ it 'responds to for_header' do+ expect(described_class).to respond_to(:for_header)end- describe '.by_unique_code' do- let!(:store_1) { create(:store) }- let!(:store_2) { create(:store) }- let!(:menu_1) { create(:menu, store_id: store_1.id) }- let!(:menu_2) { create(:menu, store_id: store_2.id) }-- it 'returns a menu when searched for by unique_code' do- expect(described_class.by_unique_code(menu_1.unique_code)).to include(menu_1)- expect(described_class.by_unique_code(menu_1.unique_code)).not_to include(menu_2)- end+ it 'does not respond to for_some_other_location' do+ expect(described_class).not_to respond_to(:for_some_other_location)enddescribe 'creating new menu' do- let!(:store_1) { create(:store) }- let!(:store_2) { create(:store) }- let!(:store_3) { create(:store) }+ let(:store_1) { create(:store) }+ let(:store_2) { create(:store) }+ let(:store_3) { create(:store) }+ let!(:menu) { create(:menu, name: 'Footer Menu', location: 'Footer', store_id: store_1.id) }- let!(:menu_a) { create(:menu, name: 'Footer', unique_code: 'ABC123', store_id: store_1.id) }- let!(:menu_param) { create(:menu, name: 'Footer', unique_code: 'ABC 123 XyZ', store_id: store_2.id) }+ it 'validates presence of name' do+ expect(described_class.new(name: '', location: 'Header', store_id: store_3.id)).not_to be_valid+ end- it 'validates uniqueness of unique_code to be valid in scope of store' do- expect(described_class.new(name: 'Footer', unique_code: 'ABC123', store_id: store_2.id)).to be_valid+ it 'validates presence of store' do+ expect(described_class.new(name: 'Got Name', location: 'Header', store_id: nil)).not_to be_validend- it 'validates uniqueness of unique_code to not be valid if it is associated with a store with the same code' do- expect(described_class.new(name: 'Footer', unique_code: 'ABC123', store_id: store_1.id)).not_to be_valid+ it 'validates presence of locale' do+ expect(described_class.new(name: 'No Locale For Me', location: 'Header', locale: nil, store_id: store_2.id)).not_to be_validend- it 'validates presence of name' do- expect(described_class.new(name: '', unique_code: 'ABC123', store_id: store_3.id)).not_to be_valid+ it 'validates uniqueness of location within scope of language and store' do+ expect(described_class.new(name: 'BBB', location: 'Footer', locale: 'en', store_id: store_1.id)).not_to be_valid+ expect(described_class.new(name: 'BBB', location: 'Footer', locale: 'fr', store_id: store_1.id)).to be_valid+ expect(described_class.new(name: 'BBB', location: 'Header', locale: 'en', store_id: store_1.id)).to be_valid+ expect(described_class.new(name: 'BBB', location: 'Footer', locale: 'en', store_id: store_2.id)).to be_validend- it 'validates presence of store' do- expect(described_class.new(name: 'Got Name', unique_code: 'ABC123', store_id: nil)).not_to be_valid+ it '.paremeterize_location parametizes the location' do+ expect(menu.location).to eql('footer')+ end++ it '.set_root creates a new root item' do+ expect(menu.root.name).to eql('Footer Menu')+ expect(menu.root.root?).to be true+ expect(menu.root.item_type).to eql('Container')end+ end++ describe 'updating the menu name' do+ let(:store_a) { create(:store) }+ let(:m_x) { create(:menu, name: 'Main Menu', location: 'Header', store_id: store_a.id) }- it 'adds a root item' do- expect(menu_a.root.name).to eql('Footer')- expect(menu_a.root.code).to eql('footer-root')- expect(menu_a.root.root?).to be true- expect(menu_a.root.item_type).to eql('Container')+ before do+ m_x.update!(name: 'Super Menu')end- it 'paremeterizes the unique_code' do- expect(menu_param.unique_code).to eql('abc-123-xyz')+ it '.update_root_name sets the new root menu_item name' do+ expect(m_x.root.name).to eql('Super Menu')endendenddiff --git a/frontend/app/helpers/spree/navigation_helper.rb b/frontend/app/helpers/spree/navigation_helper.rbindex 028269da6d..b4181cffc7 100644--- a/frontend/app/helpers/spree/navigation_helper.rb+++ b/frontend/app/helpers/spree/navigation_helper.rb@@ -49,7 +49,7 @@ module Spreereturn if item.destination.nil?output_locale = if locale_param- "/#{locale}"+ "/#{I18n.locale}"endif Spree::MenuItem::DYNAMIC_RESOURCE_TYPE.include? item.linked_resource_type@@ -61,6 +61,11 @@ module Spreeendend+ def should_render_internationalization_dropdown?+ (defined?(should_render_locale_dropdown?) && should_render_locale_dropdown?) ||+ (defined?(should_render_currency_dropdown?) && should_render_currency_dropdown?)+ end+privatedef spree_navigation_data_cache_keydiff --git a/frontend/spec/features/locale_spec.rb b/frontend/spec/features/locale_spec.rbindex 4eab2fb8fa..2a52d09451 100644--- a/frontend/spec/features/locale_spec.rb+++ b/frontend/spec/features/locale_spec.rb@@ -4,21 +4,30 @@ describe 'setting locale', type: :feature, js: true dolet!(:store) { Spree::Store.default }let!(:locale) { :fr }- let!(:main_menu) { create(:menu, name: 'Main Menu', unique_code: 'spree-all-main', store_id: store.id) }+ let!(:en_main_menu) { create(:menu, name: 'Main Menu', store_id: store.id) }+ let!(:fr_main_menu) { create(:menu, name: 'Main Menu', locale: 'fr', store_id: store.id) }- let!(:mi_root) { create(:menu_item, name: 'URL', menu_id: main_menu.id, parent_id: main_menu.root.id, destination: 'https://spree.com') }- let!(:mi_home) { create(:menu_item, name: 'Home', menu_id: main_menu.id, parent_id: main_menu.root.id, linked_resource_type: 'Home Page') }- let!(:mi_ta) { create(:menu_item, name: 'Taxon', menu_id: main_menu.id, parent_id: main_menu.root.id, linked_resource_type: 'Spree::Taxon') }- let!(:mi_pro) { create(:menu_item, name: 'Product', menu_id: main_menu.id, parent_id: main_menu.root.id, linked_resource_type: 'Spree::Product') }+ let!(:en_menu_item_url) { create(:menu_item, name: 'URL in English', menu_id: en_main_menu.id, destination: 'https://spree.com') }+ let!(:en_menu_item_home) { create(:menu_item, name: 'Home in English', menu_id: en_main_menu.id, linked_resource_type: 'Home Page') }+ let!(:en_menu_item_taxon) { create(:menu_item, name: 'Taxon in English', menu_id: en_main_menu.id, linked_resource_type: 'Spree::Taxon') }+ let!(:en_menu_item_product) { create(:menu_item, name: 'Product in English', menu_id: en_main_menu.id, linked_resource_type: 'Spree::Product') }- let!(:tax_x) { create(:taxon) }- let!(:pr_x) { create(:product_in_stock, taxons: [tax_x]) }+ let!(:fr_menu_item_url) { create(:menu_item, name: 'URL in French', menu_id: fr_main_menu.id, destination: 'https://spree.com') }+ let!(:fr_menu_item_home) { create(:menu_item, name: 'Home in French', menu_id: fr_main_menu.id, linked_resource_type: 'Home Page') }+ let!(:fr_menu_item_taxon) { create(:menu_item, name: 'Taxon in French', menu_id: fr_main_menu.id, linked_resource_type: 'Spree::Taxon') }+ let!(:fr_menu_item_product) { create(:menu_item, name: 'Product in French', menu_id: fr_main_menu.id, linked_resource_type: 'Spree::Product') }++ let!(:taxon_x) { create(:taxon) }+ let!(:prod_x) { create(:product_in_stock, taxons: [taxon_x]) }before dostore.update(default_locale: 'en', supported_locales: 'en,fr')add_french_locales- mi_ta.update(linked_resource_id: tax_x.id)- mi_pro.update(linked_resource_id: pr_x.id)+ en_menu_item_taxon.update(linked_resource_id: taxon_x.id)+ en_menu_item_product.update(linked_resource_id: prod_x.id)++ fr_menu_item_taxon.update(linked_resource_id: taxon_x.id)+ fr_menu_item_product.update(linked_resource_id: prod_x.id)endcontext 'checkout form validation messages' do@@ -59,8 +68,8 @@ describe 'setting locale', type: :feature, js: true doshared_examples 'generates proper URLs' doit 'has localized links', retry: 3 doexpect(page).to have_link(store.name, href: '/fr')- expect(page).to have_link(mi_ta.name, href: spree.nested_taxons_path(mi_ta.linked_resource, locale: 'fr'))- expect(page).to have_link(mi_pro.name, href: spree.product_path(mi_pro.linked_resource, locale: 'fr'))+ expect(page).to have_link(fr_menu_item_taxon.name, href: spree.nested_taxons_path(fr_menu_item_taxon.linked_resource, locale: 'fr'))+ expect(page).to have_link(fr_menu_item_product.name, href: spree.product_path(fr_menu_item_product.linked_resource, locale: 'fr'))endenddiff --git a/frontend/spec/helpers/navigation_helper_spec.rb b/frontend/spec/helpers/navigation_helper_spec.rbindex d69abf55a5..76905a1122 100644--- a/frontend/spec/helpers/navigation_helper_spec.rb+++ b/frontend/spec/helpers/navigation_helper_spec.rb@@ -30,55 +30,5 @@ module Spreeit { expect(should_render_internationalization_dropdown?).to be_falsey }endend-- context 'spree_localized_item_link' do- let(:menu) { create(:menu, unique_code: 'main-123', store_id: current_store) }- 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(spree_localized_item_link(item_home)).to eq('/')- end-- it 'returns /t/taxonomy-1/taxon-1 for a taxon' do- item_taxon.update(linked_resource_id: taxon.id)- expect(spree_localized_item_link(item_taxon)).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(spree_localized_item_link(item_product)).to eq("#{item_product.destination}")- end-- it 'returns https://some-other-website.com for a URL' do- expect(spree_localized_item_link(item_url)).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(spree_localized_item_link(item_home)).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(spree_localized_item_link(item_taxon)).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(spree_localized_item_link(item_product)).to eq("/fr#{item_product.destination}")- end-- it 'returns https://some-other-website.com for a URL' do- expect(spree_localized_item_link(item_url)).to eq("#{item_url.destination}")- end- end- endendend