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.

SD-1389 add platform serializers tests

KacperMekarski228b2fb0e1f0
diff --git a/api/spec/serializers/spree/api/v2/platform/address_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/address_serializer_spec.rb
new file mode 100644
index 0000000000..9366d0451c
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/address_serializer_spec.rb
@@ -0,0 +1,56 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::AddressSerializer do
+ subject { described_class.new(address) }
+
+ let(:address) { create(:address, user: create(:user)) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: address.id.to_s,
+ type: :address,
+ attributes: {
+ firstname: address.firstname,
+ lastname: address.lastname,
+ address1: address.address1,
+ address2: address.address2,
+ city: address.city,
+ zipcode: address.zipcode,
+ phone: address.phone,
+ state_name: address.state_name,
+ alternative_phone: address.alternative_phone,
+ company: address.company,
+ created_at: address.created_at,
+ updated_at: address.updated_at,
+ deleted_at: address.deleted_at,
+ label: address.label,
+ },
+ relationships: {
+ country: {
+ data: {
+ id: address.country.id.to_s,
+ type: :country
+ }
+ },
+ state: {
+ data: {
+ id: address.state.id.to_s,
+ type: :state
+ }
+ },
+ user: {
+ data: {
+ id: address.user.id.to_s,
+ type: :user
+ }
+ }
+ }
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/cms_page_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/cms_page_serializer_spec.rb
new file mode 100644
index 0000000000..0e28218f1f
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/cms_page_serializer_spec.rb
@@ -0,0 +1,47 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::CmsPageSerializer do
+ subject { described_class.new(cms_page) }
+
+ let(:cms_page) { create(:base_cms_page, cms_sections: create_list(:cms_section, 2)) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: cms_page.id.to_s,
+ type: :cms_page,
+ attributes: {
+ title: cms_page.title,
+ meta_title: cms_page.meta_title,
+ content: cms_page.content,
+ meta_description: cms_page.meta_description,
+ visible: cms_page.visible,
+ slug: cms_page.slug,
+ type: cms_page.type,
+ locale: cms_page.locale,
+ deleted_at: cms_page.deleted_at,
+ created_at: cms_page.created_at,
+ updated_at: cms_page.updated_at
+ },
+ relationships: {
+ cms_sections: {
+ data: [
+ {
+ id: cms_page.cms_sections.first.id.to_s,
+ type: :cms_section
+ },
+ {
+ id: cms_page.cms_sections.second.id.to_s,
+ type: :cms_section
+ }
+ ]
+ }
+ }
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/cms_section_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/cms_section_serializer_spec.rb
new file mode 100644
index 0000000000..dd12d53944
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/cms_section_serializer_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::CmsSectionSerializer do
+ subject { described_class.new(cms_section) }
+
+ let(:cms_section) { create(:cms_section) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: cms_section.id.to_s,
+ type: :cms_section,
+ attributes: {
+ name: cms_section.name,
+ content: cms_section.content,
+ settings: cms_section.settings,
+ fit: cms_section.fit,
+ destination: cms_section.destination,
+ type: cms_section.type,
+ position: cms_section.position,
+ linked_resource_type: cms_section.linked_resource_type,
+ created_at: cms_section.created_at,
+ updated_at: cms_section.updated_at
+ },
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/country_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/country_serializer_spec.rb
new file mode 100644
index 0000000000..ba16cdef6b
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/country_serializer_spec.rb
@@ -0,0 +1,44 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::CountrySerializer do
+ subject { described_class.new(country) }
+
+ let(:country) { create(:country, states: create_list(:state, 2)) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: country.id.to_s,
+ type: :country,
+ attributes: {
+ iso_name: country.iso_name,
+ iso: country.iso,
+ iso3: country.iso3,
+ name: country.name,
+ numcode: country.numcode,
+ states_required: country.states_required,
+ updated_at: country.updated_at,
+ zipcode_required: country.zipcode_required
+ },
+ relationships: {
+ states: {
+ data: [
+ {
+ id: country.states.first.id.to_s,
+ type: :state
+ },
+ {
+ id: country.states.second.id.to_s,
+ type: :state
+ }
+ ]
+ }
+ },
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/image_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/image_serializer_spec.rb
new file mode 100644
index 0000000000..73f169ddb2
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/image_serializer_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::ImageSerializer do
+ subject { described_class.new(image) }
+
+ let(:image) { create(:image) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: image.id.to_s,
+ type: :image,
+ attributes: {
+ viewable_type: image.viewable_type,
+ attachment_height: image.attachment_height,
+ attachment_file_size: image.attachment_file_size,
+ position: image.position,
+ attachment_content_type: image.attachment_content_type,
+ attachment_file_name: image.attachment_file_name,
+ type: image.type,
+ attachment_updated_at: image.attachment_updated_at,
+ alt: image.alt,
+ created_at: image.created_at,
+ updated_at: image.updated_at
+ },
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/menu_item_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/menu_item_serializer_spec.rb
new file mode 100644
index 0000000000..e319be8405
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/menu_item_serializer_spec.rb
@@ -0,0 +1,57 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::MenuItemSerializer do
+ subject { described_class.new(menu_item) }
+
+ let(:menu_item) { create(:menu_item) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: menu_item.id.to_s,
+ type: :menu_item,
+ attributes: {
+ name: menu_item.name,
+ subtitle: menu_item.subtitle,
+ destination: menu_item.destination,
+ new_window: menu_item.new_window,
+ item_type: menu_item.item_type,
+ linked_resource_type: menu_item.linked_resource_type,
+ code: menu_item.code,
+ lft: menu_item.lft,
+ rgt: menu_item.rgt,
+ depth: menu_item.depth,
+ created_at: menu_item.created_at,
+ updated_at: menu_item.updated_at
+ },
+ relationships: {
+ image: {
+ data: nil
+ },
+ menu: {
+ data: {
+ id: menu_item.menu.id.to_s,
+ type: :menu
+ }
+ },
+ parent: {
+ data: {
+ id: menu_item.menu.root.id.to_s,
+ type: :menu_item
+ }
+ },
+ linked_resource: {
+ data: nil
+ },
+ children: {
+ data: []
+ }
+ }
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/menu_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/menu_serializer_spec.rb
new file mode 100644
index 0000000000..a4b0728336
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/menu_serializer_spec.rb
@@ -0,0 +1,37 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::MenuSerializer do
+ subject { described_class.new(menu) }
+
+ let(:menu) { create(:menu) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: menu.id.to_s,
+ type: :menu,
+ attributes: {
+ name: menu.name,
+ location: menu.location,
+ locale: menu.locale,
+ created_at: menu.created_at,
+ updated_at: menu.updated_at
+ },
+ relationships: {
+ menu_items: {
+ data: [
+ {
+ id: menu.menu_items.first.id.to_s,
+ type: :menu_item
+ }
+ ]
+ }
+ }
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/option_type_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/option_type_serializer_spec.rb
new file mode 100644
index 0000000000..1f42ec7751
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/option_type_serializer_spec.rb
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::OptionTypeSerializer do
+ subject { described_class.new(option_type) }
+
+ let(:option_type) { create(:option_type, option_values: create_list(:option_value, 2)) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: option_type.id.to_s,
+ type: :option_type,
+ attributes: {
+ name: option_type.name,
+ presentation: option_type.presentation,
+ position: option_type.position,
+ created_at: option_type.created_at,
+ updated_at: option_type.updated_at,
+ filterable: option_type.filterable
+ },
+ relationships: {
+ option_values: {
+ data: [
+ {
+ id: option_type.option_values.first.id.to_s,
+ type: :option_value
+ },
+ {
+ id: option_type.option_values.second.id.to_s,
+ type: :option_value
+ }
+ ]
+ }
+ }
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/option_value_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/option_value_serializer_spec.rb
new file mode 100644
index 0000000000..278396ba25
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/option_value_serializer_spec.rb
@@ -0,0 +1,35 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::OptionValueSerializer do
+ subject { described_class.new(option_value) }
+
+ let(:option_value) { create(:option_value) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: option_value.id.to_s,
+ type: :option_value,
+ attributes: {
+ position: option_value.position,
+ name: option_value.name,
+ presentation: option_value.presentation,
+ created_at: option_value.created_at,
+ updated_at: option_value.updated_at
+ },
+ relationships: {
+ option_type: {
+ data: {
+ id: option_value.option_type.id.to_s,
+ type: :option_type
+ }
+ },
+ }
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/product_property_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/product_property_serializer_spec.rb
new file mode 100644
index 0000000000..e9fbe9c833
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/product_property_serializer_spec.rb
@@ -0,0 +1,28 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::ProductPropertySerializer do
+ subject { described_class.new(product_property) }
+
+ let(:product_property) { create(:product_property) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: product_property.id.to_s,
+ type: :product_property,
+ attributes: {
+ value: product_property.value,
+ position: product_property.position,
+ show_property: product_property.show_property,
+ filter_param: product_property.filter_param,
+ created_at: product_property.created_at,
+ updated_at: product_property.updated_at
+ }
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/state_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/state_serializer_spec.rb
new file mode 100644
index 0000000000..96756bf2e0
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/state_serializer_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::StateSerializer do
+ subject { described_class.new(state) }
+
+ let(:state) { create(:state) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: state.id.to_s,
+ type: :state,
+ attributes: {
+ name: state.name,
+ abbr: state.abbr,
+ updated_at: state.updated_at
+ },
+ relationships: {
+ country: {
+ data: {
+ id: state.country.id.to_s,
+ type: :country
+ }
+ },
+ },
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb
new file mode 100644
index 0000000000..3367ecf6d6
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb
@@ -0,0 +1,65 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::StoreSerializer do
+ subject { described_class.new(store) }
+
+ let(:store) { create(:store, menus: [create(:menu), create(:menu, location: 'Footer')]) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: store.id.to_s,
+ type: :store,
+ attributes: {
+ name: store.name,
+ url: store.url,
+ meta_description: store.meta_description,
+ meta_keywords: store.meta_keywords,
+ seo_title: store.seo_title,
+ mail_from_address: store.mail_from_address,
+ default_currency: store.default_currency,
+ code: store.code,
+ default: store.default,
+ created_at: store.created_at,
+ updated_at: store.updated_at,
+ supported_currencies: store.supported_currencies,
+ facebook: store.facebook,
+ twitter: store.twitter,
+ instagram: store.instagram,
+ default_locale: store.default_locale,
+ customer_support_email: store.customer_support_email,
+ description: store.description,
+ address: store.address,
+ contact_phone: store.contact_phone,
+ new_order_notifications_email: store.new_order_notifications_email,
+ seo_robots: store.seo_robots,
+ supported_locales: store.supported_locales
+ },
+ relationships: {
+ default_country: {
+ data: {
+ id: store.default_country.id.to_s,
+ type: :country
+ }
+ },
+ menus: {
+ data: [
+ {
+ id: store.menus.first.id.to_s,
+ type: :menu
+ },
+ {
+ id: store.menus.second.id.to_s,
+ type: :menu
+ }
+ ]
+ }
+ },
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/taxon_image_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/taxon_image_serializer_spec.rb
new file mode 100644
index 0000000000..ba93a8de91
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/taxon_image_serializer_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::TaxonImageSerializer do
+ subject { described_class.new(taxon_image) }
+
+ let(:taxon_image) { create(:taxon_image) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: taxon_image.id.to_s,
+ type: :taxon_image,
+ attributes: {
+ viewable_type: taxon_image.viewable_type,
+ attachment_height: taxon_image.attachment_height,
+ attachment_file_size: taxon_image.attachment_file_size,
+ position: taxon_image.position,
+ attachment_content_type: taxon_image.attachment_content_type,
+ attachment_file_name: taxon_image.attachment_file_name,
+ type: taxon_image.type,
+ attachment_updated_at: taxon_image.attachment_updated_at,
+ alt: taxon_image.alt,
+ created_at: taxon_image.created_at,
+ updated_at: taxon_image.updated_at
+ },
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/taxonomy_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/taxonomy_serializer_spec.rb
new file mode 100644
index 0000000000..214de58f67
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/taxonomy_serializer_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::TaxonomySerializer do
+ subject { described_class.new(taxonomy) }
+
+ let(:taxonomy) { create(:taxonomy) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: taxonomy.id.to_s,
+ type: :taxonomy,
+ attributes: {
+ name: taxonomy.name,
+ created_at: taxonomy.created_at,
+ updated_at: taxonomy.updated_at,
+ position: taxonomy.position
+ }
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/variant_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/variant_serializer_spec.rb
new file mode 100644
index 0000000000..b2419c68e8
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/variant_serializer_spec.rb
@@ -0,0 +1,49 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::VariantSerializer do
+ subject { described_class.new(variant) }
+
+ let(:variant) { create(:variant) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: variant.id.to_s,
+ type: :variant,
+ attributes: {
+ sku: variant.email,
+ weight: variant.created_at,
+ height: variant.updated_at,
+ depth: variant.,
+ deleted_at: variant.,
+ is_master: variant.is_master,
+ cost_price: variant.cost_price,
+ position: variant.position,
+ cost_currency: variant.cost_currency,
+ track_inventory: variant.track_inventory,
+ created_at: variant.created_at,
+ updated_at: variant.updated_at,
+ discontinue_on: variant.discontinue_on
+ },
+ relationships: {
+ bill_address: {
+ data: {
+ id: variant.bill_address.id.to_s,
+ type: :address
+ }
+ },
+ ship_address: {
+ data: {
+ id: variant.ship_address.id.to_s,
+ type: :address
+ }
+ }
+ }
+ }
+ }
+ )
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/cms_page_factory.rb b/core/lib/spree/testing_support/factories/cms_page_factory.rb
index de4e197663..dd9ce58436 100644
--- a/core/lib/spree/testing_support/factories/cms_page_factory.rb
+++ b/core/lib/spree/testing_support/factories/cms_page_factory.rb
@@ -3,6 +3,8 @@ FactoryBot.define do
title { generate(:random_string) }
locale { 'en' }
+ store
+
factory :cms_homepage do
type { 'Spree::Cms::Pages::Homepage' }
end
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 8e5e6193aa..907707563b 100644
--- a/core/lib/spree/testing_support/factories/cms_section_factory.rb
+++ b/core/lib/spree/testing_support/factories/cms_section_factory.rb
@@ -2,6 +2,8 @@ FactoryBot.define do
factory :cms_section, class: Spree::CmsSection do
name { generate(:random_string) }
+ association :cms_page, factory: :base_cms_page
+
factory :cms_hero_image_section do
type { 'Spree::Cms::Sections::HeroImage' }
end
diff --git a/core/lib/spree/testing_support/factories/menu_item_factory.rb b/core/lib/spree/testing_support/factories/menu_item_factory.rb
index 236b9fd030..34e541d89f 100644
--- a/core/lib/spree/testing_support/factories/menu_item_factory.rb
+++ b/core/lib/spree/testing_support/factories/menu_item_factory.rb
@@ -3,5 +3,7 @@ FactoryBot.define do
name { 'Link To Somewhere' }
item_type { 'Link' }
linked_resource_type { 'URL' }
+
+ menu
end
end
diff --git a/core/lib/spree/testing_support/factories/taxon_image_factory.rb b/core/lib/spree/testing_support/factories/taxon_image_factory.rb
new file mode 100644
index 0000000000..75ce83e569
--- /dev/null
+++ b/core/lib/spree/testing_support/factories/taxon_image_factory.rb
@@ -0,0 +1,7 @@
+FactoryBot.define do
+ factory :taxon_image, class: Spree::TaxonImage do
+ before(:create) do |taxon_image|
+ taxon_image.attachment.attach(io: File.new(Spree::Core::Engine.root + 'spec/fixtures' + 'thinking-cat.jpg'), filename: 'thinking-cat.jpg')
+ end
+ end
+end