Sha256: 372727b9d8a557bee252dadf31c8c7886708fb279c3b8ff535138ca2e0a17a46

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Rating::Rating, ':update_rating' do
  let!(:category) { create :category }

  let!(:user_1) { create :user }
  let!(:user_2) { create :user }

  let!(:article_1) { create :article }
  let!(:article_2) { create :article }
  let!(:article_3) { create :article }

  before do
    create :rating_rate, author: user_1, resource: article_1, value: 100
    create :rating_rate, author: user_1, resource: article_2, value: 11
    create :rating_rate, author: user_1, resource: article_3, value: 10
    create :rating_rate, author: user_2, resource: article_1, value: 1

    create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1
    create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2
  end

  context 'with no scopeable' do
    it 'updates the rating data of the given resource' do
      record = described_class.find_by(resource: article_1)

      expect(record.average).to  eq 50.50000000000001
      expect(record.estimate).to eq 42.50000000000001
      expect(record.sum).to      eq 101
      expect(record.total).to    eq 2
    end
  end

  context 'with scopeable' do
    it 'updates the rating data of the given resource respecting the scope' do
      record = described_class.find_by(resource: article_1, scopeable: category)

      expect(record.average).to  eq 1.5
      expect(record.estimate).to eq 1.5
      expect(record.sum).to      eq 3
      expect(record.total).to    eq 2
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rating-0.2.0 spec/models/rating/update_rating_spec.rb