0

I have a child component whose template consists of a button element with 'routerlink' on it which helps in navigating when that button is clicked. The button doesn't have '(click)' handler. Just the routerlink.

Now I'm writing unit test in Jasmine for this component where I want to check if the correct page is loaded or the component calls the router with proper URL(of the next page) when that button is clicked. Essentially the button looks something like this:

<button routerLink="/a/b/c" id="JustAButton">

How do I text these events in order:

  • Click on the button (note that the button doesn't have a '(click)' associated with it).
  • Check if url has changed to '/a/b/c' or something equivalent which makes sure router is called with that url.

I have followed all the different cases mentioned here and here. None of them help. Mainly because I do not have 'router-outlet' in neither the component I am testing nor the component I am loading on that button click. (I tried using dummy component for the second part too)

Any help would be appreciated!

Thanks!

alphapilgrim
  • 3,761
  • 8
  • 29
  • 58

1 Answers1

0

Its hard to know exactly what you are testing without a code example but... If you are indeed only worried about testing whether a route was called and the unit you are testing is the component then you should be using a mock router like so:

beforeEach(() => {
    const mockRouter = jasmine.createSpyObj('Router', ['navigate']);
    TestBed.configureTestingModule({
        imports: [HttpModule, RouterTestingModule],
        providers: [
            RouterTestingModule,
            {provide: XHRBackend, useClass: MockBackend},
            {provide: Router, useValue: mockRouter}
        ]
    });
});

Then you can use jasmine spies (on the mockRouter) to see if the route your unit is calling as been called with arguments: https://jasmine.github.io/2.0/introduction.html#section-Spies

krosullivan
  • 172
  • 2
  • 12