Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 : jasmin test case for activateRoute.snapshot.queryParams[""]

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)

like image 841
Morez Avatar asked Aug 14 '17 10:08

Morez


People also ask

How do you write test cases for ActivatedRoute?

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).

How to mock ActivatedRoute in Angular unit test?

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 // ...

What is ActivatedRoute snapshot in Angular?

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.

How do you test for ActivatedRoute in angular 6?

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.


1 Answers

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
        }
      }
   };
});
like image 177
Morez Avatar answered Sep 18 '22 19:09

Morez