diff --git a/src/app/flows/fines/fines-mac/fines-mac-delete-account-confirmation/fines-mac-delete-account-confirmation.component.spec.ts b/src/app/flows/fines/fines-mac/fines-mac-delete-account-confirmation/fines-mac-delete-account-confirmation.component.spec.ts index 08ad57fafb..0befadfbd9 100644 --- a/src/app/flows/fines/fines-mac/fines-mac-delete-account-confirmation/fines-mac-delete-account-confirmation.component.spec.ts +++ b/src/app/flows/fines/fines-mac/fines-mac-delete-account-confirmation/fines-mac-delete-account-confirmation.component.spec.ts @@ -37,6 +37,7 @@ describe('FinesMacDeleteAccountConfirmationComponent', () => { }; mockRouter = { navigate: vi.fn().mockName('Router.navigate'), + currentNavigation: vi.fn().mockName('Router.currentNavigation').mockReturnValue(undefined), }; await TestBed.configureTestingModule({ @@ -211,4 +212,25 @@ describe('FinesMacDeleteAccountConfirmationComponent', () => { component.referrer = component['setReferrer'](); expect(component.referrer).toBe(component['reviewAccountRoute']); }); + + it('should prefer navigation state referrer when provided', () => { + mockRouter.currentNavigation.mockReturnValue({ + extras: { + state: { + referrer: component['reviewAccountRoute'], + }, + }, + }); + + fixture.destroy(); + fixture = TestBed.createComponent(FinesMacDeleteAccountConfirmationComponent); + component = fixture.componentInstance; + finesMacStore = TestBed.inject(FinesMacStore); + finesMacStore.setFinesMacStore(structuredClone(FINES_MAC_STATE_MOCK)); + finesDraftStore = TestBed.inject(FinesDraftStore); + finesDraftStore.setFinesDraftState(FINES_DRAFT_STATE); + fixture.detectChanges(); + + expect(component.referrer).toBe(component['reviewAccountRoute']); + }); }); diff --git a/src/app/flows/fines/fines-mac/fines-mac-delete-account-confirmation/fines-mac-delete-account-confirmation.component.ts b/src/app/flows/fines/fines-mac/fines-mac-delete-account-confirmation/fines-mac-delete-account-confirmation.component.ts index 761c8785e3..04cd1de5c5 100644 --- a/src/app/flows/fines/fines-mac/fines-mac-delete-account-confirmation/fines-mac-delete-account-confirmation.component.ts +++ b/src/app/flows/fines/fines-mac/fines-mac-delete-account-confirmation/fines-mac-delete-account-confirmation.component.ts @@ -1,4 +1,4 @@ -import { ChangeDetectionStrategy, Component, inject, OnDestroy } from '@angular/core'; +import { ChangeDetectionStrategy, Component, inject, OnDestroy, OnInit } from '@angular/core'; import { FINES_MAC_ROUTING_PATHS } from '../routing/constants/fines-mac-routing-paths.constant'; import { FinesMacStore } from '../stores/fines-mac.store'; import { AbstractFormParentBaseComponent } from '@hmcts/opal-frontend-common/components/abstract/abstract-form-parent-base'; @@ -22,7 +22,10 @@ import { IOpalFinesDraftAccountPatchRequestPayload } from '@services/fines/opal- templateUrl: './fines-mac-delete-account-confirmation.component.html', changeDetection: ChangeDetectionStrategy.OnPush, }) -export class FinesMacDeleteAccountConfirmationComponent extends AbstractFormParentBaseComponent implements OnDestroy { +export class FinesMacDeleteAccountConfirmationComponent + extends AbstractFormParentBaseComponent + implements OnInit, OnDestroy +{ private readonly ngUnsubscribe = new Subject(); private readonly route = inject(ActivatedRoute); private readonly utilsService = inject(UtilsService); @@ -39,7 +42,7 @@ export class FinesMacDeleteAccountConfirmationComponent extends AbstractFormPare protected readonly opalFinesService = inject(OpalFines); public accountId: number | null = Number(this.route.snapshot.paramMap.get('draftAccountId')); - public referrer = this.setReferrer(); + public referrer = this.accountDetailsRoute; /** * Creates the payload for the PATCH request to delete an account. @@ -114,6 +117,10 @@ export class FinesMacDeleteAccountConfirmationComponent extends AbstractFormPare * @returns {string} The referrer path to navigate back to the review account page. */ private setReferrer(): string { + const navigationReferrer = (history.state?.['referrer'] ?? + this['router'].currentNavigation()?.extras.state?.['referrer']) as string | undefined; + if (navigationReferrer) return navigationReferrer; + if (this.finesDraftStore.checker()) return this.reviewAccountRoute; const accountType = this.finesMacStore.accountDetails().formData.fm_create_account_account_type; @@ -154,6 +161,10 @@ export class FinesMacDeleteAccountConfirmationComponent extends AbstractFormPare this.handlePatchRequest(payload); } + public ngOnInit(): void { + this.referrer = this.setReferrer(); + } + /** * Lifecycle hook that is called when the component is destroyed. * diff --git a/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account-decision/fines-mac-review-account-decision-form/fines-mac-review-account-decision-form.component.html b/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account-decision/fines-mac-review-account-decision-form/fines-mac-review-account-decision-form.component.html index dcc9b16a9e..80071593d1 100644 --- a/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account-decision/fines-mac-review-account-decision-form/fines-mac-review-account-decision-form.component.html +++ b/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account-decision/fines-mac-review-account-decision-form/fines-mac-review-account-decision-form.component.html @@ -49,7 +49,7 @@ Delete account diff --git a/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account-decision/fines-mac-review-account-decision-form/fines-mac-review-account-decision-form.component.spec.ts b/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account-decision/fines-mac-review-account-decision-form/fines-mac-review-account-decision-form.component.spec.ts index dee8889708..918efacc8d 100644 --- a/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account-decision/fines-mac-review-account-decision-form/fines-mac-review-account-decision-form.component.spec.ts +++ b/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account-decision/fines-mac-review-account-decision-form/fines-mac-review-account-decision-form.component.spec.ts @@ -5,6 +5,8 @@ import { FINES_MAC_REVIEW_ACCOUNT_DECISION_FORM_MOCK } from '../mocks/fines-mac- import { ActivatedRoute } from '@angular/router'; import { of } from 'rxjs'; import { GovukRadioComponent } from '@hmcts/opal-frontend-common/components/govuk/govuk-radio'; +import { FinesMacStore } from '../../../stores/fines-mac.store'; +import { FinesMacStoreType } from '../../../stores/types/fines-mac-store.type'; import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; describe('FinesMacReviewAccountDecisionFormComponent', () => { @@ -12,6 +14,7 @@ describe('FinesMacReviewAccountDecisionFormComponent', () => { let fixture: ComponentFixture; let formSubmit: IFinesMacReviewAccountDecisionForm; let originalInitOuterRadios: () => void; + let finesMacStore: FinesMacStoreType; beforeAll(() => { originalInitOuterRadios = GovukRadioComponent.prototype['initOuterRadios']; @@ -42,6 +45,7 @@ describe('FinesMacReviewAccountDecisionFormComponent', () => { fixture = TestBed.createComponent(FinesMacReviewAccountDecisionFormComponent); component = fixture.componentInstance; + finesMacStore = TestBed.inject(FinesMacStore); fixture.detectChanges(); }); @@ -61,12 +65,20 @@ describe('FinesMacReviewAccountDecisionFormComponent', () => { const event = new MouseEvent('click', { bubbles: true, cancelable: true }); // eslint-disable-next-line @typescript-eslint/no-explicit-any + const setDeleteFromCheckAccountSpy = vi.spyOn(finesMacStore, 'setDeleteFromCheckAccount'); + // eslint-disable-next-line @typescript-eslint/no-explicit-any const handleRouteSpy = vi.spyOn(component, 'handleRoute'); const expectedRoute = `${component.finesMacRoutes.children.deleteAccountConfirmation}/${component.accountId}`; link.dispatchEvent(event); - expect(handleRouteSpy).toHaveBeenCalledWith(expectedRoute, { event }); + expect(setDeleteFromCheckAccountSpy).toHaveBeenCalledWith(true); + expect(handleRouteSpy).toHaveBeenCalledWith(expectedRoute, { + event, + routeData: { + referrer: component.finesMacRoutes.children.reviewAccount, + }, + }); }); it('should not emit form submit event with form value', () => { diff --git a/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account-decision/fines-mac-review-account-decision-form/fines-mac-review-account-decision-form.component.ts b/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account-decision/fines-mac-review-account-decision-form/fines-mac-review-account-decision-form.component.ts index 2b8febf5ee..1751823a35 100644 --- a/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account-decision/fines-mac-review-account-decision-form/fines-mac-review-account-decision-form.component.ts +++ b/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account-decision/fines-mac-review-account-decision-form/fines-mac-review-account-decision-form.component.ts @@ -1,4 +1,4 @@ -import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; +import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output, inject } from '@angular/core'; import { AbstractFormBaseComponent } from '@hmcts/opal-frontend-common/components/abstract/abstract-form-base'; import { IFinesMacReviewAccountDecisionForm } from '../interfaces/fines-mac-review-account-decision-form.interface'; import { FINES_MAC_REVIEW_ACCOUNT_DECISION_OPTIONS } from '../constants/fines-mac-review-account-decision-options.constant'; @@ -12,6 +12,7 @@ import { import { GovukTextAreaComponent } from '@hmcts/opal-frontend-common/components/govuk/govuk-text-area'; import { GovukButtonComponent } from '@hmcts/opal-frontend-common/components/govuk/govuk-button'; import { FINES_MAC_ROUTING_PATHS } from '../../../routing/constants/fines-mac-routing-paths.constant'; +import { FinesMacStore } from '../../../stores/fines-mac.store'; import { IFinesMacReviewAccountDecisionFieldErrors } from '../interfaces/fines-mac-review-account-decision-field-errors.interface'; import { FINES_MAC_REVIEW_ACCOUNT_DECISION_FIELD_ERRORS } from '../constants/fines-mac-review-account-decision-field-errors.constant'; @@ -41,6 +42,7 @@ const ALPHANUMERIC_WITH_HYPHENS_SPACES_APOSTROPHES_DOT_PATTERN_VALIDATOR = patte export class FinesMacReviewAccountDecisionFormComponent extends AbstractFormBaseComponent implements OnInit { @Output() protected override formSubmit = new EventEmitter(); + protected readonly finesMacStore = inject(FinesMacStore); @Input({ required: true }) public accountId!: number; public readonly DECISION_OPTIONS = FINES_MAC_REVIEW_ACCOUNT_DECISION_OPTIONS; public readonly finesMacRoutes = FINES_MAC_ROUTING_PATHS; @@ -123,6 +125,24 @@ export class FinesMacReviewAccountDecisionFormComponent extends AbstractFormBase this.setInitialErrorMessages(); } + /** + * Navigates to the delete account confirmation page and records that the user + * came from the check account details screen, so the confirmation page can + * return them there when they cancel. + * + * @param event - The click event from the delete account link. + */ + public handleDeleteAccount(event: Event): void { + event.preventDefault(); + this.finesMacStore.setDeleteFromCheckAccount(true); + this.handleRoute(`${this.finesMacRoutes.children.deleteAccountConfirmation}/${this.accountId}`, { + event, + routeData: { + referrer: this.finesMacRoutes.children.reviewAccount, + }, + }); + } + /** * Angular lifecycle hook that is called after the component's data-bound properties have been initialized. * diff --git a/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account.component.spec.ts b/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account.component.spec.ts index 6e32bcf6f8..b63f74fab4 100644 --- a/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account.component.spec.ts +++ b/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account.component.spec.ts @@ -520,7 +520,12 @@ describe('FinesMacReviewAccountComponent', () => { component.handleDeleteAccount(mockEvent); - expect(routerSpy).toHaveBeenCalledWith([route], { relativeTo: component['activatedRoute'].parent }); + expect(routerSpy).toHaveBeenCalledWith([route], { + relativeTo: component['activatedRoute'].parent, + state: { + referrer: component['finesMacRoutes'].children.reviewAccount, + }, + }); expect(mockEvent.preventDefault).toHaveBeenCalled(); expect(component['finesMacStore'].setDeleteFromCheckAccount).toHaveBeenCalledTimes(0); }); @@ -539,7 +544,12 @@ describe('FinesMacReviewAccountComponent', () => { component.handleDeleteAccount(mockEvent); - expect(routerSpy).toHaveBeenCalledWith([route], { relativeTo: component['activatedRoute'].parent }); + expect(routerSpy).toHaveBeenCalledWith([route], { + relativeTo: component['activatedRoute'].parent, + state: { + referrer: component['finesMacRoutes'].children.reviewAccount, + }, + }); expect(mockEvent.preventDefault).toHaveBeenCalled(); expect(component['finesMacStore'].setDeleteFromCheckAccount).toHaveBeenCalledTimes(1); }); diff --git a/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account.component.ts b/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account.component.ts index ad55810ab7..f03f3bc828 100644 --- a/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account.component.ts +++ b/src/app/flows/fines/fines-mac/fines-mac-review-account/fines-mac-review-account.component.ts @@ -448,9 +448,14 @@ export class FinesMacReviewAccountComponent extends AbstractFormParentBaseCompon `${this.finesMacRoutes.children.deleteAccountConfirmation}/${this.accountId}`, nonRelative, event, + { + referrer: this.finesMacRoutes.children.reviewAccount, + }, ); } else { - this.routerNavigate(`${this.finesMacRoutes.children.deleteAccountConfirmation}`, nonRelative, event); + this.routerNavigate(`${this.finesMacRoutes.children.deleteAccountConfirmation}`, nonRelative, event, { + referrer: this.finesMacRoutes.children.reviewAccount, + }); } }