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 model tests for each section type

Matthew Kennedy302c620043f8
diff --git a/core/app/models/spree/cms/sections/featured_article.rb b/core/app/models/spree/cms/sections/featured_article.rb
index fdf81e45c9..a690056a01 100644
--- a/core/app/models/spree/cms/sections/featured_article.rb
+++ b/core/app/models/spree/cms/sections/featured_article.rb
@@ -12,6 +12,10 @@ module Spree::Cms::Sections
gutters == 'Gutters'
end
+ def screen?
+ fit == 'Screen'
+ end
+
private
def reset_link_attributes
diff --git a/core/app/models/spree/cms/sections/hero_image.rb b/core/app/models/spree/cms/sections/hero_image.rb
index a0a1522830..861e0c40a6 100644
--- a/core/app/models/spree/cms/sections/hero_image.rb
+++ b/core/app/models/spree/cms/sections/hero_image.rb
@@ -12,6 +12,10 @@ module Spree::Cms::Sections
gutters == 'Gutters'
end
+ def screen?
+ fit == 'Screen'
+ end
+
private
def reset_link_attributes
diff --git a/core/app/models/spree/cms/sections/image_gallery.rb b/core/app/models/spree/cms/sections/image_gallery.rb
index b2d60c7485..b8d6b0c457 100644
--- a/core/app/models/spree/cms/sections/image_gallery.rb
+++ b/core/app/models/spree/cms/sections/image_gallery.rb
@@ -12,6 +12,14 @@ module Spree::Cms::Sections
store :settings, accessors: [:layout_style, :display_labels], coder: JSON
+ def screen?
+ fit == 'Screen'
+ end
+
+ def default_layout?
+ layout_style == 'Default'
+ end
+
private
def default_values
diff --git a/core/app/models/spree/cms/sections/product_carousel.rb b/core/app/models/spree/cms/sections/product_carousel.rb
index 4f12945856..59be6f98eb 100644
--- a/core/app/models/spree/cms/sections/product_carousel.rb
+++ b/core/app/models/spree/cms/sections/product_carousel.rb
@@ -4,6 +4,10 @@ module Spree::Cms::Sections
LINKED_RESOURCE_TYPE = ['Spree::Taxon']
+ def screen?
+ fit == 'Screen'
+ end
+
private
def default_values
diff --git a/core/app/models/spree/cms/sections/side_by_side_images.rb b/core/app/models/spree/cms/sections/side_by_side_images.rb
index 4b5b2cd617..1533199fd7 100644
--- a/core/app/models/spree/cms/sections/side_by_side_images.rb
+++ b/core/app/models/spree/cms/sections/side_by_side_images.rb
@@ -14,6 +14,10 @@ module Spree::Cms::Sections
gutters == 'Gutters'
end
+ def screen?
+ fit == 'Screen'
+ end
+
private
def reset_link_attributes
diff --git a/core/lib/spree/testing_support/factories/cms_section_factory.rb b/core/lib/spree/testing_support/factories/cms_section_factory.rb
index 2fe0ab74d8..8e5e6193aa 100644
--- a/core/lib/spree/testing_support/factories/cms_section_factory.rb
+++ b/core/lib/spree/testing_support/factories/cms_section_factory.rb
@@ -2,7 +2,7 @@ FactoryBot.define do
factory :cms_section, class: Spree::CmsSection do
name { generate(:random_string) }
- factory :cms_hero_section do
+ factory :cms_hero_image_section do
type { 'Spree::Cms::Sections::HeroImage' }
end
@@ -14,12 +14,16 @@ FactoryBot.define do
type { 'Spree::Cms::Sections::ProductCarousel' }
end
- factory :cms_three_taxon_categories_section do
+ factory :cms_image_gallery_section do
type { 'Spree::Cms::Sections::ImageGallery' }
end
- factory :cms_side_by_side_promotion_section do
+ factory :cms_side_by_side_images_section do
type { 'Spree::Cms::Sections::SideBySideImages' }
end
+
+ factory :cms_rich_text_content_section do
+ type { 'Spree::Cms::Sections::RichTextContent' }
+ end
end
end
diff --git a/core/spec/models/spree/cms/sections/featured_article_spec.rb b/core/spec/models/spree/cms/sections/featured_article_spec.rb
new file mode 100644
index 0000000000..7fd5020e5c
--- /dev/null
+++ b/core/spec/models/spree/cms/sections/featured_article_spec.rb
@@ -0,0 +1,62 @@
+require 'spec_helper'
+
+describe Spree::Cms::Sections::FeaturedArticle, type: :model do
+ let!(:store) { create(:store) }
+ let!(:homepage) { create(:cms_homepage, store: store) }
+
+ it 'validates presence of name' do
+ expect(described_class.new(name: nil, cms_page: homepage)).not_to be_valid
+ end
+
+ it 'validates presence of page' do
+ expect(described_class.new(name: 'Got Name')).not_to be_valid
+ end
+
+ context 'when a new Featured Article section is created' do
+ let!(:featured_article_section) { create(:cms_featured_article_section, cms_page: homepage) }
+
+ it 'sets gutters to No Gutters' do
+ section = Spree::CmsSection.find(featured_article_section.id)
+
+ expect(section.settings[:gutters]).to eq('No Gutters')
+ end
+
+ it 'sets fit to Screen' do
+ section = Spree::CmsSection.find(featured_article_section.id)
+
+ expect(section.fit).to eq('Screen')
+ end
+
+ it 'sets linked_resource_type to Spree::Taxon' do
+ section = Spree::CmsSection.find(featured_article_section.id)
+
+ expect(section.linked_resource_type).to eq('Spree::Taxon')
+ end
+
+ it '#gutters? is false' do
+ section = Spree::CmsSection.find(featured_article_section.id)
+
+ expect(section.gutters?).to be false
+ end
+
+ it '#screen? is true' do
+ section = Spree::CmsSection.find(featured_article_section.id)
+
+ expect(section.screen?).to be true
+ end
+ end
+
+ context 'when changing the link type' do
+ let!(:featured_article_section) { create(:cms_featured_article_section, cms_page: homepage) }
+
+ before do
+ featured_article_section.update!(linked_resource_type: 'Spree::Product')
+ end
+
+ it 'resets the linked resource to nil' do
+ section = Spree::CmsSection.find(featured_article_section.id)
+
+ expect(section.linked_resource_id).to be nil
+ end
+ end
+end
diff --git a/core/spec/models/spree/cms/sections/hero_image_spec.rb b/core/spec/models/spree/cms/sections/hero_image_spec.rb
new file mode 100644
index 0000000000..73c61da720
--- /dev/null
+++ b/core/spec/models/spree/cms/sections/hero_image_spec.rb
@@ -0,0 +1,62 @@
+require 'spec_helper'
+
+describe Spree::Cms::Sections::HeroImage, type: :model do
+ let!(:store) { create(:store) }
+ let!(:homepage) { create(:cms_homepage, store: store) }
+
+ it 'validates presence of name' do
+ expect(described_class.new(name: nil, cms_page: homepage)).not_to be_valid
+ end
+
+ it 'validates presence of page' do
+ expect(described_class.new(name: 'Got Name')).not_to be_valid
+ end
+
+ context 'when a new Hero Image section is created' do
+ let!(:hero_section) { create(:cms_hero_image_section, cms_page: homepage) }
+
+ it 'sets gutters to No Gutters' do
+ section = Spree::CmsSection.find(hero_section.id)
+
+ expect(section.settings[:gutters]).to eq('No Gutters')
+ end
+
+ it 'sets fit to Screen' do
+ section = Spree::CmsSection.find(hero_section.id)
+
+ expect(section.fit).to eq('Screen')
+ end
+
+ it 'sets linked_resource_type to Spree::Taxon' do
+ section = Spree::CmsSection.find(hero_section.id)
+
+ expect(section.linked_resource_type).to eq('Spree::Taxon')
+ end
+
+ it '#gutters? is false' do
+ section = Spree::CmsSection.find(hero_section.id)
+
+ expect(section.gutters?).to be false
+ end
+
+ it '#screen? is true' do
+ section = Spree::CmsSection.find(hero_section.id)
+
+ expect(section.screen?).to be true
+ end
+ end
+
+ context 'when changing the link type' do
+ let!(:hero_section) { create(:cms_hero_image_section, cms_page: homepage) }
+
+ before do
+ hero_section.update!(linked_resource_type: 'Spree::Product')
+ end
+
+ it 'resets the linked resource to nil' do
+ section = Spree::CmsSection.find(hero_section.id)
+
+ expect(section.linked_resource_id).to be nil
+ end
+ end
+end
diff --git a/core/spec/models/spree/cms/sections/image_gallery_spec.rb b/core/spec/models/spree/cms/sections/image_gallery_spec.rb
new file mode 100644
index 0000000000..277d7301f9
--- /dev/null
+++ b/core/spec/models/spree/cms/sections/image_gallery_spec.rb
@@ -0,0 +1,82 @@
+require 'spec_helper'
+
+describe Spree::Cms::Sections::ImageGallery, type: :model do
+ let!(:store) { create(:store) }
+ let!(:homepage) { create(:cms_homepage, store: store) }
+
+ it 'validates presence of name' do
+ expect(described_class.new(name: nil, cms_page: homepage)).not_to be_valid
+ end
+
+ it 'validates presence of page' do
+ expect(described_class.new(name: 'Got Name')).not_to be_valid
+ end
+
+ context 'when a new Image Gallery section is created' do
+ let!(:image_gallery_section) { create(:cms_image_gallery_section, cms_page: homepage) }
+
+ it 'sets layout_style to Default' do
+ section = Spree::CmsSection.find(image_gallery_section.id)
+
+ expect(section.settings[:layout_style]).to eq('Default')
+ end
+
+ it 'sets link_type_one to Spree::Taxon' do
+ section = Spree::CmsSection.find(image_gallery_section.id)
+
+ expect(section.content[:link_type_one]).to eq('Spree::Taxon')
+ end
+
+ it 'sets link_type_two to Spree::Taxon' do
+ section = Spree::CmsSection.find(image_gallery_section.id)
+
+ expect(section.content[:link_type_two]).to eq('Spree::Taxon')
+ end
+
+ it 'sets link_type_three to Spree::Taxon' do
+ section = Spree::CmsSection.find(image_gallery_section.id)
+
+ expect(section.content[:link_type_three]).to eq('Spree::Taxon')
+ end
+
+ it 'sets fit to Container' do
+ section = Spree::CmsSection.find(image_gallery_section.id)
+
+ expect(section.fit).to eq('Container')
+ end
+
+ it '#screen? is true' do
+ section = Spree::CmsSection.find(image_gallery_section.id)
+
+ expect(section.screen?).to be false
+ end
+
+ it '#default_layout? is true' do
+ section = Spree::CmsSection.find(image_gallery_section.id)
+
+ expect(section.default_layout?).to be true
+ end
+ end
+
+ context 'when changing the link types for links one two and three' do
+ let!(:image_gallery_section) { create(:cms_image_gallery_section, cms_page: homepage) }
+
+ before do
+ section = Spree::CmsSection.find(image_gallery_section.id)
+
+ section.content[:link_type_one] = 'Spree::Product'
+ section.content[:link_type_two] = 'Spree::Product'
+ section.content[:link_type_three] = 'Spree::Product'
+ section.save!
+ section.reload
+ end
+
+ it 'link_one, link_two and link_three are reset to nil' do
+ section = Spree::CmsSection.find(image_gallery_section.id)
+
+ expect(section.link_one).to be nil
+ expect(section.link_two).to be nil
+ expect(section.link_three).to be nil
+ end
+ end
+end
diff --git a/core/spec/models/spree/cms/sections/product_carousel_spec.rb b/core/spec/models/spree/cms/sections/product_carousel_spec.rb
new file mode 100644
index 0000000000..8617d964d6
--- /dev/null
+++ b/core/spec/models/spree/cms/sections/product_carousel_spec.rb
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+describe Spree::Cms::Sections::ProductCarousel, type: :model do
+ let!(:store) { create(:store) }
+ let!(:homepage) { create(:cms_homepage, store: store) }
+
+ it 'validates presence of name' do
+ expect(described_class.new(name: nil, cms_page: homepage)).not_to be_valid
+ end
+
+ it 'validates presence of page' do
+ expect(described_class.new(name: 'Got Name')).not_to be_valid
+ end
+
+ context 'when a new Product Carousel section is created' do
+ let!(:product_carousel_section) { create(:cms_product_carousel_section, cms_page: homepage) }
+
+ it 'sets fit to Screen' do
+ section = Spree::CmsSection.find(product_carousel_section.id)
+
+ expect(section.fit).to eq('Screen')
+ end
+
+ it 'sets linked_resource_type to Spree::Taxon' do
+ section = Spree::CmsSection.find(product_carousel_section.id)
+
+ expect(section.linked_resource_type).to eq('Spree::Taxon')
+ end
+ end
+end
diff --git a/core/spec/models/spree/cms/sections/rich_text_content_spec.rb b/core/spec/models/spree/cms/sections/rich_text_content_spec.rb
new file mode 100644
index 0000000000..a8a927a892
--- /dev/null
+++ b/core/spec/models/spree/cms/sections/rich_text_content_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe Spree::Cms::Sections::RichTextContent, type: :model do
+ let!(:store) { create(:store) }
+ let!(:homepage) { create(:cms_homepage, store: store) }
+
+ it 'validates presence of name' do
+ expect(described_class.new(name: nil, cms_page: homepage)).not_to be_valid
+ end
+
+ it 'validates presence of page' do
+ expect(described_class.new(name: 'Got Name')).not_to be_valid
+ end
+
+ context 'when a new Rich Text Content section is created' do
+ let!(:rich_text_content_section) { create(:cms_rich_text_content_section, cms_page: homepage) }
+
+ it 'sets fit to Screen' do
+ section = Spree::CmsSection.find(rich_text_content_section.id)
+
+ expect(section.fit).to eq('Container')
+ end
+ end
+end
diff --git a/core/spec/models/spree/cms/sections/side_by_side_images_spec.rb b/core/spec/models/spree/cms/sections/side_by_side_images_spec.rb
new file mode 100644
index 0000000000..8aad99c336
--- /dev/null
+++ b/core/spec/models/spree/cms/sections/side_by_side_images_spec.rb
@@ -0,0 +1,68 @@
+require 'spec_helper'
+
+describe Spree::Cms::Sections::SideBySideImages, type: :model do
+ let!(:store) { create(:store) }
+ let!(:homepage) { create(:cms_homepage, store: store) }
+
+ it 'validates presence of name' do
+ expect(described_class.new(name: nil, cms_page: homepage)).not_to be_valid
+ end
+
+ it 'validates presence of page' do
+ expect(described_class.new(name: 'Got Name')).not_to be_valid
+ end
+
+ context 'when a new Image Gallery section is created' do
+ let!(:side_by_side_images_section) { create(:cms_side_by_side_images_section, cms_page: homepage) }
+
+ it 'sets link_type_one to Spree::Taxon' do
+ section = Spree::CmsSection.find(side_by_side_images_section.id)
+
+ expect(section.content[:link_type_one]).to eq('Spree::Taxon')
+ end
+
+ it 'sets link_type_two to Spree::Taxon' do
+ section = Spree::CmsSection.find(side_by_side_images_section.id)
+
+ expect(section.content[:link_type_two]).to eq('Spree::Taxon')
+ end
+
+ it 'sets fit to Container' do
+ section = Spree::CmsSection.find(side_by_side_images_section.id)
+
+ expect(section.fit).to eq('Container')
+ end
+
+ it '#screen? is true' do
+ section = Spree::CmsSection.find(side_by_side_images_section.id)
+
+ expect(section.screen?).to be false
+ end
+
+ it '#gutters? is true' do
+ section = Spree::CmsSection.find(side_by_side_images_section.id)
+
+ expect(section.gutters?).to be true
+ end
+ end
+
+ context 'when changing the link types for links one and two' do
+ let!(:side_by_side_images_section) { create(:cms_side_by_side_images_section, cms_page: homepage) }
+
+ before do
+ section = Spree::CmsSection.find(side_by_side_images_section.id)
+
+ section.content[:link_type_one] = 'Spree::Product'
+ section.content[:link_type_two] = 'Spree::Product'
+ section.save!
+ section.reload
+ end
+
+ it 'link_one and link_two are reset to nil' do
+ section = Spree::CmsSection.find(side_by_side_images_section.id)
+
+ expect(section.link_one).to be nil
+ expect(section.link_two).to be nil
+ end
+ end
+end