Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('FinesMacDeleteAccountConfirmationComponent', () => {
};
mockRouter = {
navigate: vi.fn().mockName('Router.navigate'),
currentNavigation: vi.fn().mockName('Router.currentNavigation').mockReturnValue(undefined),
};

await TestBed.configureTestingModule({
Expand Down Expand Up @@ -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']);
});
});
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<void>();
private readonly route = inject(ActivatedRoute);
private readonly utilsService = inject(UtilsService);
Expand All @@ -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.
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<a
class="govuk-link govuk-link--no-visited-state govuk-error-colour"
href=""
(click)="handleRoute(`${finesMacRoutes.children.deleteAccountConfirmation}/${accountId}`, { event: $event })"
(click)="handleDeleteAccount($event)"
>Delete account</a
>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ 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', () => {
let component: FinesMacReviewAccountDecisionFormComponent;
let fixture: ComponentFixture<FinesMacReviewAccountDecisionFormComponent>;
let formSubmit: IFinesMacReviewAccountDecisionForm;
let originalInitOuterRadios: () => void;
let finesMacStore: FinesMacStoreType;

beforeAll(() => {
originalInitOuterRadios = GovukRadioComponent.prototype['initOuterRadios'];
Expand Down Expand Up @@ -42,6 +45,7 @@ describe('FinesMacReviewAccountDecisionFormComponent', () => {

fixture = TestBed.createComponent(FinesMacReviewAccountDecisionFormComponent);
component = fixture.componentInstance;
finesMacStore = TestBed.inject(FinesMacStore);
fixture.detectChanges();
});

Expand All @@ -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<any, any>(finesMacStore, 'setDeleteFromCheckAccount');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleRouteSpy = vi.spyOn<any, any>(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', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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<IFinesMacReviewAccountDecisionForm>();

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;
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}

Expand Down
Loading