Adapt tests to match new counter methods
This commit is contained in:
parent
369ae1b378
commit
68e0f02a2d
|
@ -14,19 +14,16 @@ module User::InboxMethods
|
||||||
end
|
end
|
||||||
|
|
||||||
def unread_inbox_count
|
def unread_inbox_count
|
||||||
Rails.cache.fetch("#{inbox_cache_key}/unread_inbox_count") do
|
Rails.cache.fetch(inbox_cache_key) do
|
||||||
count = Inbox.select("COUNT(id) AS count")
|
count = Inbox.where(new: true, user_id: id).count(:id)
|
||||||
.where(new: true)
|
|
||||||
.where(user_id: id)
|
|
||||||
.group(:user_id)
|
|
||||||
.order(:count)
|
|
||||||
.first
|
|
||||||
return nil if count.nil?
|
|
||||||
return nil unless count.count.positive?
|
|
||||||
|
|
||||||
count.count
|
# Returning +nil+ here in order to not display a counter
|
||||||
|
# at all when there isn't anything in the user's inbox
|
||||||
|
return nil unless count.positive?
|
||||||
|
|
||||||
|
count
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def inbox_cache_key = "#{cache_key}-#{inbox_updated_at}"
|
def inbox_cache_key = "#{cache_key}/unread_inbox_count-#{inbox_updated_at}"
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,13 +2,16 @@
|
||||||
|
|
||||||
module User::NotificationMethods
|
module User::NotificationMethods
|
||||||
def unread_notification_count
|
def unread_notification_count
|
||||||
Rails.cache.fetch("#{notification_cache_key}/unread_notification_count") do
|
Rails.cache.fetch(notification_cache_key) do
|
||||||
count = Notification.for(self).where(new: true).count
|
count = Notification.for(self).where(new: true).count(:id)
|
||||||
|
|
||||||
|
# Returning +nil+ here in order to not display a counter
|
||||||
|
# at all when there aren't any notifications
|
||||||
return nil unless count.positive?
|
return nil unless count.positive?
|
||||||
|
|
||||||
count
|
count
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def notification_cache_key = "#{cache_key}-#{notifications_updated_at}"
|
def notification_cache_key = "#{cache_key}/unread_notification_count-#{notifications_updated_at}"
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,50 +3,6 @@
|
||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
describe ApplicationHelper, type: :helper do
|
describe ApplicationHelper, type: :helper do
|
||||||
describe "#inbox_count" do
|
|
||||||
context "no signed in user" do
|
|
||||||
it "should return 0 as inbox count" do
|
|
||||||
expect(helper.inbox_count).to eq(0)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "user is signed in" do
|
|
||||||
let(:user) { FactoryBot.create(:user) }
|
|
||||||
let(:question) { FactoryBot.create(:question) }
|
|
||||||
|
|
||||||
before do
|
|
||||||
sign_in(user)
|
|
||||||
Inbox.create(user_id: user.id, question_id: question.id, new: true)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return the inbox count" do
|
|
||||||
expect(helper.inbox_count).to eq(1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#notification_count" do
|
|
||||||
context "no signed in user" do
|
|
||||||
it "should return 0 as notification count" do
|
|
||||||
expect(helper.notification_count).to eq(0)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "user is signed in" do
|
|
||||||
let(:user) { FactoryBot.create(:user) }
|
|
||||||
let(:another_user) { FactoryBot.create(:user) }
|
|
||||||
|
|
||||||
before do
|
|
||||||
sign_in(user)
|
|
||||||
another_user.follow(user)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return the notification count" do
|
|
||||||
expect(helper.notification_count).to eq(1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#privileged" do
|
describe "#privileged" do
|
||||||
context "current user and checked user do not match" do
|
context "current user and checked user do not match" do
|
||||||
let(:user) { FactoryBot.create(:user) }
|
let(:user) { FactoryBot.create(:user) }
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "rails_helper"
|
||||||
|
|
||||||
|
describe User::InboxMethods do
|
||||||
|
context "given a user" do
|
||||||
|
let(:user) { FactoryBot.create(:user) }
|
||||||
|
|
||||||
|
describe "#unread_inbox_count" do
|
||||||
|
subject { user.unread_inbox_count }
|
||||||
|
|
||||||
|
context "user has no questions in their inbox" do
|
||||||
|
it "should return nil" do
|
||||||
|
expect(subject).to eq(nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "user has 1 question in their inbox" do
|
||||||
|
# FactoryBot seems to have issues with setting the +new+ field on inbox entries
|
||||||
|
# so we can create it manually instead
|
||||||
|
let!(:inbox) { Inbox.create(question: FactoryBot.create(:question), user:, new: true) }
|
||||||
|
|
||||||
|
it "should return 1" do
|
||||||
|
expect(subject).to eq(1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,31 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "rails_helper"
|
||||||
|
|
||||||
|
describe User::NotificationMethods do
|
||||||
|
context "given a user" do
|
||||||
|
let(:user) { FactoryBot.create(:user) }
|
||||||
|
|
||||||
|
describe "#unread_notification_count" do
|
||||||
|
subject { user.unread_notification_count }
|
||||||
|
|
||||||
|
context "user has no notifications" do
|
||||||
|
it "should return nil" do
|
||||||
|
expect(subject).to eq(nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "user has a notification" do
|
||||||
|
let(:other_user) { FactoryBot.create(:user) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
other_user.follow(user)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return 1" do
|
||||||
|
expect(subject).to eq(1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue