Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notifications for volunteer birthdays (#5316) #5328

Merged
merged 9 commits into from
Nov 8, 2023
Prev Previous commit
Next Next commit
Add tests for volunteer birthday notifications
  • Loading branch information
davidgumberg committed Nov 8, 2023
commit 84f1bc2a81ca1437a191b535068a5be6b4ef26be
69 changes: 69 additions & 0 deletions spec/lib/tasks/volunteer_birthday_reminder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require "rails_helper"
require "rake"
Rake.application.rake_require "tasks/volunteer_birthday_reminder"

RSpec.describe "lib/tasks/volunteer_birthday_reminder.rake" do
let(:rake_task) { Rake::Task["volunteer_birthday_reminder"].invoke }

before do
Rake::Task.clear
Casa::Application.load_tasks

travel_to Date.new(2022, 10, 1)
davidgumberg marked this conversation as resolved.
Show resolved Hide resolved
end

after do
travel_back
end

context "there is a volunteer with a birthday next month" do
let!(:volunteer) do
create(:volunteer, :with_assigned_supervisor, date_of_birth: Date.new(1988, 11, 30))
end

it "creates a notification" do
expect { rake_task }.to change { volunteer.supervisor.notifications.count }.by(1)
end
end

context "there are many volunteers with birthdays next month" do
volunteer_count = 10
let!(:volunteer) do
create_list(:volunteer, volunteer_count, :with_assigned_supervisor, date_of_birth: Date.new(1988, 11, 30))
end

it "creates many notifications" do
expect { rake_task }.to change { Notification.count }.by(volunteer_count)
end
end

context "there is an unsupervised volunteer with a birthday next month" do
let!(:volunteer) do
create(:volunteer, date_of_birth: Date.new(1988, 11, 30))
end

it "does not create a notification" do
expect { rake_task }.to change { Notification.count }.by(0)
end
end

context "there is a volunteer with no date_of_birth" do
let!(:volunteer) do
create(:volunteer, :with_assigned_supervisor, date_of_birth: nil)
end

it "does not create a notification" do
expect { rake_task }.to change { volunteer.supervisor.notifications.count }.by(0)
end
end

context "there is a volunteer with a birthday that is not next month" do
let!(:volunteer) do
create(:volunteer, :with_assigned_supervisor, date_of_birth: Date.new(1998, 7, 16))
end

it "does not create a notification" do
expect { rake_task }.to change { volunteer.supervisor.notifications.count }.by(0)
end
end
end
57 changes: 57 additions & 0 deletions spec/models/volunteer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,63 @@
end
end

describe ".with_supervisor" do
subject { Volunteer.with_supervisor }

context "no volunteers" do
it { is_expected.to be_empty }
end

context "volunteers" do
let!(:unassigned1) { create(:volunteer, display_name: "aaa") }
let!(:unassigned2) { create(:volunteer, display_name: "bbb") }

let!(:supervisor1) { create(:supervisor, display_name: "supe1") }
let!(:assigned1) { create(:volunteer, display_name: "ccc") }
let!(:assignment1) { create(:supervisor_volunteer, volunteer: assigned1, supervisor: supervisor1) }

let!(:supervisor2) { create(:supervisor, display_name: "supe2") }
let!(:assigned2) { create(:volunteer, display_name: "ddd") }
let!(:assignment2) { create(:supervisor_volunteer, volunteer: assigned2, supervisor: supervisor2) }

let!(:assigned3) { create(:volunteer, display_name: "eee") }
let!(:assignment3) { create(:supervisor_volunteer, volunteer: assigned3, supervisor: supervisor2) }

it { is_expected.to contain_exactly(assigned1, assigned2, assigned3) }
end
end

describe ".birthday_next_month" do
subject { Volunteer.birthday_next_month }
before do
travel_to Date.new(2022, 10, 1)
end

after do
travel_back
end

context "there are volunteers whose birthdays are not next month" do
let!(:volunteer1) { create(:volunteer, date_of_birth: Date.new(1990, 9, 1)) }
let!(:volunteer2) { create(:volunteer, date_of_birth: Date.new(1998, 10, 15)) }
let!(:volunteer3) { create(:volunteer, date_of_birth: Date.new(1919, 12, 1)) }

it { is_expected.to be_empty }
end

context "there are volunteers whose birthdays are next month" do
let!(:volunteer1) { create(:volunteer, date_of_birth: Date.new(2001, 11, 1)) }
let!(:volunteer2) { create(:volunteer, date_of_birth: Date.new(1920, 11, 15)) }
let!(:volunteer3) { create(:volunteer, date_of_birth: Date.new(1989, 11, 30)) }

let!(:volunteer4) { create(:volunteer, date_of_birth: Date.new(2001, 6, 1)) }
let!(:volunteer5) { create(:volunteer, date_of_birth: Date.new(1920, 1, 15)) }
let!(:volunteer6) { create(:volunteer, date_of_birth: Date.new(1967, 2, 21)) }

it { is_expected.to contain_exactly(volunteer1, volunteer2, volunteer3) }
end
end

describe "#with_assigned_cases" do
let!(:volunteers) { create_list(:volunteer, 3) }
let!(:volunteer_with_cases) { create_list(:volunteer, 3, :with_casa_cases) }
Expand Down
19 changes: 19 additions & 0 deletions spec/notifications/volunteer_birthday_notification_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "rails_helper"

RSpec.describe VolunteerBirthdayNotification, type: :model do
describe "message" do
let(:volunteer) do
build(:volunteer, display_name: "Biday Sewn", date_of_birth: Date.new(1968, 2, 8))
end

let(:volunteer_notification) { VolunteerBirthdayNotification.with(volunteer: volunteer) }

it "contains a short ordinal form of the volunteer's date of birth" do
expect(volunteer_notification.message).to include "February 8th"
end

it "contains the volunteer's name" do
expect(volunteer_notification.message).to include "Biday Sewn"
end
end
end