summaryrefslogtreecommitdiff
path: root/spec/models/product_cart_spec.rb
blob: 46860d3c34ea44b7f26bd70fdb0b9941df315a96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ProductCart, type: :model do
  let(:product_cart) { create(:product_cart) }

  it 'should belong to product' do
    expect(product_cart).to respond_to(:product)
  end

  it 'should belong to cart' do
    expect(product_cart).to respond_to(:cart)
  end

  describe '#sole_product_in_cart' do
    it 'validates for duplicate products in the cart' do
      another_product_cart = build(:product_cart, cart: product_cart.cart, product: product_cart.product)
      expect(another_product_cart.save).to be_falsey
      expect(another_product_cart.errors[:product_id]).to be_present
    end
  end

  describe '#product_quantity' do
    it "validates the requested quantity against the product's available quantity" do
      product = create(:product, available_quantity: 5)
      another_product_cart = build(:product_cart, cart: product_cart.cart, product:, quantity: 10)

      expect(another_product_cart.save).to be_falsey
      expect(another_product_cart.errors[:product_id][0]).to eq('not enough in existence')
    end
  end
end