Skip to content

Commit

Permalink
Merge pull request #6408 from NMDSdevopsServiceAdm/fix/navigation-aft…
Browse files Browse the repository at this point in the history
…er-deletion-of-training-or-qual-record

Fix/navigation after deletion of training or qual record
  • Loading branch information
duncanc19 authored Nov 6, 2024
2 parents 157002a + 8f2ee0a commit da25d6f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,39 @@ describe('DeleteRecordComponent', () => {
);
});

it('should navigate to the view training page when pressing the delete button and set an alert message in thw history state', async () => {
it("should navigate to previousUrl and set training deleted alert after clicking 'Delete record' when previousUrl exists", async () => {
const { component, fixture, getByText, routerSpy, alertServiceSpy } = await setup();
component.previousUrl = ['/goToPreviousUrl'];
fixture.detectChanges();

const previousUrl = '/goToPreviousUrl';
spyOn(localStorage, 'getItem').and.returnValue(previousUrl);
component.ngOnInit();

const deleteButton = getByText('Delete record');
fireEvent.click(deleteButton);

expect(routerSpy).toHaveBeenCalledWith(['/goToPreviousUrl']);
expect(routerSpy).toHaveBeenCalledWith([previousUrl]);

fixture.whenStable().then(() => {
expect(alertServiceSpy).toHaveBeenCalledWith({
type: 'success',
message: 'Training record deleted',
});
});
});

it("should navigate to worker training and quals summary page and set training deleted alert after clicking 'Delete record' when previousUrl doesn't exist", async () => {
const { component, fixture, getByText, routerSpy, alertServiceSpy } = await setup();

spyOn(localStorage, 'getItem').and.returnValue(null);
component.ngOnInit();

const deleteButton = getByText('Delete record');
fireEvent.click(deleteButton);

expect(routerSpy).toHaveBeenCalledWith([
`workplace/${component.workplace.uid}/training-and-qualifications-record/${component.worker.uid}`,
'training',
]);

fixture.whenStable().then(() => {
expect(alertServiceSpy).toHaveBeenCalledWith({
Expand Down Expand Up @@ -239,25 +263,47 @@ describe('DeleteRecordComponent', () => {
);
});

it('should navigate to the qualification page when pressing the delete button and set a alert message in the history state', async () => {
it("should navigate to previousUrl and set qualification deleted alert after clicking 'Delete record' when previousUrl exists", async () => {
const { component, fixture, getByText, routerSpy, alertServiceSpy } = await setup(false);
component.previousUrl = ['/goToPreviousUrl'];
fixture.detectChanges();

const previousUrl = '/goToPreviousUrl';
spyOn(localStorage, 'getItem').and.returnValue(previousUrl);
component.ngOnInit();

const deleteButton = getByText('Delete record');
fireEvent.click(deleteButton);

expect(routerSpy).toHaveBeenCalledWith(['/goToPreviousUrl']);
expect(routerSpy).toHaveBeenCalledWith([previousUrl]);

fixture.whenStable().then(() => {
expect(alertServiceSpy).toHaveBeenCalledWith({
type: 'success',
message: 'Qualification record deleted',
message: 'Qualification record deleted',
});
});
});

it("should navigate to worker training and quals summary page and set qualification deleted alert after clicking 'Delete record' when previousUrl doesn't exist", async () => {
const { component, fixture, getByText, routerSpy, alertServiceSpy } = await setup(false);

spyOn(localStorage, 'getItem').and.returnValue(null);
component.ngOnInit();

const deleteButton = getByText('Delete record');
fireEvent.click(deleteButton);

expect(routerSpy).toHaveBeenCalledWith([
`workplace/${component.workplace.uid}/training-and-qualifications-record/${component.worker.uid}`,
'training',
]);

fixture.whenStable().then(() => {
expect(alertServiceSpy).toHaveBeenCalledWith({
type: 'success',
message: 'Qualification record deleted',
});
});
});
});
});
});


Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export class DeleteRecordComponent implements OnInit, OnDestroy {
public qualificationRecord: QualificationResponse;
public trainingOrQualification: string;
public trainingView: boolean;
private trainingPageUrl: string;
private workerTrainingAndQualsSummaryUrl: string;
public previousUrl: string;
private recordUid: string;
private subscriptions: Subscription = new Subscription();
public previousUrl: string[];

constructor(
private route: ActivatedRoute,
Expand All @@ -36,15 +36,16 @@ export class DeleteRecordComponent implements OnInit, OnDestroy {
ngOnInit(): void {
this.setTrainingView();
this.setVariables();
this.previousUrl = [localStorage.getItem('previousUrl')];

this.trainingPageUrl = `workplace/${this.workplace.uid}/training-and-qualifications-record/${this.worker.uid}`;
this.workerTrainingAndQualsSummaryUrl = `workplace/${this.workplace.uid}/training-and-qualifications-record/${this.worker.uid}`;
this.previousUrl = localStorage.getItem('previousUrl');
this.setBackLink();
}

private setTrainingView(): void {
this.trainingView = this.route.snapshot.data.trainingRecord ? true : false;
this.trainingOrQualification = this.trainingView ? 'training' : 'qualification';

if (this.trainingView) {
this.trainingRecord = this.route.snapshot.data.trainingRecord;
this.recordUid = this.trainingRecord.uid;
Expand All @@ -65,19 +66,21 @@ export class DeleteRecordComponent implements OnInit, OnDestroy {

public returnToEditPage(event: Event): void {
event.preventDefault();
this.router.navigate([this.trainingPageUrl, this.trainingOrQualification, this.recordUid]);
this.router.navigate([this.workerTrainingAndQualsSummaryUrl, this.trainingOrQualification, this.recordUid]);
}

public deleteRecord(): void {
const message = `${this.capitalizeFirstLetter(this.trainingOrQualification)} record deleted`;
this.subscriptions.add(
this.deleteTrainingOrQualificationRecord().subscribe(() => {
this.router.navigate(this.previousUrl).then(()=>{
this.alertService.addAlert({
type: 'success',
message: message,
this.router
.navigate(this.previousUrl ? [this.previousUrl] : [this.workerTrainingAndQualsSummaryUrl, 'training'])
.then(() => {
this.alertService.addAlert({
type: 'success',
message: message,
});
});
});
}),
);
}
Expand Down

0 comments on commit da25d6f

Please sign in to comment.