Transcript
00:00 The first thing I will do is remove this HTTP.ALL handler. Once it's gone, I need to create a more targeted handler for the request that our tested code makes. So let's take a look at that request. It's a POST request to this particular URL. I will copy this URL and go back to the handlers.
00:17 So I will create an HTTP.POST call which targets all POST requests, and I will provide this URL as the path argument. Then I will create a resolver function, and here I can handle this intercepted request in many different ways. But right now I want to respond to it with a mock response.
00:36 So let's first construct that response by calling response.json. This is a static method on the global response class, and this creates a JSON response, and it expects buddy here. So in our getAuthToken function, the successful scenario expects the buddy to have a token property. So I will provide an object
00:55 here with token that equals abc-123. And now that I have this response created, all I have to do is return it. And this will let MSW know that when it intercepts this matching request, it should use this response to respond to it. Now with this in place, I will go to my test file
01:15 and finally create an assertion for this getAuthToken function. So I will expect that calling getAuthToken with these credentials will return the token object where the information will be as I defined in my mock. Not that I'm using this await, expect, and dot
01:32 resolves syntax. I do this because if this promise rejects, Vetus will print a more nicer error message when that happens. So I'll be able to figure out why this promise rejects in a faster way. So here I will run the test with npm
01:49 test to verify that they are passing. They are passing. What happens here is that when our getAuthToken function performs this post request right here, MSW intercepts it, finds this matching handler matching by method and also the request URL.
02:05 And since we're returning a particular response instance, the mock response instance, it will use this JSON response as the response that we receive here. And then of course because it's a successful response, it will read its body and return this
02:20 particular token that we're able to assert on in our automated tests.