From c19a6e22029143888128361b2e3abec8936f3418 Mon Sep 17 00:00:00 2001 From: joannafawl Date: Tue, 21 Dec 2021 10:13:37 +0000 Subject: [PATCH 1/2] Use navigateByUrl instead of navigate to fix return url --- .../new-training-and-qualifications-record.component.ts | 1 + .../directives/add-edit-training/add-edit-training.directive.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/features/workers/new-training-qualifications-record/new-training-and-qualifications-record.component.ts b/src/app/features/workers/new-training-qualifications-record/new-training-and-qualifications-record.component.ts index 1b71b05fdf..f9ed4bbe97 100644 --- a/src/app/features/workers/new-training-qualifications-record/new-training-and-qualifications-record.component.ts +++ b/src/app/features/workers/new-training-qualifications-record/new-training-and-qualifications-record.component.ts @@ -77,6 +77,7 @@ export class NewTrainingAndQualificationsRecordComponent implements OnInit, OnDe this.filterTrainingByDefault = '0_showall'; this.filterTrainingByStatus = FilterTrainingAndQualsOptions; this.getFilterByStatus(this.filterTrainingByDefault); + this.setReturnRoute(); } public setTrainingAndQualifications(): void { diff --git a/src/app/shared/directives/add-edit-training/add-edit-training.directive.ts b/src/app/shared/directives/add-edit-training/add-edit-training.directive.ts index 69ae021746..e2fe0577ca 100644 --- a/src/app/shared/directives/add-edit-training/add-edit-training.directive.ts +++ b/src/app/shared/directives/add-edit-training/add-edit-training.directive.ts @@ -252,6 +252,6 @@ export class AddEditTrainingDirective implements OnInit, AfterViewInit { } public onCancel(): void { - this.router.navigate(this.previousUrl, { fragment: 'training-and-qualifications' }); + this.router.navigateByUrl(this.previousUrl[0]); } } From ae94596dee178dd35f6c47cf8f5f672fe879db13 Mon Sep 17 00:00:00 2001 From: joannafawl Date: Tue, 21 Dec 2021 10:31:32 +0000 Subject: [PATCH 2/2] Add test for cancel button --- .../add-edit-training.component.spec.ts | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/app/features/workers/add-edit-training/add-edit-training.component.spec.ts b/src/app/features/workers/add-edit-training/add-edit-training.component.spec.ts index 49518f0930..4cf7b0e51c 100644 --- a/src/app/features/workers/add-edit-training/add-edit-training.component.spec.ts +++ b/src/app/features/workers/add-edit-training/add-edit-training.component.spec.ts @@ -1,6 +1,7 @@ import { HttpClientTestingModule } from '@angular/common/http/testing'; +import { getTestBed } from '@angular/core/testing'; import { FormBuilder } from '@angular/forms'; -import { ActivatedRoute, RouterModule } from '@angular/router'; +import { ActivatedRoute, Router, RouterModule } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; import { ErrorSummaryService } from '@core/services/error-summary.service'; import { TrainingService } from '@core/services/training.service'; @@ -11,7 +12,7 @@ import { MockTrainingService } from '@core/test-utils/MockTrainingService'; import { MockWorkerServiceWithWorker } from '@core/test-utils/MockWorkerServiceWithWorker'; import { FeatureFlagsService } from '@shared/services/feature-flags.service'; import { SharedModule } from '@shared/shared.module'; -import { render } from '@testing-library/angular'; +import { fireEvent, render } from '@testing-library/angular'; import { AddEditTrainingComponent } from './add-edit-training.component'; @@ -45,10 +46,16 @@ describe('AddEditTrainingComponent', () => { ], }); + const injector = getTestBed(); + const router = injector.inject(Router) as Router; + const routerSpy = spyOn(router, 'navigateByUrl'); + routerSpy.and.returnValue(Promise.resolve(true)); + const component = fixture.componentInstance; return { component, fixture, + routerSpy, getByText, getByTestId, queryByText, @@ -127,4 +134,18 @@ describe('AddEditTrainingComponent', () => { expect(queryByText('Delete')).toBeFalsy(); }); }); + + describe('Cancel button', () => { + it('should call navigateByUrl when pressing cancel', async () => { + const { component, fixture, getByText, routerSpy } = await setup(); + + component.previousUrl = ['/dashboard?view=categories#training-and-qualifications']; + + const cancelButton = getByText('Cancel'); + fireEvent.click(cancelButton); + fixture.detectChanges(); + + expect(routerSpy).toHaveBeenCalledWith('/dashboard?view=categories#training-and-qualifications'); + }); + }); });