TodayILearned

TIL… How to test a window-redirect using Mocks

I was testing an action, which upon completion needed to redirect the user to a new page. Normally, in javascript you can redirect using :

window.location = "newPath";

But that’s hard to test for. Thankfully there is a better way:

window.location.assign("newPath");

You can then mock the assign function and test for that call! My thanks to the creators of jest for that insight!

Advertisements
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s