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 more tests for wishlist model.

Matthew Kennedyf46adb6c83ca
diff --git a/core/spec/models/spree/wishlist_spec.rb b/core/spec/models/spree/wishlist_spec.rb
index f7abc10f94..b126a8f4d1 100644
--- a/core/spec/models/spree/wishlist_spec.rb
+++ b/core/spec/models/spree/wishlist_spec.rb
@@ -1,13 +1,59 @@
require 'spec_helper'
describe Spree::Wishlist, type: :model do
- let(:user) { create(:user) }
- let(:wishlist) { create(:wishlist, user: user, name: 'My Wishlist') }
+ let!(:store) { Spree::Store.default }
+ let!(:other_store) { create(:store) }
+ let!(:user) { create(:user) }
+ let!(:other_user) { create(:user) }
+
+ let!(:wishlist) { create(:wishlist, user: user, name: 'My Wishlist', store: store, is_default: true) }
+ let!(:wishlist_belonging_to_other_store) { create(:wishlist, user: user, name: 'My Wishlist', store: other_store, is_default: true) }
+ let!(:wishlist_belonging_to_other_user) { create(:wishlist, user: other_user, name: 'My Wishlist', store: store, is_default: true) }
it 'has a valid factory' do
expect(wishlist).to be_valid
end
+ it 'validates presence of name' do
+ expect(described_class.new(name: nil, user: user, store: store)).not_to be_valid
+ end
+
+ it 'validates presence of store' do
+ expect(described_class.new(name: 'My Wishlist', user: user, store: nil)).not_to be_valid
+ end
+
+ it 'validates presence of user' do
+ expect(described_class.new(name: 'My Wishlist', user: nil, store: store)).not_to be_valid
+ end
+
+ describe '.ensure_default_exists_and_is_unique' do
+ context 'when user creates a new default store' do
+ let!(:new_wl) { create(:wishlist, name: 'My New WishList', user: user, store: store, is_default: true) }
+
+ it 'preserves is_default: true for new wishlist' do
+ expect(new_wl.is_default).to be true
+ end
+
+ it 'sets is_default: false on the wishlist that was the previous default' do
+ wishlist.reload
+
+ expect(wishlist.is_default).to be false
+ end
+
+ it 'does not alter the state of wishlist belonging to other users' do
+ wishlist_belonging_to_other_user.reload
+
+ expect(wishlist_belonging_to_other_user.is_default).to be true
+ end
+
+ it 'does not alter the state of wishlist belonging to same users, but in other stores' do
+ wishlist_belonging_to_other_store.reload
+
+ expect(wishlist_belonging_to_other_store.is_default).to be true
+ end
+ end
+ end
+
describe '.include?' do
let(:variant) { create(:variant) }
@@ -79,10 +125,10 @@ describe Spree::Wishlist, type: :model do
describe '#destroy' do
let!(:wished_variant) { create(:wished_variant) }
- it 'deletes associated wished products' do
- expect {
+ it 'deletes associated wished variants' do
+ expect do
wished_variant.wishlist.destroy
- }.to change(Spree::WishedVariant, :count).by(-1)
+ end.to change(Spree::WishedVariant, :count).by(-1)
end
end
end