Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock route.snapshot.params?

In my Angular 4 component I have something like:

constructor(private route: ActivatedRoute) { }  ngOnInit() {   this.myId = this.route.snapshot.params['myId']; } 

And I'm trying to create a mock that suppose to look like follows:

class MockActivatedRoute extends ActivatedRoute {   public params = Observable.of({ myId: 123 }); }     

My test fails with:

TypeError: Cannot read property 'params' of undefined.

How do I suppose to mock it? Have I misunderstood the proper usage of ActivatedRoute and should better use router.subscribe in my component? I saw some complicated examples where people mocked snapshot itself, however for me it looks overcomplicated.


The test itself is quite simple:

describe('ngOnInit', () => { it('should set up initial state properly',   () => {   const component = TestBed.createComponent(MyComponent).componentInstance;     component.ngOnInit();     expect(component.myId).toEqual('123');   }); }); 

If I simply change a method under the test to something like follows - the test works:

ngOnInit() {     //this.myId = this.route.snapshot.params['myId'];     this.route.params.subscribe(params => {     this.myId = params['myId'];     }); } 

Obviously I need to mock Activated snapshot, but is there a better approach?

like image 324
and85 Avatar asked Oct 19 '17 13:10

and85


People also ask

How do you activate a mock route?

You can get the activated route mock/stub and add the the params object and replay subject to solve your problem (just follow the pattern for paramMap. Alternatively, download the finished one from here github.com/adrobson/activatedroutestub.

How do you test angular resolvers?

To test the resolver we need to render RouterOutlet . const fixture = MockRender(RouterOutlet); Additionally, we also need to properly customize mocked services if the resolver is using them to fetch data. const dataService = TestBed.

What is TestBed inject?

TestBed. configureTestingModule() helps you configure the providers. Configuring the providers means you are letting the Angular dependency injection system know about this dependency which later it can inject in to components when requested through a dependency injection token.

What is paramMap in angular?

ParamMaplinkA map that provides access to the required and optional parameters specific to a route. The map supports retrieving a single value with get() or multiple values with getAll() .


2 Answers

Ok, I've found how to mock ActivatedRoute snapshot in the simple way. Something like this works for me:

providers: [MyComponent, {   provide: ActivatedRoute,   useValue: {snapshot: {params: {'myId': '123'}}} } 

Thanks :)

like image 69
and85 Avatar answered Sep 19 '22 09:09

and85


If using route.snapshot.paramMap.get( 'uuid' ) instead:

import { ActivatedRoute, convertToParamMap } from '@angular/router';  {     provide: ActivatedRoute, useValue:         { snapshot: { paramMap: convertToParamMap( { 'uuid': '99-88-77' } ) } } } 
like image 40
Simon Avatar answered Sep 20 '22 09:09

Simon