Skip to content

Commit

Permalink
Merge pull request #4422 from NMDSdevopsServiceAdm/bug/cancelButtonTr…
Browse files Browse the repository at this point in the history
…aining

Bug: Cancel button - training
  • Loading branch information
joannafawl authored Dec 21, 2021
2 parents 8051f16 + ae94596 commit 22e2528
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

0 comments on commit 22e2528

Please sign in to comment.