dawarich/spec/requests/users/digests_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

141 lines
3.8 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe '/digests', type: :request do
context 'when user is not signed in' do
describe 'GET /index' do
it 'redirects to the sign in page' do
get users_digests_url
expect(response.status).to eq(302)
end
end
describe 'GET /show' do
it 'redirects to the sign in page' do
get users_digest_url(year: 2024)
expect(response).to redirect_to(new_user_session_path)
end
end
describe 'POST /create' do
it 'redirects to the sign in page' do
post users_digests_url, params: { year: 2024 }
expect(response.status).to eq(302)
end
end
end
context 'when user is signed in' do
let(:user) { create(:user) }
before { sign_in user }
describe 'GET /index' do
it 'renders a successful response' do
get users_digests_url
expect(response.status).to eq(200)
end
it 'displays existing digests' do
digest = create(:users_digest, user:, year: 2024)
get users_digests_url
expect(response.body).to include('2024')
end
it 'shows empty state when no digests exist' do
get users_digests_url
expect(response.body).to include('No Year-End Digests Yet')
end
end
describe 'GET /show' do
let!(:digest) { create(:users_digest, user:, year: 2024) }
it 'renders a successful response' do
get users_digest_url(year: 2024)
expect(response.status).to eq(200)
end
it 'includes digest content' do
get users_digest_url(year: 2024)
expect(response.body).to include('2024 Year in Review')
expect(response.body).to include('Distance Traveled')
end
it 'redirects when digest not found' do
get users_digest_url(year: 2020)
expect(response).to redirect_to(users_digests_path)
expect(flash[:alert]).to eq('Digest not found')
end
end
describe 'POST /create' do
context 'with valid year' do
before do
create(:stat, user:, year: 2024, month: 1)
end
it 'enqueues Users::Digests::CalculatingJob' do
post users_digests_url, params: { year: 2024 }
expect(Users::Digests::CalculatingJob).to have_been_enqueued.with(user.id, 2024)
end
it 'redirects with success notice' do
post users_digests_url, params: { year: 2024 }
expect(response).to redirect_to(users_digests_path)
expect(flash[:notice]).to include('is being generated')
end
end
context 'with invalid year' do
it 'redirects with alert for year with no stats' do
post users_digests_url, params: { year: 2024 }
expect(response).to redirect_to(users_digests_path)
expect(flash[:alert]).to eq('Invalid year selected')
end
it 'redirects with alert for year before 2000' do
post users_digests_url, params: { year: 1999 }
expect(response).to redirect_to(users_digests_path)
expect(flash[:alert]).to eq('Invalid year selected')
end
it 'redirects with alert for future year' do
post users_digests_url, params: { year: Time.current.year + 1 }
expect(response).to redirect_to(users_digests_path)
expect(flash[:alert]).to eq('Invalid year selected')
end
end
context 'when user is inactive' do
before do
create(:stat, user:, year: 2024, month: 1)
user.update(status: :inactive, active_until: 1.day.ago)
end
it 'returns an unauthorized response' do
post users_digests_url, params: { year: 2024 }
expect(response).to redirect_to(root_path)
expect(flash[:notice]).to eq('Your account is not active.')
end
end
end
end
end