gitlab-org--gitlab-foss/spec/models/award_emoji_spec.rb
Yorick Peterse 6f3c490107
Refactor AutocompleteController
This refactors the AutocompleteController according to the guidelines
and boundaries discussed in
https://gitlab.com/gitlab-org/gitlab-ce/issues/49653. Specifically,
ActiveRecord logic is moved to different finders, which are then used in
the controller. View logic in turn is moved to presenters, instead of
directly using ActiveRecord's "to_json" method.

The finder MoveToProjectFinder is also adjusted according to the
abstraction guidelines and boundaries, resulting in a much more simple
finder.

By using finders (and other abstractions) more actively, we can push a
lot of logic out of the controller. We also remove the need for various
"before_action" hooks, though this could be achieved without using
finders as well.

The various finders related to AutcompleteController have also been
moved into a namespace. This removes the need for calling everything
"AutocompleteSmurfFinder", instead you can use
"Autocomplete::SmurfFinder".
2018-08-20 13:53:00 +02:00

103 lines
3.2 KiB
Ruby

require 'spec_helper'
describe AwardEmoji do
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)
new_award = build(:award_emoji, user: user, awardable: issue)
expect(new_award).not_to be_valid
end
# 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
end
end
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
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
end