Skip to content

Commit

Permalink
ap-4509: Refactor spec
Browse files Browse the repository at this point in the history
  • Loading branch information
kmahern committed Oct 18, 2023
1 parent 5848af8 commit 248ad05
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 39 deletions.
2 changes: 1 addition & 1 deletion app/models/linked_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class LinkedApplication < ApplicationRecord
private

def cannot_link_self
errors.add(:linked_application_ref, "You cannot link an application to itself.") if lead_application == associated_application
errors.add(:linked_application_ref, I18n.t("activemodel.errors.models.linked_application.attributes.application_cannot_be_linked_to_itself.message")) if lead_application == associated_application
end
end
4 changes: 4 additions & 0 deletions config/locales/en/activemodel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,10 @@ en:
date_not_in_range: "The date you used delegated functions cannot be before %{months}"
client_declaration_confirmed:
accepted: Confirm this information is correct and that you'll get a signed declaration
linked_application:
attributes:
application_cannot_be_linked_to_itself:
message: Application cannot be linked to itself.
other_assets_declaration:
attributes:
valuable_items_value:
Expand Down
5 changes: 4 additions & 1 deletion spec/factories/linked_applications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
lead_application { lead_application }
associated_application { associated_application }
link_type_code { "FAMILY" }
link_type_description { "Family" }

trait :family do
link_type_code { "FAMILY" }
end
end
end
97 changes: 61 additions & 36 deletions spec/models/legal_aid_application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1746,61 +1746,86 @@
end

describe "#associated_applications" do
let(:lead_application) { create(:legal_aid_application) }
let(:associated_application) { create(:legal_aid_application) }
let(:associated_application_two) { create(:legal_aid_application) }
subject(:associated_applications) { application_one.associated_applications }

it "returns an array of associated_applications" do
linked_application_one = LinkedApplication.create!(lead_application:, associated_application:, link_type_description: "link", link_type_code: "LNK")
linked_application_two = LinkedApplication.create!(lead_application:, associated_application: associated_application_two, link_type_description: "link", link_type_code: "LNK")
let(:application_one) { create(:legal_aid_application) }
let(:application_two) { create(:legal_aid_application) }
let(:application_three) { create(:legal_aid_application) }

expect(lead_application.associated_applications).to contain_exactly(associated_application, associated_application_two)
expect(lead_application.associated_linked_applications).to contain_exactly(linked_application_one, linked_application_two)
context "when application has no linked applications" do
it { is_expected.to eq [] }
end

it "does not allow an a application to be linked to the same application twice" do
LinkedApplication.create!(lead_application:, associated_application:, link_type_description: "link", link_type_code: "LNK")
expect { LinkedApplication.create!(lead_application:, associated_application:, link_type_description: "link", link_type_code: "LNK") }.to raise_error ActiveRecord::RecordNotUnique
end
context "when application is a lead applicaton" do
before do
create(:linked_application, :family, lead_application: application_one, associated_application: application_two)
create(:linked_application, :family, lead_application: application_one, associated_application: application_three)
end

it "does not allow an application to be linked to itself" do
expect { LinkedApplication.create!(lead_application:, associated_application: lead_application, link_type_description: "link", link_type_code: "LNK") }.to raise_error ActiveRecord::RecordInvalid, "Validation failed: Linked application ref You cannot link an application to itself."
it "returns an array of associated applications" do
expect(associated_applications).to contain_exactly(application_two, application_three)
end
end

it "does not allow an a application to be linked to a nonexistent application" do
expect { LinkedApplication.create!(lead_application_id: "11111111-2222-3333-4444-555555555555", associated_application:, link_type_description: "link", link_type_code: "LNK") }.to raise_error ActiveRecord::RecordInvalid, "Validation failed: Lead application must exist"
context "when associated application is the same as lead application" do
it "does not allow an application to be linked to itself" do
expect { create(:linked_application, :family, lead_application: application_one, associated_application: application_one) }.to raise_error ActiveRecord::RecordInvalid, "Validation failed: Linked application ref Application cannot be linked to itself."
end
end

it "calling destroy! deletes the linked_application record but not the linked appication itself" do
linked_application = LinkedApplication.create!(lead_application:, associated_application:, link_type_description: "link", link_type_code: "LNK")
associated_application.destroy!
expect { described_class.find(associated_application.id) }.to raise_error ActiveRecord::RecordNotFound
expect { LinkedApplication.find(linked_application.id) }.to raise_error ActiveRecord::RecordNotFound
expect(described_class.find(lead_application.id)).to eq lead_application
context "when destroying an associated application" do
before do
create(:linked_application, :family, lead_application: application_one, associated_application: application_two)
end

it "destroys the link but not the associated application" do
expect { application_two.destroy! }
.to change { application_one.reload.associated_applications }
.from([application_two])
.to([])
end
end
end

describe "#lead_application" do
let(:lead_application) { create(:legal_aid_application) }
let(:associated_application) { create(:legal_aid_application) }
subject(:lead_application) { application_one.lead_application }

let(:application_one) { create(:legal_aid_application) }
let(:application_two) { create(:legal_aid_application) }

context "when application has no linked applications" do
it { is_expected.to be_nil }
end

it "returns the lead_application" do
linked_application = LinkedApplication.create!(lead_application:, associated_application:, link_type_description: "link", link_type_code: "LNK")
context "when application is the lead application" do
before do
create(:linked_application, :family, lead_application: application_one, associated_application: application_two)
end

expect(associated_application.lead_application).to eq lead_application
expect(associated_application.lead_linked_application).to eq linked_application
it { is_expected.to be_nil }
end

it "does not allow an a application to be linked to a nonexistent application" do
expect { LinkedApplication.create!(lead_application:, associated_application_id: "11111111-2222-3333-4444-555555555555", link_type_description: "link", link_type_code: "LNK") }.to raise_error ActiveRecord::RecordInvalid, "Validation failed: Associated application must exist"
context "when application is an associated applicaton" do
before do
create(:linked_application, :family, lead_application: application_two, associated_application: application_one)
end

it "returns the single lead application" do
expect(lead_application).to eq application_two
end
end

it "calling destroy! deletes the linked_application record but not the linked appication itself" do
linked_application = LinkedApplication.create!(lead_application:, associated_application:, link_type_description: "link", link_type_code: "LNK")
lead_application.destroy!
expect { described_class.find(lead_application.id) }.to raise_error ActiveRecord::RecordNotFound
expect { LinkedApplication.find(linked_application.id) }.to raise_error ActiveRecord::RecordNotFound
expect(described_class.find(associated_application.id)).to eq associated_application
context "when destroying a lead application" do
before do
create(:linked_application, :family, lead_application: application_one, associated_application: application_two)
end

it "destroys the link but not the associated application" do
expect { application_one.destroy! }
.to change { application_two.reload.lead_application }
.from(application_one)
.to(nil)
end
end
end

Expand Down
1 change: 0 additions & 1 deletion spec/models/linked_application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
it { expect(linked_application.lead_application).to eq lead_application }
it { expect(linked_application.associated_application).to eq associated_application }
it { expect(linked_application.link_type_code).to eq "FAMILY" }
it { expect(linked_application.link_type_description).to eq "Family" }
end

0 comments on commit 248ad05

Please sign in to comment.