|
21 | 21 |
|
22 | 22 | describe "callbacks" do |
23 | 23 | it { is_expected.to callback(:unlock_timesheet_entry).before(:destroy) } |
| 24 | + |
| 25 | + describe "#unlock_timesheet_entry" do |
| 26 | + let(:company) { create(:company) } |
| 27 | + let(:client) { create(:client, company:) } |
| 28 | + let(:invoice) { create(:invoice, company:, client:, status: :draft) } |
| 29 | + let(:timesheet_entry) { create(:timesheet_entry, locked: true) } |
| 30 | + |
| 31 | + context "when invoice is in draft status" do |
| 32 | + context "when timesheet_entry is present" do |
| 33 | + let(:invoice_line_item) do |
| 34 | + create(:invoice_line_item, invoice:, timesheet_entry:) |
| 35 | + end |
| 36 | + |
| 37 | + it "unlocks the timesheet entry on destroy" do |
| 38 | + expect(timesheet_entry.locked).to be true |
| 39 | + invoice_line_item.destroy |
| 40 | + expect(timesheet_entry.reload.locked).to be false |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + context "when timesheet_entry is nil" do |
| 45 | + let(:invoice_line_item) do |
| 46 | + create(:invoice_line_item, invoice:, timesheet_entry: nil) |
| 47 | + end |
| 48 | + |
| 49 | + it "does not raise an error on destroy" do |
| 50 | + expect { invoice_line_item.destroy }.not_to raise_error |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context "when invoice is not in draft status" do |
| 56 | + let(:sent_invoice) { create(:invoice, company:, client:, status: :sent) } |
| 57 | + |
| 58 | + context "when timesheet_entry is present" do |
| 59 | + let(:invoice_line_item) do |
| 60 | + create(:invoice_line_item, invoice: sent_invoice, timesheet_entry:) |
| 61 | + end |
| 62 | + |
| 63 | + it "does not unlock the timesheet entry on destroy" do |
| 64 | + expect(timesheet_entry.locked).to be true |
| 65 | + invoice_line_item.destroy |
| 66 | + expect(timesheet_entry.reload.locked).to be true |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + context "when timesheet_entry is nil" do |
| 71 | + let(:invoice_line_item) do |
| 72 | + create(:invoice_line_item, invoice: sent_invoice, timesheet_entry: nil) |
| 73 | + end |
| 74 | + |
| 75 | + it "does not raise an error on destroy" do |
| 76 | + expect { invoice_line_item.destroy }.not_to raise_error |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | + end |
24 | 81 | end |
25 | 82 |
|
26 | 83 | describe "Associations" do |
|
0 commit comments