From 248ad05c7b48e4169b0fc2943036f8a273631bad Mon Sep 17 00:00:00 2001 From: Katharine Ahern Date: Wed, 18 Oct 2023 08:16:34 +0100 Subject: [PATCH] ap-4509: Refactor spec --- app/models/linked_application.rb | 2 +- config/locales/en/activemodel.yml | 4 + spec/factories/linked_applications.rb | 5 +- spec/models/legal_aid_application_spec.rb | 97 ++++++++++++++--------- spec/models/linked_application_spec.rb | 1 - 5 files changed, 70 insertions(+), 39 deletions(-) diff --git a/app/models/linked_application.rb b/app/models/linked_application.rb index de665e2ee0d..36d16d8b5ba 100644 --- a/app/models/linked_application.rb +++ b/app/models/linked_application.rb @@ -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 diff --git a/config/locales/en/activemodel.yml b/config/locales/en/activemodel.yml index 8f48dfff330..ea7b020281a 100644 --- a/config/locales/en/activemodel.yml +++ b/config/locales/en/activemodel.yml @@ -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: diff --git a/spec/factories/linked_applications.rb b/spec/factories/linked_applications.rb index 88f18d827e9..7aff8272ad1 100644 --- a/spec/factories/linked_applications.rb +++ b/spec/factories/linked_applications.rb @@ -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 diff --git a/spec/models/legal_aid_application_spec.rb b/spec/models/legal_aid_application_spec.rb index ed50890a116..91c293c569b 100644 --- a/spec/models/legal_aid_application_spec.rb +++ b/spec/models/legal_aid_application_spec.rb @@ -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 diff --git a/spec/models/linked_application_spec.rb b/spec/models/linked_application_spec.rb index 51f3ee6c2fe..8ddd962b0f3 100644 --- a/spec/models/linked_application_spec.rb +++ b/spec/models/linked_application_spec.rb @@ -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