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 more tests.

Matthew Kennedy410a43cc21e4
diff --git a/core/app/models/spree/menu.rb b/core/app/models/spree/menu.rb
index efcc099636..feab2dc692 100644
--- a/core/app/models/spree/menu.rb
+++ b/core/app/models/spree/menu.rb
@@ -5,7 +5,6 @@ module Spree
before_validation :paremeterize_unique_code
after_create :set_root
- after_save :set_root_menu_item_name
validates :name, presence: true
validates :unique_code, presence: true, uniqueness: true
@@ -24,9 +23,5 @@ module Spree
def set_root
self.root ||= MenuItem.create!(menu_id: id, name: name, item_type: 'Container', code: "#{name}-root")
end
-
- def set_root_menu_item_name
- root.update(name: name)
- end
end
end
diff --git a/core/lib/spree/testing_support/factories/menu_factory.rb b/core/lib/spree/testing_support/factories/menu_factory.rb
index 58860e787d..af7ac1471e 100644
--- a/core/lib/spree/testing_support/factories/menu_factory.rb
+++ b/core/lib/spree/testing_support/factories/menu_factory.rb
@@ -1,6 +1,6 @@
FactoryBot.define do
factory :menu, class: Spree::Menu do
- name { 'Main Menu' }
+ name { generate(:random_string) }
unique_code { generate(:random_string) }
end
end
diff --git a/core/spec/models/spree/menu_item_spec.rb b/core/spec/models/spree/menu_item_spec.rb
index 968d270b2e..f523ef6b0e 100644
--- a/core/spec/models/spree/menu_item_spec.rb
+++ b/core/spec/models/spree/menu_item_spec.rb
@@ -35,6 +35,55 @@ describe Spree::MenuItem, type: :model do
end
end
+ describe '.reset_link_attributes from URL to Taxon' do
+ let(:i_a) do
+ create(:menu_item, name: 'Home', item_type: 'Link', menu_id: menu.id,
+ parent_id: menu.root.id, linked_resource_type: 'URL', destination: 'http://somewhere.com', new_window: true, linked_resource_id: 5)
+ end
+
+ before do
+ i_a.update!(linked_resource_type: 'Spree::Taxon')
+ end
+
+ it 'sets resets the new_window to false' do
+ expect(i_a.new_window).to be false
+ end
+
+ it 'sets resets the destination to nil' do
+ expect(i_a.destination).to be nil
+ end
+ end
+
+ describe '.reset_link_attributes from URL to Home Page' do
+ let(:i_b) do
+ create(:menu_item, name: 'Home', item_type: 'Link', menu_id: menu.id,
+ parent_id: menu.root.id, linked_resource_type: 'URL', destination: 'http://somewhere.com', new_window: true, linked_resource_id: 5)
+ end
+
+ before do
+ i_b.update!(linked_resource_type: 'Home Page')
+ end
+
+ it 'sets destination to /' do
+ expect(i_b.destination).to eql('/')
+ end
+ end
+
+ describe '.reset_link_attributes from Link to Container' do
+ let(:i_c) do
+ create(:menu_item, name: 'Home', item_type: 'Link', menu_id: menu.id,
+ parent_id: menu.root.id, linked_resource_type: 'URL', destination: 'http://somewhere.com', new_window: true, linked_resource_id: 5)
+ end
+
+ before do
+ i_c.update!(item_type: 'Container')
+ end
+
+ it 'sets resets the destination to nil' do
+ expect(i_c.destination).to be nil
+ end
+ end
+
describe '#build_path' do
let(:product) { create(:product) }
let(:taxon) { create(:taxon) }
@@ -64,7 +113,7 @@ describe Spree::MenuItem, type: :model do
end
end
- describe 'paremeterize_code' do
+ describe '.paremeterize_code' do
let(:item) { 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 'paramatizes a code when one is given' do
@@ -72,7 +121,7 @@ describe Spree::MenuItem, type: :model do
end
end
- describe 'check_for_root' do
+ 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
end
diff --git a/core/spec/models/spree/menu_spec.rb b/core/spec/models/spree/menu_spec.rb
index 5e99339589..abcca6a214 100644
--- a/core/spec/models/spree/menu_spec.rb
+++ b/core/spec/models/spree/menu_spec.rb
@@ -15,8 +15,8 @@ describe Spree::Menu, type: :model do
end
describe '.by_unique_code' do
- let!(:menu_1) { create(:menu, name: 'Footer') }
- let!(:menu_2) { create(:menu, name: 'Header') }
+ let!(:menu_1) { create(:menu) }
+ let!(:menu_2) { create(:menu) }
it 'returns a menu when searched for by name' do
expect(described_class.by_unique_code(menu_1.unique_code)).to include(menu_1)
@@ -24,11 +24,27 @@ describe Spree::Menu, type: :model do
end
end
- describe 'validates uniqueness of unique_code' do
+ describe 'creating new menu' do
let!(:menu_1) { create(:menu, name: 'Footer', unique_code: 'ABC123') }
+ let!(:menu_param) { create(:menu, name: 'Footer', unique_code: 'ABC 123 XyZ') }
- it 'returns uniqueness error' do
+ it 'validates uniqueness of unique_code' do
expect(described_class.new(name: 'Footer', unique_code: 'ABC123')).not_to be_valid
end
+
+ it 'validates presence of name' do
+ expect(described_class.new(name: '', unique_code: 'ABC123')).not_to be_valid
+ end
+
+ it 'adds a root item' do
+ expect(menu_1.root.name).to eql('Footer')
+ expect(menu_1.root.code).to eql('footer-root')
+ expect(menu_1.root.root?).to be true
+ expect(menu_1.root.item_type).to eql('Container')
+ end
+
+ it 'paremeterizes the unique_code' do
+ expect(menu_param.unique_code).to eql('abc-123-xyz')
+ end
end
end