Jasmine test for Angular 2- Move to another page

I am writing a test case for angular 2 code using jasmine. I am trying to return to the login page after the user logs out. How to check page location?

+6
source share
2 answers

All you really need to do is verify that the router's navigation method is called with the correct argument (i.e., the path of the login page). Trying to test the actual navigation may cause more side effects than is required for the unit test.

To verify that the Router.navigate method is Router.navigate called, just use the stub and spy on it.

 @Component({}) class SomeComponent { constructor(private router: Router) {} logout() { this.router.navigate(['/login']) } } let routerStub; beforeEach(() => { routerStub = { navigate: jasmine.createSpy('navigate'); } TestBed.configureTestModule({ declaration: [ SomeComponent ], providers: [ { provide: Router, useValue: routerStub } ] }); }); it('it should navigate to login after user logs out', () => { let component = TestBed.createComponent(SomeComponent).componentInstance; component.logout(); expect(routerStub.navigate).toHaveBeenCalledWith(['/login']); }) 
+3
source

The Angular team has made every effort to incorporate testing into all native aspects of the Angular environment. If you use your own Angular router, then you have built-in testing for you. check out the BLOG . If you are not using an Angular router, then you are on your own, I think.

0
source

Source: https://habr.com/ru/post/1012776/


All Articles