In this episode, we're digging into testing the login functionality of our app before jumping into the more exciting features. We've already sorted out registration, so now it's time to make sure users can log in smoothly.
We start by writing a test for the login page. The first thing we check is if a logged-in user tries to access the login page—they should get redirected elsewhere. Then we focus on actually logging users in, ensuring the whole flow works as expected. Along the way, we run into a missing login page (cue a quick controller creation!), set up the necessary middleware to handle guest access, and troubleshoot a few little bugs that pop up.
We also write tests to make sure users get proper validation errors when required details aren't provided. Lastly, we cover the full login process, confirming that a user can log in with the correct credentials and gets redirected to the home page—and, of course, we check that they're truly authenticated after logging in.
We experiment with refactoring our tests to use higher order functions, talking through when that's worth it and when sticking to closures might just be simpler. And don't worry if you feel like there's a million more tests we could write! Here, we're keeping our emphasis on learning good testing practices (especially with Pest) so we don't get lost in the weeds.
Next up, we'll kick off some refactoring using custom functions and later, custom assertions, to help keep our tests neat and tidy.