My goal is to execute a test to check for the presence of a specific string in URL parameters. Inside my TypeScript file, I have defined the following method:
checkURLParams() {
if (this.route.parent) {
this.route.parent.params.subscribe((params) => {
if (params['view'] === 'view') {
this.form.disable();
}
});
}
}
The primary objective of this test is to verify that when the URL parameters include the string 'view', the form should be disabled.
In my spec file, I currently have this setup:
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [MyComponent],
imports: [
RouterTestingModule,
ReactiveFormsModule
],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
component.ngOnInit();
fixture.detectChanges();
});
fit('test', fakeAsync(() => {
// Arrange
// Act
component.checkURLParams();
// Assert
expect(component.form.disabled).toBeTruthy();
}));
I have come across various solutions for testing URL parameters differently, but I am struggling to find a way to mock the URL parameters within the test so that they contain 'view' and trigger the form disablement. What would be the most effective approach to tackle this challenge?