Sha256: c2fe617e302f87f898516ff041298c4b046abda91cb5fa85bf35bd88295bf3f3

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Rating::Rating, ':values_data' 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: 4

    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
    subject { described_class.values_data article_1, nil }

    it 'returns the average of value for a resource' do
      expect(subject.as_json['rating_avg']).to eq 52.0
    end

    it 'returns the sum of values for a resource' do
      expect(subject.as_json['rating_sum']).to eq 104
    end

    it 'returns the count of votes for a resource' do
      expect(subject.as_json['rating_count']).to eq 2
    end
  end

  context 'with scopeable' do
    subject { described_class.values_data article_1, category }

    it 'returns the average of value for a resource' do
      expect(subject.as_json['rating_avg']).to eq 1.5
    end

    it 'returns the sum of values for a resource' do
      expect(subject.as_json['rating_sum']).to eq 3
    end

    it 'returns the count of votes for a resource' do
      expect(subject.as_json['rating_count']).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/values_data_spec.rb