diff --git a/frontend/src/app/features/training-and-qualifications/new-training-qualifications-record/delete-record/delete-record.component.spec.ts b/frontend/src/app/features/training-and-qualifications/new-training-qualifications-record/delete-record/delete-record.component.spec.ts index a826e94237..4a92bddc51 100644 --- a/frontend/src/app/features/training-and-qualifications/new-training-qualifications-record/delete-record/delete-record.component.spec.ts +++ b/frontend/src/app/features/training-and-qualifications/new-training-qualifications-record/delete-record/delete-record.component.spec.ts @@ -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({ @@ -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', }); }); }); }); }); }); - - diff --git a/frontend/src/app/features/training-and-qualifications/new-training-qualifications-record/delete-record/delete-record.component.ts b/frontend/src/app/features/training-and-qualifications/new-training-qualifications-record/delete-record/delete-record.component.ts index 1556f1773a..47c22b2ed9 100644 --- a/frontend/src/app/features/training-and-qualifications/new-training-qualifications-record/delete-record/delete-record.component.ts +++ b/frontend/src/app/features/training-and-qualifications/new-training-qualifications-record/delete-record/delete-record.component.ts @@ -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, @@ -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; @@ -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, + }); }); - }); }), ); }