Sha256: 203f1da25ca4e3546ffba85e0193b1c7f6aa87a1eec55d2bebbc3fd9ad3f4074

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

require 'spec_helper'

describe Spree::Product do
  it { should respond_to(:avg_rating) }
  it { should respond_to(:reviews) }
  it { should respond_to(:stars) }

  context '#stars' do
    let(:product) { build(:product) }

    it 'rounds' do
      product.stub(:avg_rating).and_return(3.7)
      expect(product.stars).to eq(4)

      product.stub(:avg_rating).and_return(2.3)
      expect(product.stars).to eq(2)
    end


    it 'handles a nil value' do
      product.stub(:avg_rating).and_return(nil)
      expect {
        expect(product.stars).to eq(0)
      }.not_to raise_error
    end
  end

  context '#recalculate_rating' do
    let!(:product) { create(:product) }

    context 'when there are approved reviews' do
      let!(:approved_review_1) { create(:review, product: product, approved: true, rating: 4) }
      let!(:approved_review_2) { create(:review, product: product, approved: true, rating: 5) }
      let!(:unapproved_review_1) { create(:review, product: product, approved: false, rating: 4) }

      it "updates the product average rating and ignores unapproved reviews" do
        product.avg_rating = 0
        product.reviews_count = 0
        product.save!

        product.recalculate_rating
        product.avg_rating.should eq(4.5)
        product.reviews_count.should eq(2)
      end
    end

    context 'when no approved reviews' do
      let!(:unapproved_review_1) { create(:review, product: product, approved: false, rating: 4) }

      it "updates the product average rating and ignores unapproved reviews" do
        product.avg_rating = 3
        product.reviews_count = 20
        product.save!

        product.recalculate_rating
        product.avg_rating.should eq(0)
        product.reviews_count.should eq(0)
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
solidus_reviews-1.0.0 spec/models/product_spec.rb
jiffyshirts_spree_reviews-2.3.1.2 spec/models/product_spec.rb
jiffyshirts_spree_reviews-2.3.1.1 spec/models/product_spec.rb