Stack Exchange
Angular4 routing tests
I works on an Angular project (v4), and I want test a ngOnInit of component. But, I have no idea to test a component with routing. In fact, I want found a label contains into 2 array of objects, due to the URL params. Thank you for your answers.
import { Component, OnInit } from '@angular/core';
import { MenuNavComponent } from '../../shared/folder/cpt.component';
import { Router } from '@angular/router';

@Component({
  selector: 'cpt-small',
  templateUrl: './cpt-small.component.html',
  styleUrls: [
    './cpt.scss'
  ]
})
export class Cpt extends ParentCpt implements OnInit {

  constructor( private router: Router ) {
    super();
  }

  ngOnInit() {
    this.router.events.subscribe(() => {
      if (!this.router.url.startsWith('/home')) {
        let routeURL = this.router.url.split('/')[1];
        routeURL = `/${routeURL}`;
        this.menu.forEach((menuCategory, index) => {
          menuCategory.items.forEach((item) => {
            if (routeURL === item.link) {
              this.onDropDownClicked(index);
            }
          });
        });
      }
    });
  }

  onDropDownClicked(index) {
    this.menu.forEach((dropDown, i) => {
      if (i !== index) {
        dropDown['open'] = false;
      } else {
        dropDown['open'] = true;
      }
    });
  }
}

