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.

Start building models tests for wishlists.

Matthew Kennedy8103097c330f
diff --git a/core/db/migrate/20210902123011_create_spree_wished_products.rb b/core/db/migrate/20210902123011_create_spree_wished_products.rb
index 04a9b4b46a..7889661232 100644
--- a/core/db/migrate/20210902123011_create_spree_wished_products.rb
+++ b/core/db/migrate/20210902123011_create_spree_wished_products.rb
@@ -5,7 +5,6 @@ class CreateSpreeWishedProducts < ActiveRecord::Migration[5.2]
t.belongs_to :wishlist
t.column :quantity, :integer, default: 1, null: false
- t.column :remark, :text
t.timestamps
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
new file mode 100644
index 0000000000..24b08b60df
--- /dev/null
+++ b/core/lib/spree/testing_support/factories/wished_variant_factory.rb
@@ -0,0 +1,6 @@
+FactoryBot.define do
+ factory :wished_variant, class: Spree::WishedVariant do
+ variant
+ wishlist
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/wishlist_factory.rb b/core/lib/spree/testing_support/factories/wishlist_factory.rb
new file mode 100644
index 0000000000..d819f0666b
--- /dev/null
+++ b/core/lib/spree/testing_support/factories/wishlist_factory.rb
@@ -0,0 +1,18 @@
+FactoryBot.define do
+ factory :wishlist, class: Spree::Wishlist do
+ user
+
+ sequence(:name) { |n| "Wishlist_#{n}" }
+ is_private { true }
+ is_default { false }
+
+ before(:create) do |wishlist|
+ if wishlist.store.nil?
+ default_store = Spree::Store.default.persisted? ? Spree::Store.default : nil
+ store = default_store || create(:store)
+
+ wishlist.store = store
+ end
+ end
+ end
+end
diff --git a/core/spec/models/spree/wished_variant_spec.rb b/core/spec/models/spree/wished_variant_spec.rb
new file mode 100644
index 0000000000..308eaf3076
--- /dev/null
+++ b/core/spec/models/spree/wished_variant_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+RSpec.describe Spree::WishedVariant, type: :model do
+ describe '#quantity' do
+ subject { build(:wished_variant) }
+
+ let!(:variant) { create(:variant)}
+ let!(:wished_variant_with_product) {create(:wished_variant, variant: variant, quantity: 3)}
+
+ it { is_expected.to respond_to(:quantity) }
+ it { expect(subject.quantity).to eq(1) }
+
+ context '#price' do
+ it { expect(wished_variant_with_product.price(currency: 'USD')).to eq(variant.amount_in('USD')) }
+ end
+
+ context '#total' do
+ it { expect(wished_variant_with_product.total(currency: 'USD')).to eql(variant.amount_in('USD') * 3) }
+ end
+ end
+end
diff --git a/core/spec/models/spree/wishlist_spec.rb b/core/spec/models/spree/wishlist_spec.rb
new file mode 100644
index 0000000000..47e9b979b3
--- /dev/null
+++ b/core/spec/models/spree/wishlist_spec.rb
@@ -0,0 +1,88 @@
+require 'spec_helper'
+
+describe Spree::Wishlist, type: :model do
+ let(:user) { create(:user) }
+ let(:wishlist) { create(:wishlist, user: user, name: 'My Wishlist') }
+
+ it 'has a valid factory' do
+ expect(wishlist).to be_valid
+ end
+
+ context '.include?' do
+ let(:variant) { create(:variant) }
+
+ before do
+ wished_variant = create(:wished_variant, variant: variant)
+ wishlist.wished_variants << wished_variant
+ wishlist.save
+ end
+
+ it 'is true if the wishlist includes the specified variant' do
+ expect(wishlist.include?(variant.id)).to be true
+ end
+ end
+
+ context '.to_param' do
+ it 'returns the wishlists token' do
+ expect(wishlist.to_param).to eq wishlist.token
+ end
+ end
+
+ context '.get_by_param' do
+ it 'returns the wishlist of the token' do
+ hash = wishlist.token
+ result = described_class.get_by_param(hash)
+ expect(result).to eq wishlist
+ end
+
+ it 'returns nil when not found' do
+ result = described_class.get_by_param('nope')
+ expect(result).to be_nil
+ end
+ end
+
+ context '.can_be_read_by?' do
+ context 'when the wishlist is private' do
+ it 'is true when the user owns the wishlist' do
+ wishlist.is_private = true
+ expect(wishlist.can_be_read_by?(user)).to be true
+ end
+
+ it 'is false when the user does not own the wishlist' do
+ wishlist.is_private = true
+ other_user = create(:user)
+ expect(wishlist.can_be_read_by?(other_user)).to be false
+ end
+ end
+
+ context 'when the wishlist is public' do
+ it 'is true for any user' do
+ wishlist.is_private = false
+ other_user = create(:user)
+ expect(wishlist.can_be_read_by?(other_user)).to be true
+ end
+ end
+ end
+
+ context '.public?' do
+ it 'is true when the wishlist is public' do
+ wishlist.is_private = false
+ expect(wishlist.public?).to be true
+ end
+
+ it 'is false when the wishlist is private' do
+ wishlist.is_private = true
+ expect(wishlist.public?).not_to be true
+ end
+ end
+
+ context '#destroy' do
+ let!(:wished_variant) { create(:wished_variant) }
+
+ it 'deletes associated wished products' do
+ expect {
+ wished_variant.wishlist.destroy
+ }.to change(Spree::WishedVariant, :count).by(-1)
+ end
+ end
+end