DZN
DZN

Reputation: 1564

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

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?

Upvotes: 6

Views: 5277

Answers (2)

mickdev
mickdev

Reputation: 2885

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

Upvotes: 6

C D
C D

Reputation: 56

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';

Upvotes: 2

Related Questions