2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-25 14:10:20 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Awardable do
|
2021-07-14 23:09:42 -04:00
|
|
|
let!(:note) { create(:note) }
|
|
|
|
let!(:award_emoji) { create(:award_emoji, :downvote, awardable: note) }
|
2016-04-25 14:10:20 -04:00
|
|
|
|
|
|
|
describe "Associations" do
|
2021-07-14 23:09:42 -04:00
|
|
|
subject { build(:note) }
|
2017-04-25 08:28:55 -04:00
|
|
|
|
2016-04-25 14:10:20 -04:00
|
|
|
it { is_expected.to have_many(:award_emoji).dependent(:destroy) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "ClassMethods" do
|
2021-07-14 23:09:42 -04:00
|
|
|
let!(:note2) { create(:note) }
|
|
|
|
let!(:award_emoji2) { create(:award_emoji, awardable: note2) }
|
2016-04-25 14:10:20 -04:00
|
|
|
|
2017-08-30 03:48:55 -04:00
|
|
|
describe "orders" do
|
|
|
|
it "orders on upvotes" do
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(Note.order_upvotes_desc.to_a).to eq [note2, note]
|
2017-08-30 03:48:55 -04:00
|
|
|
end
|
2016-04-25 14:10:20 -04:00
|
|
|
|
2017-08-30 03:48:55 -04:00
|
|
|
it "orders on downvotes" do
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(Note.order_downvotes_desc.to_a).to eq [note, note2]
|
2017-08-30 03:48:55 -04:00
|
|
|
end
|
2016-04-25 14:10:20 -04:00
|
|
|
end
|
|
|
|
|
2018-10-27 11:52:26 -04:00
|
|
|
describe "#awarded" do
|
2017-08-30 03:48:55 -04:00
|
|
|
it "filters by user and emoji name" do
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(Note.awarded(award_emoji.user, "thumbsup")).to be_empty
|
|
|
|
expect(Note.awarded(award_emoji.user, "thumbsdown")).to eq [note]
|
|
|
|
expect(Note.awarded(award_emoji2.user, "thumbsup")).to eq [note2]
|
|
|
|
expect(Note.awarded(award_emoji2.user, "thumbsdown")).to be_empty
|
2017-08-30 03:48:55 -04:00
|
|
|
end
|
2018-10-27 11:52:26 -04:00
|
|
|
|
|
|
|
it "filters by user and any emoji" do
|
2021-07-14 23:09:42 -04:00
|
|
|
note3 = create(:note)
|
|
|
|
create(:award_emoji, awardable: note3, name: "star", user: award_emoji.user)
|
|
|
|
create(:award_emoji, awardable: note3, name: "star", user: award_emoji2.user)
|
2018-10-27 11:52:26 -04:00
|
|
|
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(Note.awarded(award_emoji.user)).to contain_exactly(note, note3)
|
|
|
|
expect(Note.awarded(award_emoji2.user)).to contain_exactly(note2, note3)
|
2018-10-27 11:52:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#not_awarded" do
|
2021-07-14 23:09:42 -04:00
|
|
|
it "returns notes not awarded by user" do
|
|
|
|
expect(Note.not_awarded(award_emoji.user)).to eq [note2]
|
|
|
|
expect(Note.not_awarded(award_emoji2.user)).to eq [note]
|
2018-10-27 11:52:26 -04:00
|
|
|
end
|
2016-04-25 14:10:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#upvotes" do
|
|
|
|
it "counts the number of upvotes" do
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(note.upvotes).to be 0
|
2016-04-25 14:10:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#downvotes" do
|
|
|
|
it "counts the number of downvotes" do
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(note.downvotes).to be 1
|
2016-04-25 14:10:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-06 14:19:37 -04:00
|
|
|
describe '#user_can_award?' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
2021-07-14 23:09:42 -04:00
|
|
|
note.project.add_guest(user)
|
2018-04-06 14:19:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is truthy when the user is allowed to award emoji' do
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(note.user_can_award?(user)).to be_truthy
|
2018-04-06 14:19:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is falsy when the project is archived' do
|
2021-07-14 23:09:42 -04:00
|
|
|
note.project.update!(archived: true)
|
2018-04-06 14:19:37 -04:00
|
|
|
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(note.user_can_award?(user)).to be_falsy
|
2018-04-06 14:19:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-29 10:01:00 -04:00
|
|
|
describe 'querying award_emoji on an Awardable' do
|
2021-07-14 23:09:42 -04:00
|
|
|
let(:note) { create(:note) }
|
2016-08-29 10:01:00 -04:00
|
|
|
|
|
|
|
it 'sorts in ascending fashion' do
|
2021-07-14 23:09:42 -04:00
|
|
|
create_list(:award_emoji, 3, awardable: note)
|
2016-08-29 10:01:00 -04:00
|
|
|
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(note.award_emoji).to eq note.award_emoji.sort_by(&:id)
|
2016-08-29 10:01:00 -04:00
|
|
|
end
|
|
|
|
end
|
2020-05-15 11:08:04 -04:00
|
|
|
|
|
|
|
describe "#grouped_awards" do
|
|
|
|
context 'default award emojis' do
|
2021-07-14 23:09:42 -04:00
|
|
|
let(:note_without_downvote) { create(:note) }
|
|
|
|
let(:note_with_downvote) do
|
|
|
|
note_with_downvote = create(:note)
|
|
|
|
create(:award_emoji, :downvote, awardable: note_with_downvote)
|
|
|
|
note_with_downvote
|
2020-05-15 11:08:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "includes unused thumbs buttons by default" do
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(note_without_downvote.grouped_awards.keys.sort).to eq %w(thumbsdown thumbsup)
|
2020-05-15 11:08:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't include unused thumbs buttons when disabled in project" do
|
2021-07-14 23:09:42 -04:00
|
|
|
note_without_downvote.project.show_default_award_emojis = false
|
2020-05-15 11:08:04 -04:00
|
|
|
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(note_without_downvote.grouped_awards.keys.sort).to be_empty
|
2020-05-15 11:08:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "includes unused thumbs buttons when enabled in project" do
|
2021-07-14 23:09:42 -04:00
|
|
|
note_without_downvote.project.show_default_award_emojis = true
|
2020-05-15 11:08:04 -04:00
|
|
|
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(note_without_downvote.grouped_awards.keys.sort).to eq %w(thumbsdown thumbsup)
|
2020-05-15 11:08:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't include unused thumbs buttons in summary" do
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(note_without_downvote.grouped_awards(with_thumbs: false).keys).to be_empty
|
2020-05-15 11:08:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "includes used thumbs buttons when disabled in project" do
|
2021-07-14 23:09:42 -04:00
|
|
|
note_with_downvote.project.show_default_award_emojis = false
|
2020-05-15 11:08:04 -04:00
|
|
|
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(note_with_downvote.grouped_awards.keys).to eq %w(thumbsdown)
|
2020-05-15 11:08:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "includes used thumbs buttons in summary" do
|
2021-07-14 23:09:42 -04:00
|
|
|
expect(note_with_downvote.grouped_awards(with_thumbs: false).keys).to eq %w(thumbsdown)
|
2020-05-15 11:08:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-25 14:10:20 -04:00
|
|
|
end
|