|
345 | 345 | expect(consents).not_to include(consent_next_year)
|
346 | 346 | end
|
347 | 347 | end
|
| 348 | + |
| 349 | + describe "#update_vaccination_records_no_notify" do |
| 350 | + let(:patient) { create(:patient) } |
| 351 | + let(:programme) { create(:programme, :hpv) } |
| 352 | + let(:consent) { create(:consent, patient:, programme:) } |
| 353 | + |
| 354 | + context "when vaccination records exist for the patient and programme" do |
| 355 | + let!(:first_vaccination_record) do |
| 356 | + create(:vaccination_record, patient:, programme:, notify_parents: false) |
| 357 | + end |
| 358 | + let!(:second_vaccination_record) do |
| 359 | + create(:vaccination_record, patient:, programme:, notify_parents: true) |
| 360 | + end |
| 361 | + let!(:other_patient_record) do |
| 362 | + create( |
| 363 | + :vaccination_record, |
| 364 | + patient: create(:patient), |
| 365 | + programme:, |
| 366 | + notify_parents: false |
| 367 | + ) |
| 368 | + end |
| 369 | + let!(:other_programme_record) do |
| 370 | + create( |
| 371 | + :vaccination_record, |
| 372 | + patient:, |
| 373 | + programme: create(:programme, :flu), |
| 374 | + notify_parents: false |
| 375 | + ) |
| 376 | + end |
| 377 | + |
| 378 | + before do |
| 379 | + allow(VaccinationNotificationCriteria).to receive(:call).with( |
| 380 | + vaccination_record: first_vaccination_record |
| 381 | + ).and_return(true) |
| 382 | + allow(VaccinationNotificationCriteria).to receive(:call).with( |
| 383 | + vaccination_record: second_vaccination_record |
| 384 | + ).and_return(false) |
| 385 | + end |
| 386 | + |
| 387 | + it "updates notify_parents for matching vaccination records" do |
| 388 | + expect { consent.update_vaccination_records_no_notify! }.to change { |
| 389 | + first_vaccination_record.reload.notify_parents |
| 390 | + }.from(false).to(true).and change { |
| 391 | + second_vaccination_record.reload.notify_parents |
| 392 | + }.from(true).to(false) |
| 393 | + end |
| 394 | + |
| 395 | + it "does not update vaccination records for other patients" do |
| 396 | + expect { consent.update_vaccination_records_no_notify! }.not_to( |
| 397 | + change { other_patient_record.reload.notify_parents } |
| 398 | + ) |
| 399 | + end |
| 400 | + |
| 401 | + it "does not update vaccination records for other programmes" do |
| 402 | + expect { consent.update_vaccination_records_no_notify! }.not_to( |
| 403 | + change { other_programme_record.reload.notify_parents } |
| 404 | + ) |
| 405 | + end |
| 406 | + |
| 407 | + it "calls VaccinationNotificationCriteria for each matching record" do |
| 408 | + consent.update_vaccination_records_no_notify! |
| 409 | + |
| 410 | + expect(VaccinationNotificationCriteria).to have_received(:call).with( |
| 411 | + vaccination_record: first_vaccination_record |
| 412 | + ) |
| 413 | + expect(VaccinationNotificationCriteria).to have_received(:call).with( |
| 414 | + vaccination_record: second_vaccination_record |
| 415 | + ) |
| 416 | + end |
| 417 | + end |
| 418 | + |
| 419 | + context "with multiple vaccination records and mixed results" do |
| 420 | + let!(:vaccination_records) do |
| 421 | + Array.new(3) do |
| 422 | + create( |
| 423 | + :vaccination_record, |
| 424 | + patient:, |
| 425 | + programme:, |
| 426 | + notify_parents: false |
| 427 | + ) |
| 428 | + end |
| 429 | + end |
| 430 | + |
| 431 | + before do |
| 432 | + allow(VaccinationNotificationCriteria).to receive(:call).with( |
| 433 | + vaccination_record: vaccination_records[0] |
| 434 | + ).and_return(true) |
| 435 | + allow(VaccinationNotificationCriteria).to receive(:call).with( |
| 436 | + vaccination_record: vaccination_records[1] |
| 437 | + ).and_return(nil) |
| 438 | + allow(VaccinationNotificationCriteria).to receive(:call).with( |
| 439 | + vaccination_record: vaccination_records[2] |
| 440 | + ).and_return(false) |
| 441 | + end |
| 442 | + |
| 443 | + it "updates each vaccination record according to the criteria result" do |
| 444 | + expect { consent.update_vaccination_records_no_notify! }.to change { |
| 445 | + vaccination_records[0].reload.notify_parents |
| 446 | + }.from(false).to(true).and change { |
| 447 | + vaccination_records[1].reload.notify_parents |
| 448 | + } |
| 449 | + .from(false) |
| 450 | + .to(nil) |
| 451 | + .and( |
| 452 | + not_change { vaccination_records[2].reload.notify_parents } |
| 453 | + ) |
| 454 | + end |
| 455 | + end |
| 456 | + end |
348 | 457 | end
|
0 commit comments