From 29489c0698f845353b1ed7cdb945f489ce20f875 Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 10 Dec 2024 11:29:31 +0000 Subject: [PATCH] Fix bugs, add tests (2597, 2599, 2598) --- .../case-edit-page.component.spec.ts | 95 ++++++++++++------- .../case-edit-page.component.ts | 11 +++ .../case-edit-submit.component.spec.ts | 23 ++++- .../case-edit-submit.component.ts | 15 +++ .../confirm-flag-status.component.ts | 3 + .../before-you-start.component.spec.ts | 61 +++++++++++- .../before-you-start.component.ts | 17 +++- .../services/linked-cases.service.ts | 7 ++ 8 files changed, 191 insertions(+), 41 deletions(-) diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.spec.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.spec.ts index 48bce09de8..97186dd681 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.spec.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.spec.ts @@ -64,6 +64,7 @@ import { ShowCondition } from '../../../directives'; import createSpyObj = jasmine.createSpyObj; import { LinkedCasesService } from '../../palette/linked-cases/services/linked-cases.service'; import { CaseFlagStateService } from '../services/case-flag-state.service'; +import { CaseTab } from 'ccd-case-ui-toolkit'; describe('CaseEditPageComponent - creation and update event trigger tests', () => { let component: CaseEditPageComponent; @@ -77,11 +78,11 @@ describe('CaseEditPageComponent - creation and update event trigger tests', () = const CASE_TYPES_2 = [ { - id: 'Benefit_Xui', - name: 'Benefit_Xui', - description: '', - states: [], - events: [], + id: 'Benefit_Xui', + name: 'Benefit_Xui', + description: '', + states: [], + events: [], }]; const MOCK_JURISDICTION: Jurisdiction[] = [{ id: 'JURI_1', @@ -113,7 +114,7 @@ describe('CaseEditPageComponent - creation and update event trigger tests', () = linkedCasesService = {}, caseFlagStateService = new CaseFlagStateService() }) => - new CaseEditPageComponent( + new CaseEditPageComponent( caseEdit as CaseEditComponent, route as ActivatedRoute, formValueService as FormValueService, @@ -129,7 +130,7 @@ describe('CaseEditPageComponent - creation and update event trigger tests', () = addressesService as AddressesService, linkedCasesService as LinkedCasesService, caseFlagStateService as CaseFlagStateService - ); + ); it('should create', () => { component = initializeComponent({}); @@ -175,7 +176,7 @@ describe('CaseEditPageComponent - creation and update event trigger tests', () = }); describe('updateEventTriggerCaseFields', () => { - it(`should NOT update event trigger's case fields as eventTrigger is null`, () => { + it('should NOT update event trigger\'s case fields as eventTrigger is null', () => { component = initializeComponent({}); const caseFieldIdMock = 'bothDefendants'; const jsonDataMock = {} as unknown as CaseEventData; @@ -186,7 +187,7 @@ describe('CaseEditPageComponent - creation and update event trigger tests', () = expect(eventTriggerMock).toBeNull(); }); - it(`should NOT update event trigger's case fields as eventTrigger has no case fields`, () => { + it('should NOT update event trigger\'s case fields as eventTrigger has no case fields', () => { component = initializeComponent({}); const caseFieldIdMock = 'bothDefendants'; const jsonDataMock = {} as unknown as CaseEventData; @@ -197,7 +198,7 @@ describe('CaseEditPageComponent - creation and update event trigger tests', () = expect(eventTriggerMock).toEqual(jasmine.objectContaining({ id: 'noCaseFields' })); }); - it(`should update event trigger's case fields value`, () => { + it('should update event trigger\'s case fields value', () => { component = initializeComponent({}); const caseFieldIdMock = 'bothDefendants'; const jsonDataMock = { @@ -219,7 +220,7 @@ describe('CaseEditPageComponent - creation and update event trigger tests', () = expect(eventTriggerMock['case_fields'][0].value).toEqual(true); }); - it(`should update event trigger's case fields value with jsonData's object`, () => { + it('should update event trigger\'s case fields value with jsonData\'s object', () => { component = initializeComponent({}); const caseFieldIdMock = 'bothDefendants'; const result = { @@ -246,7 +247,7 @@ describe('CaseEditPageComponent - creation and update event trigger tests', () = expect(eventTriggerMock['case_fields'][0].value).toEqual(jasmine.objectContaining(result)); }); - it(`should NOT update event trigger's case fields value as jsonData's value is null`, () => { + it('should NOT update event trigger\'s case fields value as jsonData\'s value is null', () => { component = initializeComponent({}); const caseFieldIdMock = 'bothDefendants'; const result = { @@ -279,7 +280,7 @@ describe('CaseEditPageComponent - creation and update event trigger tests', () = expect(eventTriggerMock['case_fields'][0].value).toEqual(jasmine.objectContaining(result)); }); - it(`should NOT update event trigger's case fields value as jsonData's value is undefined`, () => { + it('should NOT update event trigger\'s case fields value as jsonData\'s value is undefined', () => { component = initializeComponent({}); const caseFieldIdMock = 'bothDefendants'; const result = { @@ -310,7 +311,7 @@ describe('CaseEditPageComponent - creation and update event trigger tests', () = expect(eventTriggerMock['case_fields'][0].value).toEqual(jasmine.objectContaining(result)); }); - it(`should update event trigger's case fields value as jsonData's value is present`, () => { + it('should update event trigger\'s case fields value as jsonData\'s value is present', () => { component = initializeComponent({}); const caseFieldIdMock = 'bothDefendants'; const result = { @@ -438,11 +439,11 @@ describe('CaseEditPageComponent - all other tests', () => { const CASE_TYPES_2 = [ { - id: 'Benefit_Xui', - name: 'Benefit_Xui', - description: '', - states: [], - events: [], + id: 'Benefit_Xui', + name: 'Benefit_Xui', + description: '', + states: [], + events: [], }]; const MOCK_JURISDICTION: Jurisdiction[] = [{ id: 'JURI_1', @@ -457,6 +458,7 @@ describe('CaseEditPageComponent - all other tests', () => { jurisdictionService.getJurisdictions.and.returnValue(of(MOCK_JURISDICTION)); const linkedCasesService = new LinkedCasesService(jurisdictionService, searchService); const caseFlagStateService = new CaseFlagStateService(); + let linkedCasesServiceSpy: jasmine.SpyObj; describe('Save and Resume enabled', () => { const eventTrigger = { @@ -576,7 +578,7 @@ describe('CaseEditPageComponent - all other tests', () => { { provide: FieldsUtils, useValue: fieldsUtils }, PlaceholderService, { provide: LoadingService, useValue: loadingServiceMock }, - { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService }, { provide: AddressesService, useValue: addressesServiceMock }, { provide: MultipageComponentStateService, useValue: multipageComponentStateService }, { provide: LinkedCasesService, useValue: linkedCasesService }, @@ -868,6 +870,29 @@ describe('CaseEditPageComponent - all other tests', () => { expect(caseFlagStateService.fieldStateToNavigate).toEqual(3); expect(caseFlagStateService.lastPageFieldState).toEqual(3); }); + + it('should remove any unsibmitted data on cancel', () => { + comp.eventTrigger = eventTrigger as CaseEventTrigger; + comp.currentPage = wizardPage; + comp.caseEdit.caseDetails.tabs = [{ + id: '123', + fields: [ + { + id: 'caseLinks', + value: [ + { value: { CaseReference: 'REF1' } }, + { value: { CaseReference: 'REF2' } }, + { value: { CaseReference: 'REF3' } } + ] + } + ] + }] as CaseTab[]; + linkedCasesServiceSpy = TestBed.inject(LinkedCasesService) as jasmine.SpyObj; + spyOn(comp, 'isLinkedCasesJourney').and.returnValues(true); + linkedCasesService.initialCaseLinkRefs = ['REF1']; + comp.cancel(); + expect(comp.caseEdit.caseDetails.tabs[0].fields[0].value).toEqual([{ 'value': { 'CaseReference': 'REF1' } }]); + }); }); describe('Save and Resume disabled', () => { @@ -984,7 +1009,7 @@ describe('CaseEditPageComponent - all other tests', () => { { provide: FieldsUtils, useValue: fieldsUtils }, PlaceholderService, { provide: LoadingService, useValue: loadingServiceMock }, - { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService }, { provide: AddressesService, useValue: addressesServiceMock }, { provide: LinkedCasesService, useValue: linkedCasesService }, { provide: CaseFlagStateService, useValue: caseFlagStateService } @@ -1153,7 +1178,7 @@ describe('CaseEditPageComponent - all other tests', () => { { provide: FieldsUtils, useValue: fieldsUtils }, PlaceholderService, { provide: LoadingService, useValue: loadingServiceMock }, - { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService }, { provide: AddressesService, useValue: addressesServiceMock }, { provide: LinkedCasesService, useValue: linkedCasesService }, { provide: CaseFlagStateService, useValue: caseFlagStateService } @@ -1327,7 +1352,7 @@ describe('CaseEditPageComponent - all other tests', () => { { provide: FieldsUtils, useValue: fieldsUtils }, PlaceholderService, { provide: LoadingService, useValue: loadingServiceMock }, - { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService }, { provide: AddressesService, useValue: addressesServiceMock }, { provide: LinkedCasesService, useValue: linkedCasesService }, { provide: CaseFlagStateService, useValue: caseFlagStateService } @@ -1676,8 +1701,8 @@ describe('CaseEditPageComponent - all other tests', () => { { provide: FieldsUtils, useValue: fieldsUtils }, PlaceholderService, { provide: LoadingService, useValue: loadingServiceMock }, - { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, - { provide: AddressesService, useValue: addressesServiceMock}, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService }, + { provide: AddressesService, useValue: addressesServiceMock }, { provide: LinkedCasesService, useValue: linkedCasesService }, { provide: CaseFlagStateService, useValue: caseFlagStateService } ], @@ -1732,7 +1757,7 @@ describe('CaseEditPageComponent - all other tests', () => { const value = control.value; if (!value) { - return {mockRequired:true}; + return { mockRequired: true }; } } } @@ -1787,7 +1812,7 @@ describe('CaseEditPageComponent - all other tests', () => { id: 'AddressLine1', label: 'AddressLine1', display_context: 'MANDATORY', - field_type: {id: 'Text', type: 'Text'}, + field_type: { id: 'Text', type: 'Text' }, value: '', } as CaseField; @@ -1897,7 +1922,7 @@ describe('CaseEditPageComponent - all other tests', () => { { provide: FieldsUtils, useValue: fieldsUtils }, PlaceholderService, { provide: LoadingService, useValue: loadingServiceMock }, - { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService}, + { provide: ValidPageListCaseFieldsService, useValue: validPageListCaseFieldsService }, { provide: AddressesService, useValue: addressesServiceMock }, { provide: LinkedCasesService, useValue: linkedCasesService }, { provide: CaseFlagStateService, useValue: caseFlagStateService } @@ -1944,7 +1969,7 @@ describe('CaseEditPageComponent - all other tests', () => { expect(comp.validationErrors.length).toBe(3); comp.generateErrorMessage(wizardPage.case_fields); comp.validationErrors.forEach((error) => { - expect(error.message).toEqual(`%FIELDLABEL% is required`); + expect(error.message).toEqual('%FIELDLABEL% is required'); }); }); @@ -1977,7 +2002,7 @@ describe('CaseEditPageComponent - all other tests', () => { comp.generateErrorMessage(wizardPage.case_fields); comp.validationErrors.forEach((error) => { expect(error.message).toEqual( - `The field that is causing the error cannot be determined but there is an error present. Please fill in more of the form` + 'The field that is causing the error cannot be determined but there is an error present. Please fill in more of the form' ); }); }); @@ -1997,7 +2022,7 @@ describe('CaseEditPageComponent - all other tests', () => { comp.generateErrorMessage(wizardPage.case_fields); comp.validationErrors.forEach((error) => { expect(error.message).toEqual( - `The field that is causing the error cannot be determined but there is an error present` + 'The field that is causing the error cannot be determined but there is an error present' ); }); }); @@ -2031,7 +2056,7 @@ describe('CaseEditPageComponent - all other tests', () => { comp.generateErrorMessage(wizardPage.case_fields); comp.validationErrors.forEach((error) => { expect(error.message).toEqual( - `%FIELDLABEL% is below the minimum length` + '%FIELDLABEL% is below the minimum length' ); }); }); @@ -2064,7 +2089,7 @@ describe('CaseEditPageComponent - all other tests', () => { comp.generateErrorMessage(wizardPage.case_fields); comp.validationErrors.forEach((error) => { - expect(error.message).toEqual(`%FIELDLABEL% exceeds the maximum length`); + expect(error.message).toEqual('%FIELDLABEL% exceeds the maximum length'); }); }); @@ -2112,7 +2137,7 @@ describe('CaseEditPageComponent - all other tests', () => { comp.generateErrorMessage(wizardPage.case_fields); comp.validationErrors.forEach((error) => { - expect(error.message).toEqual(`%FIELDLABEL% is required`); + expect(error.message).toEqual('%FIELDLABEL% is required'); }); }); @@ -2157,7 +2182,7 @@ describe('CaseEditPageComponent - all other tests', () => { expect(comp.validationErrors.length).toBe(1); comp.generateErrorMessage(wizardPage.case_fields); comp.validationErrors.forEach((error) => { - expect(error.message).toEqual(`There is an internal issue with complexField2 fields. The field that is causing the error cannot be determined but there is an error present`); + expect(error.message).toEqual('There is an internal issue with complexField2 fields. The field that is causing the error cannot be determined but there is an error present'); }); }); diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.ts index 3799888960..9247bb621e 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-page/case-edit-page.component.ts @@ -494,6 +494,17 @@ export class CaseEditPageComponent implements OnInit, AfterViewChecked, OnDestro public cancel(): void { if (this.isLinkedCasesJourney()){ + // if the user cancels the journey we need to clean up the data + const linkedCasesTab = this.caseEdit.caseDetails.tabs.find((tab) => + tab?.fields?.some((field) => field.id === 'caseLinks') + )?.fields?.[0] ?? null; + const initalLinks = this.linkedCasesService.initialCaseLinkRefs; + if (linkedCasesTab && linkedCasesTab?.value.length !== initalLinks.length){ + const initialCaseRefs = this.linkedCasesService.initialCaseLinkRefs; + linkedCasesTab.value = linkedCasesTab.value.filter((item) => + initialCaseRefs.includes(item.value.CaseReference) + ); + } this.linkedCasesService.resetLinkedCaseData(); } if (this.eventTrigger.can_save_draft) { diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-submit/case-edit-submit.component.spec.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-submit/case-edit-submit.component.spec.ts index 61dc6e41a1..16fdc2be7c 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-submit/case-edit-submit.component.spec.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-submit/case-edit-submit.component.spec.ts @@ -6,7 +6,7 @@ import { ActivatedRoute, Router } from '@angular/router'; import { BehaviorSubject, of } from 'rxjs'; import { PlaceholderService } from '../../../directives/substitutor/services'; -import { CaseField, Jurisdiction, Profile } from '../../../domain'; +import { CaseField, CaseTab, Jurisdiction, Profile } from '../../../domain'; import { createAProfile } from '../../../domain/profile/profile.test.fixture'; import { aCaseField } from '../../../fixture/shared.test.fixture'; import { CaseReferencePipe } from '../../../pipes/case-reference/case-reference.pipe'; @@ -132,6 +132,7 @@ describe('CaseEditSubmitComponent', () => { let profileNotifier: ProfileNotifier; let casesReferencePipe: jasmine.SpyObj; let formValidatorsService: jasmine.SpyObj; + let linkedCasesServiceSpy: jasmine.SpyObj; const caseField1: CaseField = aCaseField('field1', 'field1', 'Text', 'OPTIONAL', 4); const caseField2: CaseField = aCaseField('field2', 'field2', 'Text', 'OPTIONAL', 3, null, false, true); const caseField3: CaseField = aCaseField('field3', 'field3', 'Text', 'OPTIONAL', 2); @@ -803,6 +804,26 @@ describe('CaseEditSubmitComponent', () => { const previousButton = de.query(By.css('.button-secondary')); expect(previousButton.nativeElement.textContent).toContain('Previous'); }); + + it('should remove any unsibmitted data on cancel', () => { + comp.caseEdit.caseDetails.tabs = [{ + id: '123', + fields: [ + { + id: 'caseLinks', + value: [ + { value: { CaseReference: 'REF1' } }, + { value: { CaseReference: 'REF2' } }, + { value: { CaseReference: 'REF3' } } + ] + } + ] + }] as CaseTab[]; + caseEditComponent.isLinkedCasesSubmission = true; + linkedCasesService.initialCaseLinkRefs = ['REF1']; + comp.cancel(); + expect(comp.caseEdit.caseDetails.tabs[0].fields[0].value).toEqual([{ 'value': { 'CaseReference': 'REF1' } }]); + }); }); describe('CaseEditSubmitComponent without custom end button label and with Save and Resume enabled', () => { diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-submit/case-edit-submit.component.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-submit/case-edit-submit.component.ts index 881ebde8a7..03fb29f8ec 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-submit/case-edit-submit.component.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/case-editor/case-edit-submit/case-edit-submit.component.ts @@ -115,6 +115,10 @@ export class CaseEditSubmitComponent implements OnInit, OnDestroy { public submit(): void { if (this.summary.valid && this.description.valid) { + if (this.caseEdit.isLinkedCasesSubmission){ + // once the user submits the linked cases we need to reset the service so data isnt retained if they open the event again + this.linkedCasesService.resetLinkedCaseData(); + } this.caseEdit.submitForm({ eventTrigger: this.eventTrigger, form: this.editForm, @@ -174,6 +178,17 @@ export class CaseEditSubmitComponent implements OnInit, OnDestroy { public cancel(): void { if (this.caseEdit.isLinkedCasesSubmission){ + // if they cancel on the last CYA page we should ensure there is no unsubmitted data in the caseDetails + const linkedCasesTab = this.caseEdit.caseDetails.tabs.find((tab) => + tab?.fields?.some((field) => field.id === 'caseLinks') + )?.fields?.[0] ?? null; + const initalLinks = this.linkedCasesService.initialCaseLinkRefs; + if (linkedCasesTab && linkedCasesTab?.value.length !== initalLinks.length){ + const initialCaseRefs = this.linkedCasesService.initialCaseLinkRefs; + linkedCasesTab.value = linkedCasesTab.value.filter((item) => + initialCaseRefs.includes(item.value.CaseReference) + ); + } this.linkedCasesService.resetLinkedCaseData(); } if (this.eventTrigger.can_save_draft) { diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/case-flag/components/confirm-flag-status/confirm-flag-status.component.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/case-flag/components/confirm-flag-status/confirm-flag-status.component.ts index da3007af01..34a5178e42 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/case-flag/components/confirm-flag-status/confirm-flag-status.component.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/case-flag/components/confirm-flag-status/confirm-flag-status.component.ts @@ -42,6 +42,9 @@ export class ConfirmFlagStatusComponent extends AbstractJourneyComponent impleme this.formGroup.addControl(this.selectedStatusControlName, new FormControl(this.flagCreationStatuses.find( key => CaseFlagStatus[key] === this.defaultStatus))); this.formGroup.addControl(this.statusReasonControlName, new FormControl('')); + if (this.multipageComponentStateService.getJourneyCollectionMainObject().journeyPageNumber === CaseFlagFieldState.FLAG_STATUS) { + this.multipageComponentStateService.getJourneyCollectionMainObject().journeyPageNumber = CaseFlagFieldState.FLAG_COMMENTS; + } } public onNext(): void { diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/components/before-you-start/before-you-start.component.spec.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/components/before-you-start/before-you-start.component.spec.ts index 57e428d6b2..9d93348852 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/components/before-you-start/before-you-start.component.spec.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/components/before-you-start/before-you-start.component.spec.ts @@ -7,7 +7,7 @@ import { LinkedCasesPages } from '../../enums'; import { LinkedCasesService } from '../../services/linked-cases.service'; import { BeforeYouStartComponent } from './before-you-start.component'; -describe('BeforeYouStartComponent', () => { +fdescribe('BeforeYouStartComponent', () => { let component: BeforeYouStartComponent; let fixture: ComponentFixture; @@ -16,7 +16,9 @@ describe('BeforeYouStartComponent', () => { isLinkedCasesEventTrigger: false, caseFieldValue: [], linkedCases: [], + initialCaseLinkRefs: [], serverLinkedApiError: null, + hasContinuedFromStart: false, }; const mockRouter = { @@ -39,6 +41,10 @@ describe('BeforeYouStartComponent', () => { })); beforeEach(() => { + linkedCasesService.caseFieldValue = [{ id: '123' }]; + linkedCasesService.linkedCases = []; + linkedCasesService.hasContinuedFromStart = false; + linkedCasesService.initialCaseLinkRefs = []; fixture = TestBed.createComponent(BeforeYouStartComponent); component = fixture.componentInstance; spyOn(component.linkedCasesStateEmitter, 'emit'); @@ -87,4 +93,57 @@ describe('BeforeYouStartComponent', () => { component.next(); expect(component.next).toHaveBeenCalled(); }); + + it('should set the initialCaseLinkRefs when linkedCases is empty and caseFieldValue is not empty', () => { + expect(linkedCasesService.initialCaseLinkRefs).toEqual(['123']); + }); + + it('should set the initialCaseLinkRefs when linkedCases is not empty and caseFieldValue is not empty', () => { + expect(linkedCasesService.initialCaseLinkRefs).toEqual(['123']); + }); +}); + +fdescribe('BeforeYouStartComponent - secondary checks', () => { + let component: BeforeYouStartComponent; + let fixture: ComponentFixture; + + const linkedCasesService = { + caseId: '1682374819203471', + isLinkedCasesEventTrigger: false, + caseFieldValue: [{ id: '123' }], + linkedCases: [{ caseReference: '123' }], + initialCaseLinkRefs: [], + serverLinkedApiError: null, + hasContinuedFromStart: false, + }; + + const mockRouter = { + navigate: jasmine.createSpy('navigate').and.returnValue(Promise.resolve()), + url: '' + }; + + beforeEach(waitForAsync(() => { + TestBed.configureTestingModule({ + imports: [RouterTestingModule], + schemas: [CUSTOM_ELEMENTS_SCHEMA], + declarations: [BeforeYouStartComponent], + providers: [ + { provide: LinkedCasesService, useValue: linkedCasesService }, + { provide: Router, useValue: mockRouter }, + + ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(BeforeYouStartComponent); + component = fixture.componentInstance; + spyOn(component.linkedCasesStateEmitter, 'emit'); + fixture.detectChanges(); + }); + + it('should set the initialCaseLinkRefs when linkedCases is not empty and caseFieldValue is not empty', () => { + expect(linkedCasesService.initialCaseLinkRefs).toEqual(['123']); + }); }); diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/components/before-you-start/before-you-start.component.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/components/before-you-start/before-you-start.component.ts index b880eb9e0f..e1fdec5647 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/components/before-you-start/before-you-start.component.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/components/before-you-start/before-you-start.component.ts @@ -27,13 +27,22 @@ export class BeforeYouStartComponent extends AbstractJourneyComponent implements this.isLinkCasesJourney = this.linkedCasesService.isLinkedCasesEventTrigger; this.serverLinkedApiError = this.linkedCasesService.serverLinkedApiError; // re-initiate the state based on the casefield value - const linkedCaseRefereneIds = this.linkedCasesService.caseFieldValue.filter(item => item).map(item => item.id); - this.linkedCasesService.linkedCases = this.linkedCasesService.linkedCases.filter - (item => linkedCaseRefereneIds.indexOf(item.caseReference) !== -1); - this.linkedCasesService.initialCaseLinks = this.linkedCasesService.linkedCases; + const linkedCaseReferenceIds = this.linkedCasesService.caseFieldValue.filter((item) => item).map((item) => item.id); + this.linkedCasesService.linkedCases = this.linkedCasesService.linkedCases.filter((item) => linkedCaseReferenceIds.indexOf(item.caseReference) !== -1); + if (this.linkedCasesService.linkedCases.length === 0 && this.linkedCasesService.caseFieldValue.length !== 0 && !this.linkedCasesService.hasContinuedFromStart) { + this.linkedCasesService.caseFieldValue.forEach((item) => { + this.linkedCasesService.initialCaseLinkRefs.push(item.id); + }); + } else { + this.linkedCasesService.linkedCases.forEach((item) => { + this.linkedCasesService.initialCaseLinkRefs.push(item.caseReference); + }); + this.linkedCasesService.initialCaseLinks = this.linkedCasesService.linkedCases; + } } public onNext(): void { + this.linkedCasesService.hasContinuedFromStart = true; this.linkedCasesStateEmitter.emit({ currentLinkedCasesPage: LinkedCasesPages.BEFORE_YOU_START, errorMessages: this.errorMessages, diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/services/linked-cases.service.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/services/linked-cases.service.ts index 82e2b75504..115f01ca78 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/services/linked-cases.service.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/linked-cases/services/linked-cases.service.ts @@ -28,7 +28,9 @@ export class LinkedCasesService { public storedCaseNumber = ''; public cameFromFinalStep = false; public hasNavigatedInJourney = false; + public hasContinuedFromStart = false; public cachedFieldValues; + public initialCaseLinkRefs = []; constructor(private readonly jurisdictionService: JurisdictionService, private readonly searchService: SearchService) { @@ -58,11 +60,16 @@ export class LinkedCasesService { } public resetLinkedCaseData(): void { + //remove the newly added linked case using the linked case this.caseFieldValue = []; this.linkedCases = []; + this.initialCaseLinks = []; + this.initialCaseLinkRefs = []; this.storedCaseNumber = ''; this.cameFromFinalStep = false; this.hasNavigatedInJourney = false; + this.caseDetails = null; + this.hasContinuedFromStart = false; } public mapResponse(esSearchCasesResponse): any {