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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Add better tests to wished_variant_spec
Matthew Kennedy2d78e6068f59
diff --git a/core/app/models/spree/wished_variant.rb b/core/app/models/spree/wished_variant.rbindex 3ffa15da3a..1571051421 100644--- a/core/app/models/spree/wished_variant.rb+++ b/core/app/models/spree/wished_variant.rb@@ -9,6 +9,7 @@ module Spreehas_one :product, class_name: 'Spree::Product', through: :variantvalidates :variant, :wishlist, presence: true+ validates :variant, uniqueness: { scope: [:wishlist] }def price(currency)variant.amount_in(currency[:currency])diff --git a/core/spec/models/spree/wished_variant_spec.rb b/core/spec/models/spree/wished_variant_spec.rbindex 308eaf3076..994346cd27 100644--- a/core/spec/models/spree/wished_variant_spec.rb+++ b/core/spec/models/spree/wished_variant_spec.rb@@ -1,21 +1,46 @@require 'spec_helper'RSpec.describe Spree::WishedVariant, type: :model do- describe '#quantity' do+ describe '.quantity' dosubject { build(:wished_variant) }- let!(:variant) { create(:variant)}- let!(:wished_variant_with_product) {create(:wished_variant, variant: variant, quantity: 3)}+ 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) }- context '#price' do- it { expect(wished_variant_with_product.price(currency: 'USD')).to eq(variant.amount_in('USD')) }+ it 'validates presence of wishlist' do+ expect(described_class.new(quantity: 3, wishlist: nil, variant: variant)).not_to be_validend- context '#total' do- it { expect(wished_variant_with_product.total(currency: 'USD')).to eql(variant.amount_in('USD') * 3) }+ 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') }endendenddiff --git a/core/spec/models/spree/wishlist_spec.rb b/core/spec/models/spree/wishlist_spec.rbindex 47e9b979b3..f7abc10f94 100644--- a/core/spec/models/spree/wishlist_spec.rb+++ b/core/spec/models/spree/wishlist_spec.rb@@ -8,7 +8,7 @@ describe Spree::Wishlist, type: :model doexpect(wishlist).to be_validend- context '.include?' do+ describe '.include?' dolet(:variant) { create(:variant) }before do@@ -22,13 +22,13 @@ describe Spree::Wishlist, type: :model doendend- context '.to_param' do+ describe '.to_param' doit 'returns the wishlists token' doexpect(wishlist.to_param).to eq wishlist.tokenendend- context '.get_by_param' do+ describe '.get_by_param' doit 'returns the wishlist of the token' dohash = wishlist.tokenresult = described_class.get_by_param(hash)@@ -41,7 +41,7 @@ describe Spree::Wishlist, type: :model doendend- context '.can_be_read_by?' do+ describe '.can_be_read_by?' docontext 'when the wishlist is private' doit 'is true when the user owns the wishlist' dowishlist.is_private = true@@ -64,7 +64,7 @@ describe Spree::Wishlist, type: :model doendend- context '.public?' do+ describe '.public?' doit 'is true when the wishlist is public' dowishlist.is_private = falseexpect(wishlist.public?).to be true@@ -76,7 +76,7 @@ describe Spree::Wishlist, type: :model doendend- context '#destroy' do+ describe '#destroy' dolet!(:wished_variant) { create(:wished_variant) }it 'deletes associated wished products' do