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.

Update naming and handling of add_item

Matthew Kennedy0edf3df855c9
diff --git a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
index dcb52fd25b..0507271c3d 100644
--- a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
@@ -59,45 +59,52 @@ module Spree
render_serialized_payload { serialize_resource(@default_wishlist) }
end
- def add_wished_variant
- spree_authorize! :create, Spree::WishedVariant
+ def add_item
+ spree_authorize! :create, Spree::WishedItem
- if resource.wished_variants.present? && resource.wished_variants.detect { |wv| wv.variant_id == params[:variant_id] }.present?
- @wished_variant = resource.wished_variants.detect { |wv| wv.variant_id == params[:variant_id] }
+ if params[:quantity].present?
+ params[:quantity] = params[:quantity].to_i
else
- @wished_variant = Spree::WishedVariant.new(params.permit(:quantity, :variant_id))
- @wished_variant.wishlist = resource
- @wished_variant.save
+ params[:quantity] = 1
+ end
+
+ if resource.wished_items.present? && resource.wished_items.detect { |wv| wv.variant_id.to_s == params[:variant_id].to_s }.present?
+ @wished_item = resource.wished_items.detect { |wi| wi.variant_id.to_s == params[:variant_id].to_s }
+ @wished_item.quantity = params[:quantity]
+ else
+ @wished_item = Spree::WishedItem.new(params.permit(:quantity, :variant_id))
+ @wished_item.wishlist = resource
+ @wished_item.save
end
resource.reload
- if @wished_variant.persisted?
- render_serialized_payload { serialize_wished_variant(@wished_variant) }
+ if @wished_item.persisted?
+ render_serialized_payload { serialize_wished_item(@wished_item) }
else
render_error_payload(resource.errors.full_messages.to_sentence)
end
end
- def update_wished_variant_quantity
- spree_authorize! :update, wished_variant
+ def set_item_quantity
+ spree_authorize! :update, wished_item
return render_error_item_quantity unless params[:quantity].to_i > 0
- wished_variant.update(params.permit(:quantity))
+ wished_item.update(params.permit(:quantity))
- if wished_variant.errors.empty?
- render_serialized_payload { serialize_wished_variant(wished_variant) }
+ if wished_item.errors.empty?
+ render_serialized_payload { serialize_wished_item(wished_item) }
else
render_error_payload(resource.errors.full_messages.to_sentence)
end
end
- def remove_wished_variant
- spree_authorize! :destroy, wished_variant
+ def remove_item
+ spree_authorize! :destroy, wished_item
- if wished_variant.destroy
- render_serialized_payload { serialize_wished_variant(wished_variant) }
+ if wished_item.destroy
+ render_serialized_payload { serialize_wished_item(wished_item) }
else
render_error_payload(resource.errors.full_messages.to_sentence)
end
@@ -129,17 +136,17 @@ module Spree
params.require(:wishlist).permit(permitted_wishlist_attributes)
end
- def wished_variant_attributes
- params.permit(permitted_wished_variant_attributes)
+ def wished_item_attributes
+ params.permit(permitted_wished_item_attributes)
end
- def wished_variant
- @wished_variant ||= resource.wished_variants.find(params[:wished_variant_id])
+ def wished_item
+ @wished_item ||= resource.wished_items.find(params[:item_id])
end
- def serialize_wished_variant(wished_variant)
- ::Spree::V2::Storefront::WishedVariantSerializer.new(
- wished_variant,
+ def serialize_wished_item(wished_item)
+ ::Spree::V2::Storefront::WishedItemSerializer.new(
+ wished_item,
params: serializer_params,
include: resource_includes,
fields: sparse_fields
diff --git a/api/app/serializers/spree/v2/storefront/wished_item_serializer.rb b/api/app/serializers/spree/v2/storefront/wished_item_serializer.rb
new file mode 100644
index 0000000000..bda6fbf2b4
--- /dev/null
+++ b/api/app/serializers/spree/v2/storefront/wished_item_serializer.rb
@@ -0,0 +1,30 @@
+module Spree
+ module V2
+ module Storefront
+ class WishedItemSerializer < BaseSerializer
+ set_type :wished_item
+
+ attributes :quantity
+
+ attribute :price do |wished_item, params|
+ wished_item.price(currency: params[:currency])
+ end
+
+ attribute :total do |wished_item, params|
+ wished_item.total(currency: params[:currency])
+ end
+
+ attribute :display_price do |wished_item, params|
+ wished_item.display_price(currency: params[:currency]).to_s
+ end
+
+ attribute :display_total do |wished_item, params|
+ wished_item.display_total(currency: params[:currency]).to_s
+ end
+
+ belongs_to :variant
+ belongs_to :wishlist, serializer: ::Spree::V2::Storefront::WishlistSerializer
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/v2/storefront/wished_variant_serializer.rb b/api/app/serializers/spree/v2/storefront/wished_variant_serializer.rb
deleted file mode 100644
index fc00bd4bc9..0000000000
--- a/api/app/serializers/spree/v2/storefront/wished_variant_serializer.rb
+++ /dev/null
@@ -1,30 +0,0 @@
-module Spree
- module V2
- module Storefront
- class WishedVariantSerializer < BaseSerializer
- set_type :wished_variant
-
- attributes :quantity
-
- attribute :price do |wished_variant, params|
- wished_variant.price(currency: params[:currency])
- end
-
- attribute :total do |wished_variant, params|
- wished_variant.total(currency: params[:currency])
- end
-
- attribute :display_price do |wished_variant, params|
- wished_variant.display_price(currency: params[:currency]).to_s
- end
-
- attribute :display_total do |wished_variant, params|
- wished_variant.display_total(currency: params[:currency]).to_s
- end
-
- belongs_to :variant
- belongs_to :wishlist, serializer: ::Spree::V2::Storefront::WishlistSerializer
- end
- end
- end
-end
diff --git a/api/app/serializers/spree/v2/storefront/wishlist_serializer.rb b/api/app/serializers/spree/v2/storefront/wishlist_serializer.rb
index ae175a9070..80aff4970f 100644
--- a/api/app/serializers/spree/v2/storefront/wishlist_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/wishlist_serializer.rb
@@ -6,7 +6,7 @@ module Spree
attributes :token, :name, :is_private, :is_default
- has_many :wished_variants
+ has_many :wished_items
end
end
end
diff --git a/api/config/locales/en.yml b/api/config/locales/en.yml
index 5c119bc871..30fa1ed938 100644
--- a/api/config/locales/en.yml
+++ b/api/config/locales/en.yml
@@ -36,3 +36,5 @@ en:
no_coupon_code: "No coupon code provided and the Order doesn't have any coupon code promotions applied"
wishlist:
wrong_quantity: "Quantity has to be greater than 0"
+ pass_variant_id: "Pass the variant id that you want to add to this wishlist, eg.{ variant_id: 20 }"
+ must_be_an_integer: "Sorry, you must pass an integer value, eg. 5"
diff --git a/api/config/routes.rb b/api/config/routes.rb
index 42bd8eabe9..b226f4ae0f 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -173,9 +173,9 @@ Spree::Core::Engine.add_routes do
get :default, on: :collection
member do
- post :add_wished_variant
- patch 'update_wished_variant_quantity/:wished_variant_id', to: 'wishlists#update_wished_variant_quantity', as: :update_wished_variant_quantity
- delete 'remove_wished_variant/:wished_variant_id', to: 'wishlists#remove_wished_variant', as: :remove_wished_variant
+ post :add_item
+ patch 'set_item_quantity/:item_id', to: 'wishlists#set_item_quantity', as: :set_item_quantity
+ delete 'remove_item/:item_id', to: 'wishlists#remove_item', as: :remove_item
end
end
end
diff --git a/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb b/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb
index dbe229c7b5..c5e5cb4670 100644
--- a/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb
@@ -30,7 +30,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
wishlist.reload
end
- it 'must return wishlist' do
+ it 'must return the existing default wishlist' do
expect { get '/api/v2/storefront/wishlists/default', headers: headers_bearer }.not_to change { Spree::Wishlist.count }
end
end
@@ -48,7 +48,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
wishlist.reload
end
- it 'creates a default wishlist for the current store' do
+ it 'creates a new default wishlist for the current store' do
expect { get '/api/v2/storefront/wishlists/default', headers: headers_bearer }.to change { Spree::Wishlist.count }.from(1).to(2)
end
end
@@ -85,22 +85,22 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
let!(:wishlist_private) { create(:wishlist, user: other_user, is_private: true) }
let!(:wishlist_public) { create(:wishlist, user: other_user, is_private: false) }
- let!(:wished_variant) do
- wishlist.wished_variants.create({ variant: create(:variant) })
+ let!(:wished_item) do
+ wishlist.wished_items.create({ variant: create(:variant) })
end
it 'returns wishlist details' do
- get "/api/v2/storefront/wishlists/#{wishlist.token}?include=wished_variants", headers: headers_bearer
+ get "/api/v2/storefront/wishlists/#{wishlist.token}?include=wished_items", headers: headers_bearer
expect(response).to have_http_status(:ok)
expect(json_response['data']['attributes']['token']).to eq (wishlist.token)
expect(json_response['data']['attributes']['name']).to eq (wishlist.name)
expect(json_response['data']['attributes']['is_private']).to eq (wishlist.is_private?)
expect(json_response['data']['attributes']['is_default']).to eq (wishlist.is_default?)
- expect(json_response['data']['relationships']['wished_variants']['data'].first['id']).to eq(wished_variant.id.to_s)
+ expect(json_response['data']['relationships']['wished_items']['data'].first['id']).to eq(wished_item.id.to_s)
end
- context 'when a request is sent by random with no auth' do
+ context 'when a request is sent by random user with no auth' do
it 'returns 404 when wishlist is set to private' do
get "/api/v2/storefront/wishlists/#{wishlist_private.token}"
expect(response).to have_http_status(:not_found)
@@ -163,38 +163,96 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
end
describe '#add_item' do
- let!(:item) { create(:variant) }
+ let!(:variant) { create(:variant) }
- it 'must permit update wishlist name' do
+ it 'must permit create wished_item' do
post "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/add_item", headers: headers_bearer,
params: {
- variant_id: item.id,
+ variant_id: variant.id.to_s,
quantity: 3
}
expect(response.status).to eq(200)
-
user.wishlists.reload
- expect(json_response['data']['type']).to eql ('wished_variant')
+ expect(json_response['data']['type']).to eql ('wished_item')
expect(json_response['data']['attributes']['quantity']).to eql (3)
expect(json_response['data']['attributes']['price']).to eql ('19.99')
expect(json_response['data']['attributes']['total']).to eql ('59.97')
expect(json_response['data']['attributes']['display_price']).to eql ('$19.99')
expect(json_response['data']['attributes']['display_total']).to eql ('$59.97')
- expect(json_response['data']['relationships']['variant']['data']['id']).to eql (item.id.to_s)
+ expect(json_response['data']['relationships']['variant']['data']['id']).to eql (variant.id.to_s)
expect(json_response['data']['relationships']['variant']['data']['type']).to eql ('variant')
expect(json_response['data']['relationships']['wishlist']['data']['id']).to eql (user.wishlists.first.id.to_s)
expect(json_response['data']['relationships']['wishlist']['data']['type']).to eql ('wishlist')
end
+
+ context 'when variant is already in the wishlist' do
+ let!(:set_variant) { create(:variant)}
+ let!(:wi) { create(:wished_item, wishlist: user.wishlists.first, variant: set_variant, quantity: 1) }
+
+ it 'quantity set must return the existing wished item' do
+ post "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/add_item", headers: headers_bearer,
+ params: {
+ variant_id: set_variant.id.to_s,
+ quantity: 1
+ }
+
+ expect(response.status).to eq(200)
+ user.wishlists.reload
+
+ expect(json_response['data']['id']).to eql (wi.id.to_s)
+ expect(json_response['data']['attributes']['quantity']).to eql (1)
+ end
+
+ it 'quantity omitted must return existing wished item with quantity 1' do
+ post "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/add_item", headers: headers_bearer,
+ params: {
+ variant_id: set_variant.id.to_s
+ }
+
+ expect(response.status).to eq(200)
+ user.wishlists.reload
+
+ expect(json_response['data']['id']).to eql (wi.id.to_s)
+ expect(json_response['data']['attributes']['quantity']).to eql (1)
+ end
+ end
+
+ it 'must permit create wished_item when omitting the quantity attribute' do
+ post "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/add_item", headers: headers_bearer,
+ params: {
+ variant_id: variant.id.to_s
+ }
+ expect(response.status).to eq(200)
+ user.wishlists.reload
+ expect(json_response['data']['attributes']['quantity']).to eql (1)
+ end
+
+ it 'must not permit create new wished_item omitting the variant_id attribute' do
+ post "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/add_item", headers: headers_bearer,
+ params: {
+ bad_variant_id: variant.id.to_s
+ }
+ expect(response.status).to eq(422)
+ end
+
+ it 'must not permit create new wished_item with a quantity of 0' do
+ post "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/add_item", headers: headers_bearer,
+ params: {
+ variant_id: variant.id.to_s,
+ quantity: 0
+ }
+ expect(response.status).to eq(422)
+ end
end
- describe '#update_item_quantity' do
- let!(:wished_variant) do
- wishlist.wished_variants.create({ variant: create(:variant) })
+ describe '#set_item_quantity' do
+ let!(:wished_item) do
+ wishlist.wished_items.create({ variant: create(:variant) })
end
it 'must permit update wishlist name' do
- patch "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/update_item_quantity/#{wishlist.wished_variants.first.id}",
+ patch "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/set_item_quantity/#{wishlist.wished_items.first.id}",
headers: headers_bearer,
params: {
quantity: 17
@@ -206,20 +264,20 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
end
describe '#remove_item' do
- let!(:wished_variant) do
- wishlist.wished_variants.create({ variant: create(:variant) })
+ let!(:wished_item) do
+ wishlist.wished_items.create({ variant: create(:variant) })
end
it 'must permit update wishlist name' do
- delete "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/remove_item/#{wishlist.wished_variants.first.id}", headers: headers_bearer
+ delete "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/remove_item/#{wishlist.wished_items.first.id}", headers: headers_bearer
expect(response.status).to eq(200)
- expect(json_response['data']['type']).to eql ('wished_variant')
+ expect(json_response['data']['type']).to eql ('wished_item')
end
context 'user not authorised to access this action' do
it 'can not delete item from other users wishlist' do
- delete "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/remove_item/#{wishlist.wished_variants.first.id}"
+ delete "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/remove_item/#{wishlist.wished_items.first.id}"
expect(response.status).to eq(403)
end
diff --git a/api/spec/serializers/spree/v2/storefront/wished_variant_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/wished_item_serializer_spec.rb
similarity index 58%
rename from api/spec/serializers/spree/v2/storefront/wished_variant_serializer_spec.rb
rename to api/spec/serializers/spree/v2/storefront/wished_item_serializer_spec.rb
index 9823d6e386..379517feff 100644
--- a/api/spec/serializers/spree/v2/storefront/wished_variant_serializer_spec.rb
+++ b/api/spec/serializers/spree/v2/storefront/wished_item_serializer_spec.rb
@@ -1,10 +1,10 @@
require 'spec_helper'
-describe Spree::V2::Storefront::WishedVariantSerializer do
- subject { described_class.new(wished_variant, params: { currency: 'USD' }) }
+describe Spree::V2::Storefront::WishedItemSerializer do
+ subject { described_class.new(wished_item, params: { currency: 'USD' }) }
let!(:wishlist) { create(:wishlist) }
- let!(:wished_variant) { create(:wished_variant, wishlist: wishlist, variant: create(:variant), quantity: 5) }
+ let!(:wished_item) { create(:wished_item, wishlist: wishlist, variant: create(:variant), quantity: 5) }
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
@@ -12,19 +12,19 @@ describe Spree::V2::Storefront::WishedVariantSerializer do
expect(subject.serializable_hash).to eq(
{
data: {
- id: wished_variant.id.to_s,
- type: :wished_variant,
+ id: wished_item.id.to_s,
+ type: :wished_item,
attributes: {
display_price: '$19.99',
display_total: '$99.95',
- price: wished_variant.variant.price,
- total: (wished_variant.variant.price * 5),
+ price: wished_item.variant.price,
+ total: (wished_item.variant.price * 5),
quantity: 5,
},
relationships: {
variant: {
data: {
- id: wished_variant.variant.id.to_s,
+ id: wished_item.variant.id.to_s,
type: :variant
}
},
diff --git a/api/spec/serializers/spree/v2/storefront/wishlist_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/wishlist_serializer_spec.rb
index 8d0d72a925..29922ecf9e 100644
--- a/api/spec/serializers/spree/v2/storefront/wishlist_serializer_spec.rb
+++ b/api/spec/serializers/spree/v2/storefront/wishlist_serializer_spec.rb
@@ -4,7 +4,7 @@ describe Spree::V2::Storefront::WishlistSerializer do
subject { described_class.new(wishlist) }
let!(:wishlist) { create(:wishlist) }
- let!(:wished_variant) { create(:wished_variant, wishlist: wishlist, variant: create(:variant), quantity: 1) }
+ let!(:wished_item) { create(:wished_item, wishlist: wishlist, variant: create(:variant), quantity: 1) }
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
@@ -21,11 +21,11 @@ describe Spree::V2::Storefront::WishlistSerializer do
is_default: wishlist.is_default
},
relationships: {
- wished_variants: {
+ wished_items: {
data: [
{
- id: wishlist.wished_variants.first.id.to_s,
- type: :wished_variant
+ id: wishlist.wished_items.first.id.to_s,
+ type: :wished_item
}
]
}
diff --git a/core/app/models/spree/ability.rb b/core/app/models/spree/ability.rb
index 3ccb0dba40..6d415ef1e2 100644
--- a/core/app/models/spree/ability.rb
+++ b/core/app/models/spree/ability.rb
@@ -87,8 +87,8 @@ module Spree
can :read, ::Spree::Variant
can :read, ::Spree::Zone
can :manage, ::Spree::Wishlist, user_id: user.id
- can [:create, :update, :destroy], Spree::WishedVariant do |wished_variant|
- wished_variant.wishlist.user == user
+ can [:create, :update, :destroy], Spree::WishedItem do |wished_item|
+ wished_item.wishlist.user == user
end
end
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index cfd0007c95..df7d970a00 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -42,7 +42,7 @@ module Spree
dependent: :destroy,
inverse_of: :variant
- has_many :wished_variants, dependent: :destroy
+ has_many :wished_items, dependent: :destroy
before_validation :set_cost_currency
diff --git a/core/app/models/spree/wished_variant.rb b/core/app/models/spree/wished_item.rb
similarity index 85%
rename from core/app/models/spree/wished_variant.rb
rename to core/app/models/spree/wished_item.rb
index 1571051421..7bc7cf578a 100644
--- a/core/app/models/spree/wished_variant.rb
+++ b/core/app/models/spree/wished_item.rb
@@ -1,5 +1,5 @@
module Spree
- class WishedVariant < Spree::Base
+ class WishedItem < Spree::Base
extend DisplayMoney
money_methods :total, :price
@@ -10,6 +10,7 @@ module Spree
validates :variant, :wishlist, presence: true
validates :variant, uniqueness: { scope: [:wishlist] }
+ validates :quantity, numericality: { only_integer: true, greater_than: 0 }
def price(currency)
variant.amount_in(currency[:currency])
diff --git a/core/app/models/spree/wishlist.rb b/core/app/models/spree/wishlist.rb
index 1c8323654f..9b1524b10a 100644
--- a/core/app/models/spree/wishlist.rb
+++ b/core/app/models/spree/wishlist.rb
@@ -7,13 +7,13 @@ module Spree
belongs_to :user, class_name: Spree.user_class.name, touch: true
belongs_to :store, class_name: 'Spree::Store'
- has_many :wished_variants, class_name: 'Spree::WishedVariant', dependent: :destroy
+ has_many :wished_items, class_name: 'Spree::WishedItem', dependent: :destroy
after_commit :ensure_default_exists_and_is_unique
validates :name, :store, :user, presence: true
def include?(variant_id)
- wished_variants.exists?(variant_id: variant_id)
+ wished_items.exists?(variant_id: variant_id)
end
def to_param
diff --git a/core/db/migrate/20210906180746_rename_spree_wished_products_to_spree_wished_items.rb b/core/db/migrate/20210906180746_rename_spree_wished_products_to_spree_wished_items.rb
new file mode 100644
index 0000000000..b8906ca8aa
--- /dev/null
+++ b/core/db/migrate/20210906180746_rename_spree_wished_products_to_spree_wished_items.rb
@@ -0,0 +1,5 @@
+class RenameSpreeWishedProductsToSpreeWishedItems < ActiveRecord::Migration[5.2]
+ def change
+ rename_table :spree_wished_products, :spree_wished_items
+ end
+end
diff --git a/core/db/migrate/20210906180746_rename_spree_wished_products_to_spree_wished_variants.rb b/core/db/migrate/20210906180746_rename_spree_wished_products_to_spree_wished_variants.rb
deleted file mode 100644
index cc2aa97d4e..0000000000
--- a/core/db/migrate/20210906180746_rename_spree_wished_products_to_spree_wished_variants.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class RenameSpreeWishedProductsToSpreeWishedVariants < ActiveRecord::Migration[5.2]
- def change
- rename_table :spree_wished_products, :spree_wished_variants
- end
-end
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index 3c0aeb2876..fa6978a01d 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -31,7 +31,7 @@ module Spree
:cms_page_attributes,
:cms_section_attributes,
:wishlist_attributes,
- :wished_variant_attributes
+ :wished_item_attributes
]
mattr_reader *ATTRIBUTES
@@ -145,6 +145,6 @@ module Spree
@@wishlist_attributes = [:name, :is_default, :is_private]
- @@wished_variant_attributes = [:variant_id, :quantity]
+ @@wished_item_attributes = [:variant_id, :quantity]
end
end
diff --git a/core/lib/spree/testing_support/factories/wished_variant_factory.rb b/core/lib/spree/testing_support/factories/wished_variant_factory.rb
index 24b08b60df..4a216e83b9 100644
--- a/core/lib/spree/testing_support/factories/wished_variant_factory.rb
+++ b/core/lib/spree/testing_support/factories/wished_variant_factory.rb
@@ -1,5 +1,5 @@
FactoryBot.define do
- factory :wished_variant, class: Spree::WishedVariant do
+ factory :wished_item, class: Spree::WishedItem do
variant
wishlist
end
diff --git a/core/spec/models/spree/wished_item_spec.rb b/core/spec/models/spree/wished_item_spec.rb
new file mode 100644
index 0000000000..4e181c4adb
--- /dev/null
+++ b/core/spec/models/spree/wished_item_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+RSpec.describe Spree::WishedItem, type: :model do
+ describe '.quantity' do
+ subject { build(:wished_item) }
+
+ let!(:wishlist) { create(:wishlist) }
+ let!(:variant) { create(:variant) }
+ let!(:wished_item_with_variant) { create(:wished_item, variant: variant, quantity: 3) }
+
+ it { is_expected.to respond_to(:quantity) }
+ it { expect(subject.quantity).to eq(1) }
+
+ it 'validates presence of wishlist' do
+ expect(described_class.new(quantity: 3, wishlist: nil, variant: variant)).not_to be_valid
+ end
+
+ it 'validates presence of variant' do
+ expect(described_class.new(quantity: 3, wishlist: wishlist, variant: nil)).not_to be_valid
+ end
+
+ describe 'when wished_item is alread associated with the wishlist' do
+ let!(:existing_wished_item) { create(:wished_item, quantity: 3, wishlist: wishlist, variant: variant) }
+
+ it 'validates uniqueness of variant within scope of wishlist' do
+ expect(described_class.new(quantity: 2, wishlist: wishlist, variant: variant)).not_to be_valid
+ end
+ end
+
+ describe '.price' do
+ it { expect(wished_item_with_variant.price(currency: 'USD')).to eq(variant.amount_in('USD')) }
+ end
+
+ describe '.total' do
+ it { expect(wished_item_with_variant.total(currency: 'USD')).to eql(variant.amount_in('USD') * 3) }
+ end
+
+ describe '.display_price' do
+ it { expect(wished_item_with_variant.display_price(currency: 'USD')).to eq Spree::Money.new(variant.amount_in('USD'), currency: 'USD') }
+ end
+
+ describe '.display_total' do
+ it { expect(wished_item_with_variant.display_total(currency: 'USD')).to eq Spree::Money.new((variant.amount_in('USD') * 3), currency: 'USD') }
+ end
+ end
+end
diff --git a/core/spec/models/spree/wished_variant_spec.rb b/core/spec/models/spree/wished_variant_spec.rb
deleted file mode 100644
index 994346cd27..0000000000
--- a/core/spec/models/spree/wished_variant_spec.rb
+++ /dev/null
@@ -1,46 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::WishedVariant, type: :model do
- describe '.quantity' do
- subject { build(:wished_variant) }
-
- let!(:wishlist) { create(:wishlist) }
- let!(:variant) { create(:variant) }
- let!(:wished_variant_with_variant) { create(:wished_variant, variant: variant, quantity: 3) }
-
- it { is_expected.to respond_to(:quantity) }
- it { expect(subject.quantity).to eq(1) }
-
- it 'validates presence of wishlist' do
- expect(described_class.new(quantity: 3, wishlist: nil, variant: variant)).not_to be_valid
- end
-
- it 'validates presence of variant' do
- expect(described_class.new(quantity: 3, wishlist: wishlist, variant: nil)).not_to be_valid
- end
-
- describe 'when wished_variant is alread associated with the wishlist' do
- let!(:existing_wished_variant) { create(:wished_variant, quantity: 3, wishlist: wishlist, variant: variant) }
-
- it 'validates uniqueness of variant within scope of wishlist' do
- expect(described_class.new(quantity: 2, wishlist: wishlist, variant: variant)).not_to be_valid
- end
- end
-
- describe '.price' do
- it { expect(wished_variant_with_variant.price(currency: 'USD')).to eq(variant.amount_in('USD')) }
- end
-
- describe '.total' do
- it { expect(wished_variant_with_variant.total(currency: 'USD')).to eql(variant.amount_in('USD') * 3) }
- end
-
- describe '.display_price' do
- it { expect(wished_variant_with_variant.display_price(currency: 'USD')).to eq Spree::Money.new(variant.amount_in('USD'), currency: 'USD') }
- end
-
- describe '.display_total' do
- it { expect(wished_variant_with_variant.display_total(currency: 'USD')).to eq Spree::Money.new((variant.amount_in('USD') * 3), currency: 'USD') }
- end
- end
-end
diff --git a/core/spec/models/spree/wishlist_spec.rb b/core/spec/models/spree/wishlist_spec.rb
index 63b20b818e..529e3f1034 100644
--- a/core/spec/models/spree/wishlist_spec.rb
+++ b/core/spec/models/spree/wishlist_spec.rb
@@ -58,8 +58,8 @@ describe Spree::Wishlist, type: :model do
let(:variant) { create(:variant) }
before do
- wished_variant = create(:wished_variant, variant: variant)
- wishlist.wished_variants << wished_variant
+ wished_item = create(:wished_item, variant: variant)
+ wishlist.wished_items << wished_item
wishlist.save
end
@@ -123,12 +123,12 @@ describe Spree::Wishlist, type: :model do
end
describe '#destroy' do
- let!(:wished_variant) { create(:wished_variant) }
+ let!(:wished_item) { create(:wished_item) }
it 'deletes associated wished variants' do
expect do
- wished_variant.wishlist.destroy
- end.to change(Spree::WishedVariant, :count).by(-1)
+ wished_item.wishlist.destroy
+ end.to change(Spree::WishedItem, :count).by(-1)
end
end
end