docbrown/spec/models/follow_spec.rb
Anna Buianova 58b2013377 Fix delayed job errors related to follows #1621 (#1835)
* Add delayed_job_web to dev environment for debugging

* Add specs for the current Follow callbacks implementation

* Move follower touching to ActiveJob

* Spec for the touch followers job

* Move Follow#create_chat_channel to ActiveJob and make the job safe

* Add ActiveJob to send email notifications about follows

* Enqueue SendEmailNotificationJob after the follow is created

* Specs to wnsure jobs are enqueued on Follow creation

* Make CreateChatChannelJob queue name more specific
2019-02-23 18:16:20 -08:00

74 lines
2.2 KiB
Ruby

require "rails_helper"
RSpec.describe Follow, type: :model do
let(:user) { create(:user) }
let(:user_2) { create(:user) }
it "follows user" do
user.follow(user_2)
expect(user.following?(user_2)).to eq(true)
end
context "when enqueuing jobs" do
before { ActiveJob::Base.queue_adapter = :test }
it "enqueues touch follower job on creation" do
expect do
Follow.create(follower: user, followable: user_2)
end.to have_enqueued_job(Follows::TouchFollowerJob)
end
it "enqueues create channel job" do
expect do
Follow.create(follower: user, followable: user_2)
end.to have_enqueued_job(Follows::CreateChatChannelJob)
end
it "enqueues send notification job" do
expect do
Follow.create(follower: user, followable: user_2)
end.to have_enqueued_job(Follows::SendEmailNotificationJob)
end
end
context "when creating and inline" do
before { ActiveJob::Base.queue_adapter = :inline }
it "touches the follower user while creating" do
user.update_columns(updated_at: Time.now - 1.day, last_followed_at: Time.now - 1.day)
now = Time.now
Follow.create!(follower: user, followable: user_2)
user.reload
expect(user.updated_at).to be >= now
expect(user.last_followed_at).to be >= now
end
it "doesn't create a channel when a followable is an org" do
expect do
Follow.create!(follower: user, followable: create(:organization))
end.not_to change(ChatChannel, :count)
end
it "doesn't create a chat channel when users don't follow mutually" do
expect do
Follow.create!(follower: user, followable: user_2)
end.not_to change(ChatChannel, :count)
end
it "creates a chat channel when users follow mutually" do
Follow.create!(follower: user_2, followable: user)
expect do
Follow.create!(follower: user, followable: user_2)
end.to change(ChatChannel, :count).by(1)
end
it "sends an email notification" do
user_2.update_column(:email_follower_notifications, true)
expect do
run_background_jobs_immediately do
Follow.create!(follower: user, followable: user_2)
end
end.to change(EmailMessage, :count).by(1)
end
end
end