I am reading some values from url by using ActiveRoute.
http://localhost:4200/project-test/#/add?orderId=156&customerNumber=431
I have separate component to read the values in ngOnInit() method.
snippet :
...
constructor(private activatedRoute: ActivatedRoute,
public sanitizer: DomSanitizer,
private customerAPIService : CustomerAPIService) {}
ngOnInit() {
this.orderId = this.activatedRoute.snapshot.queryParams["orderId"];
...
}
Test case :
import { TestBed } from '@angular/core/testing';
import { CreateCustomer } from './create-customer.component';
import { ActivatedRoute } from '@angular/router';
import { CustomerAPIService } from "app/repackaging/service/customer.api.service";
describe('CreateCustomer', () => {
let component;
let mockActiveRoute;
let mockCustomerAPIService;
let mockQueryParamMap;
beforeEach(() => {
mockQueryParamMap = jasmine.createSpyObj('mockQueryParamMap', ['queryParams']);
mockActiveRoute = {queryParamMap: mockQueryParamMap};
TestBed.configureTestingModule({
providers : [
{
provide: ActivatedRoute,
useFactory: () => mockActiveRoute
},
{
provide: CustomerAPIService,
userFactory: () => mockCustomerAPIService
}
],
declarations :[
CreateCustomer
]
});
component = TestBed.createComponent(CreateCustomer).componentInstance;
});
it('should run ngOninit function', function () {
component.ngOnInit();
...
});
});
I am getting below error while writing a test
TypeError: undefined is not an object (evaluating 'this.activatedRoute.snapshot.queryParams') in http://localhost:9876/_karma_webpack_/main.bundle.js (line 350)
Test Cases for ActivatedRoute it('getDataByNameAndID test case ', () => {route.snapshot.params.id = '2';route.snapshot.params.name = 'testParamChanged';fixture = TestBed. createComponent(MyComponent);component = fixture. componentInstance;fixture. detectChanges();expect(component.name).
It is pretty straightforward to mock the ActivatedRoute in the User component test file. All we have to do is to edit the providers list as follows: beforeEach(async(() => { TestBed. configureTestingModule({ declarations: [ UserComponent ], imports: [ RouterTestingModule, HttpClientTestingModule // ...
ActivatedRouteSnapshotlinkContains the information about a route associated with a component loaded in an outlet at a particular moment in time. ActivatedRouteSnapshot can also be used to traverse the router state tree.
You can provide the ActivatedRoute in your test with a Subject and its Observable, so you can trigger the route change with source. next(). Testing code: let routeChangeSource: BehaviorSubject<Params>; // In TestBed.
extending @peeskillet answer.
There was silly mistake like I was mocking queryParamMap instead of snapshot.queryparams.
http://localhost:4200/project-test/#/add?order=156&customerNumber=431
Below is the solution to mock activeroutes.snapshot.queryparams["order"].
let orderId;
let customerNumber;
let mockActiveRoute;
...
beforeEach(() => {
...
mockActiveRoute = {
snapshot: {
queryParams: {
order: orderId,
customerNumber: customerNumber
}
}
};
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With