Skip to content

Commit 8b6a6e4

Browse files
test: Add tests for manual consent reminder behavior
1 parent 5e5a1c7 commit 8b6a6e4

File tree

3 files changed

+200
-9
lines changed

3 files changed

+200
-9
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@
5656
}
5757
]
5858
}
59-
}
59+
},
60+
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
6061
}

spec/jobs/send_automatic_school_consent_reminders_job_spec.rb

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
describe SendAutomaticSchoolConsentRemindersJob do
44
subject(:perform_now) { described_class.perform_now(session) }
55

6-
let(:programmes) { [create(:programme)] }
6+
let(:programmes) { [create(:programme, :flu)] }
77

88
let(:parents) { create_list(:parent, 2) }
99

10+
let(:manual_reminder_patient) { create(:patient, parents:, team:) }
11+
1012
let(:patient_with_initial_reminder_sent) do
1113
create(
1214
:patient,
@@ -19,9 +21,13 @@
1921
let(:patient_not_sent_reminder) do
2022
create(:patient, :consent_request_sent, parents:, programmes:)
2123
end
24+
let(:patient_vaccinated_last_year) do
25+
create(:patient, :consent_request_sent, parents:, programmes:)
26+
end
2227
let(:patient_not_sent_reminder_joined_after_first_date) do
2328
create(:patient, :consent_request_sent, parents:, programmes:)
2429
end
30+
2531
let(:patient_not_sent_request) { create(:patient, parents:, programmes:) }
2632
let(:patient_with_consent) do
2733
create(:patient, :consent_given_triage_not_needed, programmes:)
@@ -32,6 +38,7 @@
3238

3339
let!(:patients) do
3440
[
41+
manual_reminder_patient,
3542
patient_with_initial_reminder_sent,
3643
patient_not_sent_reminder,
3744
patient_not_sent_reminder_joined_after_first_date,
@@ -61,13 +68,25 @@
6168
)
6269
end
6370

71+
let(:user) { create(:user, team:) }
72+
6473
before do
6574
ConsentNotification.request.update_all(sent_at: dates.first - 1.week)
6675
ConsentNotification.reminder.update_all(sent_at: dates.first)
6776

6877
patient_not_sent_reminder_joined_after_first_date.consent_notifications.update_all(
6978
sent_at: dates.first + 1.day
7079
)
80+
81+
create(
82+
:consent_notification,
83+
patient: manual_reminder_patient,
84+
session:,
85+
programmes:,
86+
type: :initial_reminder,
87+
sent_at: dates.first - 9.days,
88+
sent_by: user
89+
)
7190
end
7291

7392
around { |example| travel_to(today) { example.run } }
@@ -85,16 +104,17 @@
85104
let(:today) { dates.first - 1.week }
86105

87106
it "sends notifications to one patient" do
88-
expect(ConsentNotification).to receive(:create_and_send!).once.with(
107+
expect(ConsentNotification).to receive(:create_and_send!).with(
89108
patient: patient_not_sent_reminder,
90-
programmes:,
109+
programmes: [programmes.first],
91110
session:,
92-
type: :initial_reminder
111+
type: :initial_reminder,
112+
current_user: nil
93113
)
94114
perform_now
95115
end
96116

97-
it "records a notification" do
117+
it "records notifications" do
98118
expect { perform_now }.to change(ConsentNotification, :count).by(1)
99119
end
100120

@@ -106,6 +126,52 @@
106126
perform_now
107127
end
108128
end
129+
130+
context "when a manual reminder was sent more than three days ago" do
131+
before do
132+
create(
133+
:consent_notification,
134+
patient: patient_not_sent_reminder,
135+
session:,
136+
programmes:,
137+
type: :initial_reminder,
138+
sent_at: 4.days.ago,
139+
sent_by: user
140+
)
141+
end
142+
143+
it "sends automatic reminders" do
144+
expect { perform_now }.to change(ConsentNotification, :count).by(1)
145+
146+
notification = ConsentNotification.last
147+
expect(notification.patient).to eq(patient_not_sent_reminder)
148+
expect(notification.programmes.length).to eq(1)
149+
expect(notification.automated_reminder?).to be true
150+
151+
programme_types = notification.programmes.map(&:type)
152+
expect(programme_types).to match_array(programmes.map(&:type))
153+
end
154+
end
155+
156+
context "when a manual reminder was sent less than three days ago" do
157+
let(:user) { create(:user, team:) }
158+
159+
before do
160+
create(
161+
:consent_notification,
162+
patient: patient_not_sent_reminder,
163+
session:,
164+
programmes:,
165+
type: :initial_reminder,
166+
sent_at: 2.days.ago,
167+
sent_by: user
168+
)
169+
end
170+
171+
it "does not send an automatic reminder" do
172+
expect { perform_now }.not_to change(ConsentNotification, :count)
173+
end
174+
end
109175
end
110176

111177
context "six days before the first session with reminders already sent" do
@@ -126,6 +192,31 @@
126192
end
127193
end
128194

195+
context "five days before the first session, eight before the second" do
196+
let(:today) { dates.first - 5.days }
197+
let(:user) { create(:user, team:) }
198+
199+
context "when a manual reminder was sent less than three days before the reminder should have gone out" do
200+
it "does not send an automatic reminder because the first session's reminder should be skipped" do
201+
expect { perform_now }.not_to change(
202+
ConsentNotification.where(patient_id: manual_reminder_patient.id),
203+
:count
204+
)
205+
end
206+
end
207+
end
208+
209+
context "2 days before the first session, six before the second" do
210+
let(:today) { dates.first - 2.days }
211+
let(:user) { create(:user, team:) }
212+
213+
context "when a manual reminder was sent less than three days before the reminder should have gone out" do
214+
it "sends the reminder for the second session" do
215+
expect { perform_now }.to change(ConsentNotification, :count).by(1)
216+
end
217+
end
218+
end
219+
129220
context "one day after the first session" do
130221
let(:today) { dates.first + 1.day }
131222

@@ -153,21 +244,24 @@
153244
patient: patient_not_sent_reminder,
154245
programmes:,
155246
session:,
156-
type: :initial_reminder
247+
type: :initial_reminder,
248+
current_user: nil
157249
)
158250

159251
expect(ConsentNotification).to receive(:create_and_send!).once.with(
160252
patient: patient_not_sent_reminder_joined_after_first_date,
161253
programmes:,
162254
session:,
163-
type: :initial_reminder
255+
type: :initial_reminder,
256+
current_user: nil
164257
)
165258

166259
expect(ConsentNotification).to receive(:create_and_send!).once.with(
167260
patient: patient_with_initial_reminder_sent,
168261
programmes:,
169262
session:,
170-
type: :subsequent_reminder
263+
type: :subsequent_reminder,
264+
current_user: nil
171265
)
172266

173267
perform_now
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# frozen_string_literal: true
2+
3+
describe SendManualSchoolConsentRemindersJob do
4+
subject(:perform_now) do
5+
described_class.perform_now(session, current_user: user)
6+
end
7+
8+
let(:programmes) { [create(:programme, :flu)] }
9+
# Create an initial consent request notification
10+
let(:request_notification) do
11+
create(
12+
:consent_notification,
13+
patient:,
14+
session:,
15+
programmes:,
16+
type: :request,
17+
sent_at: dates.first - 2.weeks
18+
)
19+
end
20+
let(:today) { dates.first - 1.week }
21+
let(:team) { create(:team, programmes:) }
22+
let(:location) { create(:school, team:) }
23+
let(:patient) { create(:patient, team:) }
24+
let(:user) { create(:user, team:) }
25+
26+
let(:dates) { [Date.new(2024, 1, 12), Date.new(2024, 1, 15)] }
27+
28+
let!(:session) do
29+
create(
30+
:session,
31+
dates:,
32+
send_consent_requests_at: dates.first - 3.weeks,
33+
days_before_consent_reminders: 7,
34+
location:,
35+
programmes:,
36+
team:
37+
)
38+
end
39+
40+
# Create a parent for the patient
41+
let(:parent) { create(:parent) }
42+
43+
before do
44+
create(:parent_relationship, patient:, parent:)
45+
create(:patient_session, patient:, session:, programmes:)
46+
patient.reload
47+
end
48+
49+
around { |example| travel_to(today) { example.run } }
50+
51+
context "when the patient has not consented or been vaccinated" do
52+
it "creates a notification" do
53+
expect { perform_now }.to change(ConsentNotification, :count).by(1)
54+
55+
last_notification = ConsentNotification.last
56+
expect(last_notification.patient).to eq(patient)
57+
expect(last_notification.programmes).to match_array(programmes)
58+
expect(last_notification.automated_reminder?).to be false
59+
expect(last_notification.sent_by).to eq(user)
60+
end
61+
end
62+
63+
context "when the patient has already replied to the consent form" do
64+
before do
65+
patient.consent_status(
66+
programme: programmes.first,
67+
academic_year: AcademicYear.current
68+
).refused!
69+
end
70+
71+
it "does not create a notification" do
72+
expect { perform_now }.not_to change(ConsentNotification, :count)
73+
end
74+
end
75+
76+
context "when the patient has already been vaccinated" do
77+
before do
78+
patient.vaccination_status(
79+
programme: programmes.first,
80+
academic_year: AcademicYear.current
81+
).vaccinated!
82+
end
83+
84+
it "does not create a notification" do
85+
expect { perform_now }.not_to change(ConsentNotification, :count)
86+
end
87+
end
88+
89+
context "when the patient has opted out of notifications" do
90+
before { patient.update(restricted_at: Time.current) }
91+
92+
it "does not create a notification" do
93+
expect { perform_now }.not_to change(ConsentNotification, :count)
94+
end
95+
end
96+
end

0 commit comments

Comments
 (0)