2022-03-27 05:04:04 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
|
|
|
|
|
|
|
describe NotificationsController do
|
2023-05-07 11:54:54 -07:00
|
|
|
include ActiveSupport::Testing::TimeHelpers
|
|
|
|
|
2023-03-10 12:16:52 -08:00
|
|
|
describe "#index" do
|
|
|
|
subject { get :index, params: { type: :new } }
|
2022-03-27 05:04:04 -07:00
|
|
|
|
2023-05-07 11:54:54 -07:00
|
|
|
let(:original_notifications_updated_at) { 1.day.ago }
|
2023-03-10 12:16:52 -08:00
|
|
|
let(:user) { FactoryBot.create(:user) }
|
2022-03-27 05:04:04 -07:00
|
|
|
|
2023-03-10 12:16:52 -08:00
|
|
|
before do
|
|
|
|
sign_in(user)
|
2022-03-27 05:04:04 -07:00
|
|
|
end
|
|
|
|
|
2023-03-10 12:16:52 -08:00
|
|
|
context "user has no notifications" do
|
|
|
|
it "should show an empty list" do
|
|
|
|
subject
|
|
|
|
expect(response).to render_template(:index)
|
|
|
|
|
|
|
|
expect(controller.instance_variable_get(:@notifications)).to be_empty
|
|
|
|
end
|
2022-03-27 05:04:04 -07:00
|
|
|
end
|
2023-01-24 06:54:41 -08:00
|
|
|
|
2023-03-10 12:16:52 -08:00
|
|
|
context "user has notifications" do
|
|
|
|
let(:other_user) { FactoryBot.create(:user) }
|
|
|
|
let(:another_user) { FactoryBot.create(:user) }
|
2023-03-10 12:38:44 -08:00
|
|
|
let(:question) { FactoryBot.create(:question, user:) }
|
|
|
|
let!(:answer) { FactoryBot.create(:answer, question:, user: other_user) }
|
|
|
|
let!(:subscription) { Subscription.create(user:, answer:) }
|
|
|
|
let!(:comment) { FactoryBot.create(:comment, answer:, user: other_user) }
|
2023-03-10 12:16:52 -08:00
|
|
|
|
|
|
|
it "should show a list of notifications" do
|
|
|
|
subject
|
|
|
|
expect(response).to render_template(:index)
|
|
|
|
expect(controller.instance_variable_get(:@notifications)).to have_attributes(size: 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "marks notifications as read" do
|
|
|
|
expect { subject }.to change { Notification.for(user).where(new: true).count }.from(2).to(0)
|
|
|
|
end
|
2023-05-07 11:54:54 -07:00
|
|
|
|
|
|
|
it "updates the the timestamp used for caching" do
|
|
|
|
user.update(notifications_updated_at: original_notifications_updated_at)
|
|
|
|
travel 1.second do
|
2023-05-07 12:04:21 -07:00
|
|
|
expect { subject }.to change { user.reload.notifications_updated_at.floor }.from(original_notifications_updated_at.floor).to(Time.now.utc.floor)
|
2023-05-07 11:54:54 -07:00
|
|
|
end
|
|
|
|
end
|
2023-01-24 06:54:41 -08:00
|
|
|
end
|
2022-03-27 05:04:04 -07:00
|
|
|
end
|
2023-03-10 12:36:43 -08:00
|
|
|
|
|
|
|
describe "#read" do
|
|
|
|
subject { post :read, format: :turbo_stream }
|
|
|
|
|
|
|
|
let(:recipient) { FactoryBot.create(:user) }
|
|
|
|
let(:notification_count) { rand(8..20) }
|
|
|
|
|
|
|
|
context "user has some unread notifications" do
|
|
|
|
before do
|
|
|
|
notification_count.times do
|
|
|
|
FactoryBot.create(:user).follow(recipient)
|
|
|
|
end
|
|
|
|
|
|
|
|
sign_in(recipient)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "marks all notifications as read" do
|
|
|
|
expect { subject }.to change { Notification.where(recipient:, new: true).count }.from(notification_count).to(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-03-27 05:04:04 -07:00
|
|
|
end
|