How to mock router.navigateByUrl(...).then in Angular with Karma and Jasmine

user3659739

My problem is pretty simple, I use navigateByUrl() then then() in this way:

this.router
.navigateByUrl(PAGE1, {
  skipLocationChange: true
})
.then(() => {
  this.router.navigate([PAGE2]);
});

and I mock navigate, navigateByUrl and then this way:

const mockRouter = {
  navigate: () => {},
  navigateByUrl(url: string) { return url; },
  then: () => {}
};

It works well for navigate and navigateByUrl but it doesn't work for then and I don't know how to mock this function. How can I proceed?

user3659739

Thanks to jonrsharpe advise i found the solution. The solution was to mock navigateByUrl this way:

navigateByUrl: () => {
    return {
      then: () => {}
    }
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I inject a mock dependency into an angular directive with Jasmine on Karma

Karma-Jasmine How to test in angular a Router component

Jasmine mock chained methods with Karma and Angular

How to use router.navigateByUrl and router.navigate in Angular

Angular 9 - how to test a service that calls navigateByUrl by mocking the router

How to load mock data from JSON file in Angular 2 karma jasmine test?

Problems setting up mock services and calling it with jasmine/ karma and Angular

Unit Test error for Angular using Karma / Jasmine for Router

How to mock a window.location function in Karma/jasmine

Angular router: promise returned from navigatebyurl is ignored

Isolation with Angular on karma jasmine

Angular 5 (Karma / Jasmine Testing) - Using a mock backend to mock error responses for error messages

How to mock Angular $q service in Jasmine test?

How to mock Injector instance in Angular / Jasmine tests?

How to mock socket.io with Angular and Jasmine

How to mock an angular $resource object in Jasmine?

Angular 15 this.router.navigate is not a function, this.router.navigateByUrl is NOT a function

How to override Provider in Angular 13 for angular testing Jasmine Karma?

unable to mock HttpClient in Jasmine and Karma using HttpTestController

Karma + Jasmine (Angular Testing): Where should I define mock classes and test variables?

Angular.js+nw.js+webpack+karma+jasmine how to test

How to test 'private' functions in an angular service with Karma and Jasmine

How to create an ArrayBuffer Variable in Angular unit test, Jasmine/Karma

How to bootstrap angular at the start of each karma/jasmine test file?

How to test Location in angular 2 with karma+jasmine

How i can test local variable in Angular 2 with karma + jasmine?

How to unit test return value of function - Angular (Jasmine/Karma)

How to unit test a nested if condition in with Karma-Jasmine in Angular?

How to write unit test case using jasmine and karma in angular 7