2017-10-30 01:44:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Rating::Extension, ':rates' do
|
2017-11-02 18:55:46 +00:00
|
|
|
let!(:category) { create :category }
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
let!(:user_1) { create :user }
|
|
|
|
let!(:user_2) { create :user }
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
let!(:article_1) { create :article }
|
|
|
|
let!(:article_2) { create :article }
|
|
|
|
let!(:article_3) { create :article }
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
let!(:rate_1) { create :rating_rate, author: user_1, resource: article_1, value: 100 }
|
|
|
|
let!(:rate_2) { create :rating_rate, author: user_1, resource: article_2, value: 11 }
|
|
|
|
let!(:rate_3) { create :rating_rate, author: user_1, resource: article_3, value: 10 }
|
|
|
|
let!(:rate_4) { create :rating_rate, author: user_2, resource: article_1, value: 1 }
|
|
|
|
|
|
|
|
let!(:rate_5) { create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 }
|
|
|
|
let!(:rate_6) { create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 }
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
context 'with no scope' do
|
|
|
|
it 'returns rates that this resource received' do
|
|
|
|
expect(article_1.rates).to match_array [rate_1, rate_4]
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
2017-11-02 18:55:46 +00:00
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
context 'with scope' do
|
|
|
|
it 'returns scoped rates that this resource received' do
|
|
|
|
expect(article_1.rates(scope: category)).to match_array [rate_5, rate_6]
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
context 'when destroy author' do
|
|
|
|
it 'destroys rates of that author' do
|
|
|
|
expect(Rating::Rate.where(author: user_1).count).to eq 4
|
|
|
|
|
|
|
|
user_1.destroy!
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
expect(Rating::Rate.where(author: user_1).count).to eq 0
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
2017-11-02 18:55:46 +00:00
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
context 'when destroy resource' do
|
2017-10-30 01:44:23 +00:00
|
|
|
it 'destroys rates of that resource' do
|
2017-11-02 18:55:46 +00:00
|
|
|
expect(Rating::Rate.where(resource: article_1).count).to eq 4
|
|
|
|
|
|
|
|
article_1.destroy!
|
|
|
|
|
|
|
|
expect(Rating::Rate.where(resource: article_1).count).to eq 0
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|