In this episode, we look at how to set default notification preferences when a new user signs up for your application. We start by writing a test that ensures a user's notification preferences are set up correctly upon creation. For this, we use an observer, specifically a User Observer, and set up our test in the appropriate directory.
From there, we build up our expectations in the test: once a user is created, they should have at least one notification preference, and by default, it's set to email. We explain how these defaults could be tweaked according to your app's needs.
Since nothing is present to make the test pass, we create a User Observer and connect it to our User model. The observer is responsible for syncing notification preferences right when the user is created, setting all available notifications to use the mail channel by default.
We loop back and rerun our tests, catching and fixing any small issues along the way (like making sure we're comparing arrays correctly). Once the tests pass, we also try this out manually through the app: deleting the user, registering a new one, and seeing all the email notification preferences set up just as expected.
Overall, this episode is about making sure your users have sensible defaults for notifications when they first join, and setting it up in a way that's easy to expand or change later.