dawarich/spec/jobs/users/digests/year_end_scheduling_job_spec.rb
Evgenii Burmakin 18b13fb915
Add yearly digest (#2073)
* Add yearly digest

* Rename YearlyDigests to Users::Digests

* Minor changes

* Update yearly digest layout and styles

* Add flags and chart to email

* Update colors

* Fix layout of stats in yearly digest view

* Remove cron job for yearly digest scheduling

* Update CHANGELOG.md

* Update digest email setting handling

* Allow sharing digest for 1 week or 1 month

* Change Digests Distance to Bigint

* Fix settings page
2025-12-28 17:33:35 +01:00

110 lines
3.6 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Users::Digests::YearEndSchedulingJob, type: :job do
describe '#perform' do
subject { described_class.perform_now }
let(:previous_year) { Time.current.year - 1 }
it 'enqueues to the digests queue' do
expect(described_class.new.queue_name).to eq('digests')
end
context 'with users having different statuses' do
let!(:active_user) { create(:user, status: :active) }
let!(:trial_user) { create(:user, status: :trial) }
let!(:inactive_user) { create(:user) }
before do
# Force inactive status after any after_commit callbacks
inactive_user.update_column(:status, 0) # inactive
create(:stat, user: active_user, year: previous_year, month: 1)
create(:stat, user: trial_user, year: previous_year, month: 1)
create(:stat, user: inactive_user, year: previous_year, month: 1)
allow(Users::Digests::CalculatingJob).to receive(:perform_later)
allow(Users::Digests::EmailSendingJob).to receive(:set).and_return(double(perform_later: nil))
end
it 'schedules jobs for active users' do
subject
expect(Users::Digests::CalculatingJob).to have_received(:perform_later)
.with(active_user.id, previous_year)
end
it 'schedules jobs for trial users' do
subject
expect(Users::Digests::CalculatingJob).to have_received(:perform_later)
.with(trial_user.id, previous_year)
end
it 'does not schedule jobs for inactive users' do
subject
expect(Users::Digests::CalculatingJob).not_to have_received(:perform_later)
.with(inactive_user.id, anything)
end
it 'schedules email sending job with delay' do
email_job_double = double(perform_later: nil)
allow(Users::Digests::EmailSendingJob).to receive(:set)
.with(wait: 30.minutes)
.and_return(email_job_double)
subject
expect(Users::Digests::EmailSendingJob).to have_received(:set)
.with(wait: 30.minutes).at_least(:twice)
end
end
context 'when user has no stats for previous year' do
let!(:user_without_stats) { create(:user, status: :active) }
let!(:user_with_stats) { create(:user, status: :active) }
before do
create(:stat, user: user_with_stats, year: previous_year, month: 1)
allow(Users::Digests::CalculatingJob).to receive(:perform_later)
allow(Users::Digests::EmailSendingJob).to receive(:set).and_return(double(perform_later: nil))
end
it 'does not schedule jobs for user without stats' do
subject
expect(Users::Digests::CalculatingJob).not_to have_received(:perform_later)
.with(user_without_stats.id, anything)
end
it 'schedules jobs for user with stats' do
subject
expect(Users::Digests::CalculatingJob).to have_received(:perform_later)
.with(user_with_stats.id, previous_year)
end
end
context 'when user only has stats for current year' do
let!(:user_current_year_only) { create(:user, status: :active) }
before do
create(:stat, user: user_current_year_only, year: Time.current.year, month: 1)
allow(Users::Digests::CalculatingJob).to receive(:perform_later)
allow(Users::Digests::EmailSendingJob).to receive(:set).and_return(double(perform_later: nil))
end
it 'does not schedule jobs for that user' do
subject
expect(Users::Digests::CalculatingJob).not_to have_received(:perform_later)
.with(user_current_year_only.id, anything)
end
end
end
end