|
11 | 11 | let(:patient) { create(:patient) } |
12 | 12 |
|
13 | 13 | before do |
14 | | - # Associate patient with teams through locations |
15 | 14 | create(:patient_location, patient:, session: session_a) |
16 | 15 | create(:patient_location, patient:, session: session_b) |
17 | 16 | end |
18 | 17 |
|
19 | 18 | describe "#perform" do |
| 19 | + subject(:perform) { described_class.new.perform(patient.id) } |
| 20 | + |
20 | 21 | context "when patient exists in multiple teams" do |
21 | 22 | context "deceased" do |
22 | | - it "creates deceased notices for all associated teams" do |
| 23 | + before do |
23 | 24 | patient.update_columns( |
24 | 25 | # if we use update! the jobs gets called automatically |
25 | 26 | date_of_death: Time.current, |
26 | 27 | date_of_death_recorded_at: Time.current |
27 | 28 | ) |
| 29 | + end |
28 | 30 |
|
29 | | - expect { described_class.new.perform(patient.id) }.to change { |
30 | | - team_a.important_notices.count |
31 | | - }.by(1).and change { team_b.important_notices.count }.by(1) |
| 31 | + it "creates deceased notices for all associated teams" do |
| 32 | + expect { perform }.to change { team_a.important_notices.count }.by( |
| 33 | + 1 |
| 34 | + ).and change { team_b.important_notices.count }.by(1) |
32 | 35 |
|
33 | 36 | expect(team_a.important_notices.first.type).to eq("deceased") |
34 | 37 | expect(team_a.important_notices.first.message).to eq( |
35 | 38 | "Record updated with child’s date of death" |
36 | 39 | ) |
| 40 | + |
| 41 | + expect { described_class.new.perform(patient.id) }.to not_change( |
| 42 | + team_a.important_notices, |
| 43 | + :count |
| 44 | + ).and not_change(team_b.important_notices, :count) |
37 | 45 | end |
38 | 46 | end |
39 | 47 |
|
40 | 48 | context "restricted" do |
| 49 | + before { patient.update_column(:restricted_at, Time.current) } |
| 50 | + |
41 | 51 | it "creates restricted notices for all associated teams" do |
42 | | - patient.update_column(:restricted_at, Time.current) |
43 | | - expect { described_class.new.perform(patient.id) }.to change( |
44 | | - ImportantNotice, |
45 | | - :count |
46 | | - ).by(2) |
| 52 | + expect { perform }.to change(ImportantNotice, :count).by(2) |
47 | 53 |
|
48 | 54 | notices = patient.important_notices.where(type: :restricted) |
49 | 55 | expect(notices.pluck(:team_id)).to contain_exactly( |
|
55 | 61 | end |
56 | 62 |
|
57 | 63 | context "patient is no longer restricted" do |
58 | | - it "dismisses existing restricted notices" do |
| 64 | + before do |
59 | 65 | patient.update!(restricted_at: Time.current) |
60 | 66 | patient.update_column(:restricted_at, nil) |
| 67 | + end |
61 | 68 |
|
62 | | - expect { described_class.new.perform(patient.id) }.to change { |
| 69 | + it "dismisses existing restricted notices" do |
| 70 | + expect { perform }.to change { |
63 | 71 | ImportantNotice |
64 | 72 | .active(team: team_a) |
65 | 73 | .where(patient:, type: :restricted) |
|
70 | 78 | end |
71 | 79 |
|
72 | 80 | context "invalidated" do |
73 | | - it "creates invalidated notices for all associated teams" do |
74 | | - patient.update_column(:invalidated_at, Time.current) |
| 81 | + before { patient.update_column(:invalidated_at, Time.current) } |
75 | 82 |
|
76 | | - expect { described_class.new.perform(patient.id) }.to change( |
77 | | - ImportantNotice, |
78 | | - :count |
79 | | - ).by(2) |
| 83 | + it "creates invalidated notices for all associated teams" do |
| 84 | + expect { perform }.to change(ImportantNotice, :count).by(2) |
80 | 85 |
|
81 | 86 | notices = patient.important_notices.where(type: :invalidated) |
82 | 87 | expect(notices.pluck(:team_id)).to contain_exactly( |
|
88 | 93 | end |
89 | 94 |
|
90 | 95 | context "patient is no longer invalidated" do |
91 | | - it "dismisses existing invalidated notices" do |
| 96 | + before do |
92 | 97 | patient.update!(invalidated_at: Time.current) |
93 | 98 | patient.update_column(:invalidated_at, nil) |
| 99 | + end |
94 | 100 |
|
95 | | - expect { described_class.new.perform(patient.id) }.to change { |
| 101 | + it "dismisses existing invalidated notices" do |
| 102 | + expect { perform }.to change { |
96 | 103 | ImportantNotice |
97 | 104 | .active(team: team_a) |
98 | 105 | .where(patient:, type: :invalidated) |
|
0 commit comments