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

KacperMekarski8ee09a772f40
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
index e319be8405..e8986e94ce 100644
--- 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
@@ -3,55 +3,77 @@ require 'spec_helper'
describe Spree::Api::V2::Platform::MenuItemSerializer do
subject { described_class.new(menu_item) }
- let(:menu_item) { create(:menu_item) }
+ let(:menu) { create(:menu) }
+ let(:menu_item) { create(:menu_item, menu: menu, linked_resource: create(:taxon)) }
+ let!(:children) do
+ [
+ create(:menu_item, parent_id: menu_item.id, menu: menu),
+ create(:menu_item, parent_id: menu_item.id, menu: menu)
+ ]
+ end
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: []
- }
- }
- }
- }
- )
+ {
+ 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: {
+ id: menu_item.icon.id.to_s,
+ type: :image
+ }
+ },
+ 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: {
+ id: menu_item.linked_resource.id.to_s,
+ type: :taxon
+ }
+ },
+ children: {
+ data: [
+ {
+ id: menu_item.children.first.id.to_s,
+ type: :menu_item
+ },
+ {
+ id: menu_item.children.second.id.to_s,
+ type: :menu_item
+ }
+ ]
+ }
+ }
+ }
+ }
+ )
end
end
diff --git a/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb
new file mode 100644
index 0000000000..a7f075c455
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb
@@ -0,0 +1,109 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::ProductSerializer do
+ subject { described_class.new(product) }
+
+ let!(:images) { create_list(:image, 2) }
+ let(:product) do
+ create(:product,
+ variants_including_master: [create(:variant, images: images), create(:variant)],
+ option_types: create_list(:option_type, 2),
+ product_properties: create_list(:product_property, 2),
+ taxons: create_list(:taxon, 2))
+ end
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: product.id.to_s,
+ type: :product,
+ attributes: {
+ name: product.name,
+ description: product.description,
+ available_on: product.available_on,
+ deleted_at: product.deleted_at,
+ slug: product.slug,
+ meta_description: product.meta_description,
+ meta_keywords: product.meta_keywords,
+ created_at: product.created_at,
+ updated_at: product.updated_at,
+ promotionable: product.promotionable,
+ meta_title: product.meta_title,
+ discontinue_on: product.discontinue_on
+ },
+ relationships: {
+ default_variant: {
+ data: {
+ id: product.default_variant.id.to_s,
+ type: :variant
+ }
+ },
+ variants: {
+ data: [
+ {
+ id: product.variants.first.id.to_s,
+ type: :variant
+ },
+ {
+ id: product.variants.second.id.to_s,
+ type: :variant
+ }
+ ]
+ },
+ option_types: {
+ data: [
+ {
+ id: product.option_types.first.id.to_s,
+ type: :option_type
+ },
+ {
+ id: product.option_types.second.id.to_s,
+ type: :option_type
+ }
+ ]
+ },
+ product_properties: {
+ data: [
+ {
+ id: product.product_properties.first.id.to_s,
+ type: :product_property
+ },
+ {
+ id: product.product_properties.second.id.to_s,
+ type: :product_property
+ }
+ ]
+ },
+ taxons: {
+ data: [
+ {
+ id: product.taxons.first.id.to_s,
+ type: :taxon
+ },
+ {
+ id: product.taxons.second.id.to_s,
+ type: :taxon
+ }
+ ]
+ },
+ images: {
+ data: [
+ {
+ id: product.variant_images.first.id.to_s,
+ type: :image
+ },
+ {
+ id: product.variant_images.second.id.to_s,
+ type: :image
+ }
+ ]
+ }
+ }
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
new file mode 100644
index 0000000000..d97baf6289
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
@@ -0,0 +1,84 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::TaxonSerializer do
+ subject { described_class.new(taxon) }
+
+ let(:taxon) { create(:taxon, products: create_list(:product, 2)) }
+ let!(:children) { [create(:taxon, parent_id: taxon.id), create(:taxon, parent_id: taxon.id)] }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: taxon.id.to_s,
+ type: :taxon,
+ attributes: {
+ position: taxon.position,
+ name: taxon.name,
+ permalink: taxon.permalink,
+ lft: taxon.lft,
+ rgt: taxon.rgt,
+ description: taxon.description,
+ created_at: taxon.created_at,
+ updated_at: taxon.updated_at,
+ meta_title: taxon.meta_title,
+ meta_description: taxon.meta_description,
+ meta_keywords: taxon.meta_keywords,
+ depth: taxon.depth,
+ pretty_name: taxon.pretty_name,
+ seo_title: taxon.seo_title,
+ is_root: taxon.root?,
+ is_child: taxon.child?,
+ is_leaf: taxon.leaf?
+ },
+ relationships: {
+ parent: {
+ data: {
+ id: taxon.parent.id.to_s,
+ type: :taxon
+ }
+ },
+ taxonomy: {
+ data: {
+ id: taxon.taxonomy.id.to_s,
+ type: :taxonomy
+ }
+ },
+ image: {
+ data: {
+ id: taxon.icon.id.to_s,
+ type: :taxon_image
+ }
+ },
+ products: {
+ data: [
+ {
+ id: taxon.products.first.id.to_s,
+ type: :product
+ },
+ {
+ id: taxon.products.second.id.to_s,
+ type: :product
+ }
+ ]
+ },
+ children: {
+ data: [
+ {
+ id: taxon.children.first.id.to_s,
+ type: :taxon
+ },
+ {
+ id: taxon.children.second.id.to_s,
+ type: :taxon
+ }
+ ]
+ }
+ }
+ }
+ }
+ )
+ end
+end
diff --git a/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
new file mode 100644
index 0000000000..953d4a9e29
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
@@ -0,0 +1,39 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::UserSerializer do
+ subject { described_class.new(user) }
+
+ let(:user) { create(:user_with_addresses) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ it do
+ expect(subject.serializable_hash).to eq(
+ {
+ data: {
+ id: user.id.to_s,
+ type: :user,
+ attributes: {
+ email: user.email,
+ created_at: user.created_at,
+ updated_at: user.updated_at
+ },
+ relationships: {
+ bill_address: {
+ data: {
+ id: user.bill_address.id.to_s,
+ type: :address
+ }
+ },
+ ship_address: {
+ data: {
+ id: user.ship_address.id.to_s,
+ type: :address
+ }
+ }
+ }
+ }
+ }
+ )
+ 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
index b2419c68e8..530725fdac 100644
--- a/api/spec/serializers/spree/api/v2/platform/variant_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/variant_serializer_spec.rb
@@ -3,7 +3,7 @@ require 'spec_helper'
describe Spree::Api::V2::Platform::VariantSerializer do
subject { described_class.new(variant) }
- let(:variant) { create(:variant) }
+ let(:variant) { create(:variant, images: create_list(:image, 2)) }
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
@@ -14,11 +14,11 @@ describe Spree::Api::V2::Platform::VariantSerializer do
id: variant.id.to_s,
type: :variant,
attributes: {
- sku: variant.email,
- weight: variant.created_at,
- height: variant.updated_at,
- depth: variant.,
- deleted_at: variant.,
+ sku: variant.sku,
+ weight: variant.weight,
+ height: variant.height,
+ depth: variant.depth,
+ deleted_at: variant.deleted_at,
is_master: variant.is_master,
cost_price: variant.cost_price,
position: variant.position,
@@ -29,17 +29,31 @@ describe Spree::Api::V2::Platform::VariantSerializer do
discontinue_on: variant.discontinue_on
},
relationships: {
- bill_address: {
+ product: {
data: {
- id: variant.bill_address.id.to_s,
- type: :address
+ id: variant.product.id.to_s,
+ type: :product
}
},
- ship_address: {
- data: {
- id: variant.ship_address.id.to_s,
- type: :address
- }
+ option_values: {
+ data: [
+ {
+ id: variant.option_values.first.id.to_s,
+ type: :option_value
+ }
+ ]
+ },
+ images: {
+ data: [
+ {
+ id: variant.images.first.id.to_s,
+ type: :image
+ },
+ {
+ id: variant.images.second.id.to_s,
+ type: :image
+ }
+ ]
}
}
}
diff --git a/core/lib/spree/testing_support/factories/icon_factory.rb b/core/lib/spree/testing_support/factories/icon_factory.rb
new file mode 100644
index 0000000000..049043ff47
--- /dev/null
+++ b/core/lib/spree/testing_support/factories/icon_factory.rb
@@ -0,0 +1,7 @@
+FactoryBot.define do
+ factory :icon, class: Spree::Icon do
+ before(:create) do |icon|
+ icon.attachment.attach(io: File.new(Spree::Core::Engine.root + 'spec/fixtures' + 'thinking-cat.jpg'), filename: 'thinking-cat.jpg')
+ end
+ end
+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 34e541d89f..9171412422 100644
--- a/core/lib/spree/testing_support/factories/menu_item_factory.rb
+++ b/core/lib/spree/testing_support/factories/menu_item_factory.rb
@@ -1,9 +1,10 @@
FactoryBot.define do
factory :menu_item, class: Spree::MenuItem do
- name { 'Link To Somewhere' }
+ sequence(:name) { |n| "Link no. #{n} To Somewhere" }
item_type { 'Link' }
linked_resource_type { 'URL' }
menu
+ icon
end
end
diff --git a/core/lib/spree/testing_support/factories/taxon_factory.rb b/core/lib/spree/testing_support/factories/taxon_factory.rb
index 5b63739c3f..f33c944f77 100644
--- a/core/lib/spree/testing_support/factories/taxon_factory.rb
+++ b/core/lib/spree/testing_support/factories/taxon_factory.rb
@@ -1,7 +1,9 @@
FactoryBot.define do
factory :taxon, class: Spree::Taxon do
sequence(:name) { |n| "taxon_#{n}" }
- association(:taxonomy, strategy: :create)
+
+ association :taxonomy, strategy: :create
+ association :icon, factory: :taxon_image
parent_id { taxonomy.root.id }
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
index 75ce83e569..202f9cb79b 100644
--- a/core/lib/spree/testing_support/factories/taxon_image_factory.rb
+++ b/core/lib/spree/testing_support/factories/taxon_image_factory.rb
@@ -1,7 +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')
+ taxon_image.attachment.attach(io: File.new(Spree::Core::Engine.root + 'spec/fixtures/thinking-cat.jpg'), filename: 'thinking-cat.jpg')
end
end
end