Mocking router.events.subscribe() Angular2

前端 未结 6 1210
生来不讨喜
生来不讨喜 2020-12-30 00:03

In my app.component.ts I have the following ngOnInit function:

ngOnInit() {
    this.sub = this.router.events.subscribe(e => {
      if (e instanceof Navi         


        
6条回答
  •  心在旅途
    2020-12-30 00:34

    I have found the answer, if someone is looking for it:

    import { NavigationEnd } from '@angular/router';
    import { Observable } from 'rxjs/Observable';
    
    class MockRouter {
      public ne = new NavigationEnd(0, 'http://localhost:4200/login', 'http://localhost:4200/login');
      public events = new Observable(observer => {
        observer.next(this.ne);
        observer.complete();
      });
    }
    
    class MockRouterNoLogin {
      public ne = new NavigationEnd(0, 'http://localhost:4200/dashboard', 'http://localhost:4200/dashboard');
      public events = new Observable(observer => {
        observer.next(this.ne);
        observer.complete();
      });
    }
    

提交回复
热议问题