What matters to me is that everything works as it should. So that's how you do that. Now you can also, basically, go to a route, for example, on this website, I don't want to hit the IMDB website every time that I want to test the site, but I want to make sure that the link is clickable. So you can see here, every movie clicks to the website, it clicks to the IMD website, and I'm basically just saying, when you want to go to that website, just go here instead. So mocking, right?
I'm mocking that, so that it doesn't hit that website, but I can still test that that button is clickable and it's going to go to where it should go. Let's take a quick look at how you would do that. So in the same test, I actually have this in the same test, look how I'm using test.stats, you see? My beautiful tests, and amazing. So here, right? I've got my page, wait, page.context. Look at that line 77, and I'm saying route. So the Twister's movie website. So when you go to the Twister movie's website, I'm just going to create an HTML page and mock page, right? To just to prove that I'm going to that page. And then I basically just, and the same here, right? For the IMDb website, every time you see that route, intercept it, and fulfill it with this body instead, this mock thing that I'm creating. And then I basically get by the little button, click it, and then I wait for the page one to have the URL, IMDb. So that's kind of a way of mocking external links that you want to go to, but you want to make sure that they are clickable and that somebody can do that. So that's really cool, and I can just play that again, and you can see how it basically just goes and finds the Twister's movie website instead of actually going to the actual website. You can see www.twistersmovie.com, right? So it's really, really cool. So yeah, that is API mocking.
So let's take a look at testing date and time. How would you do that? So you might want to test something like this. We've got a clock, right? And we've got 16, 36, 52, and it's kind of counting upwards, and it's using date.now. So basically, what I've got to do, I've got a function called startClock. I've got the const today, my date, etc. And I've got a window addEventListener, load, start the clock, right? So that's basically my clock, HTML. And then my test, what it's going to do is I'm basically using the page.clock, and I'm setting a fixed time. So my fixed time is a new date at 10 o'clock in the morning on this specific date. And then I go to my clock website. So fixed, setFixedTime returns a fixed fake time at all times, okay? And then I basically run this test, and I'm just making sure that the time is 10 o'clock. I've got that passes, and you can see in my trace viewer here, I'm running through the VS Code extension, and I get the trace viewer because I have that set. And it just shows, yep, it's going to 10 o'clock. So perfect.
Comments