2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-16 15:09:08 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe AwardEmoji do
|
2016-04-16 15:09:08 -04:00
|
|
|
describe 'Associations' do
|
|
|
|
it { is_expected.to belong_to(:awardable) }
|
|
|
|
it { is_expected.to belong_to(:user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'modules' do
|
|
|
|
it { is_expected.to include_module(Participable) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "validations" do
|
|
|
|
it { is_expected.to validate_presence_of(:awardable) }
|
|
|
|
it { is_expected.to validate_presence_of(:user) }
|
|
|
|
it { is_expected.to validate_presence_of(:name) }
|
|
|
|
|
|
|
|
# To circumvent a bug in the shoulda matchers
|
|
|
|
describe "scoped uniqueness validation" do
|
|
|
|
it "rejects duplicate award emoji" do
|
|
|
|
user = create(:user)
|
|
|
|
issue = create(:issue)
|
|
|
|
create(:award_emoji, user: user, awardable: issue)
|
2016-06-01 05:23:09 -04:00
|
|
|
new_award = build(:award_emoji, user: user, awardable: issue)
|
2016-04-16 15:09:08 -04:00
|
|
|
|
|
|
|
expect(new_award).not_to be_valid
|
|
|
|
end
|
2017-04-06 06:06:36 -04:00
|
|
|
|
|
|
|
# Assume User A and User B both created award emoji of the same name
|
|
|
|
# on the same awardable. When User A is deleted, User A's award emoji
|
|
|
|
# is moved to the ghost user. When User B is deleted, User B's award emoji
|
|
|
|
# also needs to be moved to the ghost user - this cannot happen unless
|
|
|
|
# the uniqueness validation is disabled for ghost users.
|
|
|
|
it "allows duplicate award emoji for ghost users" do
|
|
|
|
user = create(:user, :ghost)
|
|
|
|
issue = create(:issue)
|
|
|
|
create(:award_emoji, user: user, awardable: issue)
|
|
|
|
new_award = build(:award_emoji, user: user, awardable: issue)
|
|
|
|
|
|
|
|
expect(new_award).to be_valid
|
|
|
|
end
|
2020-04-02 11:08:01 -04:00
|
|
|
|
|
|
|
# Similar to allowing duplicate award emojis for ghost users,
|
|
|
|
# when Importing a project that has duplicate award emoji placed by
|
|
|
|
# ghost user we change the author to be importer user and allow
|
|
|
|
# duplicates, otherwise relation containing such duplicates
|
|
|
|
# fails to be created
|
|
|
|
context 'when importing' do
|
|
|
|
it 'allows duplicate award emoji' do
|
|
|
|
user = create(:user)
|
|
|
|
issue = create(:issue)
|
|
|
|
create(:award_emoji, user: user, awardable: issue)
|
|
|
|
new_award = build(:award_emoji, user: user, awardable: issue, importing: true)
|
|
|
|
|
|
|
|
expect(new_award).to be_valid
|
|
|
|
end
|
|
|
|
end
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|
|
|
|
end
|
2017-07-17 14:36:29 -04:00
|
|
|
|
2019-06-17 21:44:43 -04:00
|
|
|
describe 'scopes' do
|
2020-02-05 07:09:15 -05:00
|
|
|
let_it_be(:thumbsup) { create(:award_emoji, name: 'thumbsup') }
|
|
|
|
let_it_be(:thumbsdown) { create(:award_emoji, name: 'thumbsdown') }
|
2019-06-17 21:44:43 -04:00
|
|
|
|
|
|
|
describe '.upvotes' do
|
|
|
|
it { expect(described_class.upvotes).to contain_exactly(thumbsup) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.downvotes' do
|
|
|
|
it { expect(described_class.downvotes).to contain_exactly(thumbsdown) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.named' do
|
|
|
|
it { expect(described_class.named('thumbsup')).to contain_exactly(thumbsup) }
|
|
|
|
it { expect(described_class.named(%w[thumbsup thumbsdown])).to contain_exactly(thumbsup, thumbsdown) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.awarded_by' do
|
|
|
|
it { expect(described_class.awarded_by(thumbsup.user)).to contain_exactly(thumbsup) }
|
|
|
|
it { expect(described_class.awarded_by([thumbsup.user, thumbsdown.user])).to contain_exactly(thumbsup, thumbsdown) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-17 14:36:29 -04:00
|
|
|
describe 'expiring ETag cache' do
|
|
|
|
context 'on a note' do
|
|
|
|
let(:note) { create(:note_on_issue) }
|
|
|
|
let(:award_emoji) { build(:award_emoji, user: build(:user), awardable: note) }
|
|
|
|
|
|
|
|
it 'calls expire_etag_cache on the note when saved' do
|
|
|
|
expect(note).to receive(:expire_etag_cache)
|
|
|
|
|
|
|
|
award_emoji.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls expire_etag_cache on the note when destroyed' do
|
|
|
|
expect(note).to receive(:expire_etag_cache)
|
|
|
|
|
|
|
|
award_emoji.destroy!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on another awardable' do
|
|
|
|
let(:issue) { create(:issue) }
|
|
|
|
let(:award_emoji) { build(:award_emoji, user: build(:user), awardable: issue) }
|
|
|
|
|
|
|
|
it 'does not call expire_etag_cache on the issue when saved' do
|
|
|
|
expect(issue).not_to receive(:expire_etag_cache)
|
|
|
|
|
|
|
|
award_emoji.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not call expire_etag_cache on the issue when destroyed' do
|
|
|
|
expect(issue).not_to receive(:expire_etag_cache)
|
|
|
|
|
|
|
|
award_emoji.destroy!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-30 11:45:49 -04:00
|
|
|
|
|
|
|
describe '.award_counts_for_user' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create(:award_emoji, user: user, name: 'thumbsup')
|
|
|
|
create(:award_emoji, user: user, name: 'thumbsup')
|
|
|
|
create(:award_emoji, user: user, name: 'thumbsdown')
|
|
|
|
create(:award_emoji, user: user, name: '+1')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the awarded emoji in descending order' do
|
|
|
|
awards = described_class.award_counts_for_user(user)
|
|
|
|
|
|
|
|
expect(awards).to eq('thumbsup' => 2, 'thumbsdown' => 1, '+1' => 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'limits the returned number of rows' do
|
|
|
|
awards = described_class.award_counts_for_user(user, 1)
|
|
|
|
|
|
|
|
expect(awards).to eq('thumbsup' => 2)
|
|
|
|
end
|
|
|
|
end
|
2016-04-16 15:09:08 -04:00
|
|
|
end
|