mercredi 25 mai 2016

Unit testing component which uses different route components

I'm trying to test the following component.

MyComponent -

export class MyComponent {
  radioItems: any;
  selectedItem: number = 0;
  name: string;

  constructor(private _reset: ForgotPwdService, private _router: Router) {
    if (!_reset.storedMails || !_reset.fullName) {
      _router.root.navigate(['ParentRoutePath']); // Using parent route, _router.parent.navigate() doesn't work for test either
      return;
    }
    this.name = _reset.fullName;
    this.radioItems = _reset.storedMails;
  }

  getValue(event) {
    this.selectedItem = event;
  }

  onClick() {
    this._reset.mail = this.radioItems[this.selectedItem].title;
    this._router.navigate(['RoutePath']); //Using route
  }
}

In the test, in beforeEach() method, I'm providing the following route component -provide(ROUTER_PRIMARY_COMPONENT, {useValue: ComponentWithChildRoute}). But when I'm trying to run tests I'm getting the following error in the test which tries to verify the code in the constructor - Component "ComponentWithChildRoute" has no route named "ParentRoutePath". If I try to provide in for provide(ROUTER_PRIMARY_COMPONENT, {useValue: RootRouteComponent}), I'm getting Component "RootRouteComponent" has no route named "RoutePath".. Is there a way to resolve it? Maybe I can provide one of the route components in the needed tests itself, not in beforeEach()?

Aucun commentaire:

Enregistrer un commentaire