Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 unit testing error: Can't bind to 'routerLink' since it isn't a known property of 'a'

Tags:

angular

I'm trying to manage with unit testing (karma, jasmine) and I'm getting an error:

Can't bind to 'routerLink' since it isn't a known property of 'a'

I have not even changed *.spec.ts file that was created by angular-cli...

Here are my files:

user-list.component.html

<ul>
  <li *ngFor="let user of users">
    <a [routerLink]="user._id" (click)="userClicked(user)">{{user.fullName}}</a>
  </li>
</ul>

<router-outlet></router-outlet>

user-list.component.ts

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import { IUser, User } from '../../../interfaces/user';
import { UsersService } from '../../../services/users.service';

@Component({
  selector: 'app-user-list',
  templateUrl: './user-list.component.html',
  styleUrls: ['./user-list.component.css']
})
export class UsersListComponent implements OnInit {

  users: IUser[] = [];

  constructor(private usersService: UsersService, private router: Router) { }

  ngOnInit() {
    this.usersService.getAll()
      .subscribe(users => {
        this.users = users.sort(...).map(...);
      });

  }

  userClicked(user) {
    this.usersService.setCurrentUser(user);
  }

  clickXBtn(user) {
    this.usersService
        .deleteUser(user)
        .subscribe(...);
  }

}

user-list.component.spec.ts

/* tslint:disable:no-unused-variable */
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';

import { UsersListComponent } from './user-list.component';

describe('UsersListComponent', () => {
  let component: UsersListComponent;
  let fixture: ComponentFixture<UsersListComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ UsersListComponent ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(UsersListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

What should I change? How to make it work?

like image 201
Zurab-D Avatar asked Feb 15 '17 09:02

Zurab-D


People also ask

Can't bind to routerLink since it isn't a known property of input?

You need to add RouterModule to imports of every @NgModule() where components use any component or directive from (in this case routerLink and <router-outlet> . declarations: [] is to make components, directives, pipes, known inside the current module.

Can't bind to routerLink since it isn't a known property of DIV unit test?

the reason is that you probably did not include RouterModule to your @NgModule in imports section. Important thing is this needs to be included in every module where you are using Router link or any other router feature.

What is router module in angular?

Adds directives and providers for in-app navigation among views defined in an application. Use the Angular Router service to declaratively specify application states and manage state transitions.


2 Answers

Possible duplicate of Angular 2 Jasmine Can't bind to 'routerLink' since it isn't a known property of 'a'

To resolve your problem, try to stub the RouterLink https://angular.io/guide/testing-components-scenarios#components-with-routerlink

@Directive({
  selector: '[routerLink]',
  host: {
    '(click)': 'onClick()'
  }
})
export class RouterLinkStubDirective {
  @Input('routerLink') linkParams: any;
  navigatedTo: any = null;

  onClick() {
    this.navigatedTo = this.linkParams;
  }
}

And use the stubbed RouterLink in your test

like image 55
mickdev Avatar answered Oct 30 '22 18:10

mickdev


An updated version of the RouterLink stub would be

import { Directive, HostListener, Input } from '@angular/core';

@Directive({
  selector: '[routerLink]',
})
export class RouterLinkStubDirective {
  @Input('routerLink') linkParams: any;
  navigatedTo: any;

  @HostListener('click') onClick(): void {
    this.navigatedTo = this.linkParams;
  }
}

However, the stub code is not necessary. All you need to do is add this statement to your .spec.ts file

import { RouterTestingModule } from '@angular/router/testing';
like image 28
C D Avatar answered Oct 30 '22 20:10

C D