In this episode, we dive into testing how user mentions are synced when comments are created and updated. We start by ensuring our testing environment is set up correctly with Pest as the testing framework and setup for an in-memory SQLite database.
We first fix some failing tests caused by recent changes to how usernames work in our project. This involves updating our user factories and some of the existing tests to use usernames instead of names. Once everything is green, we get started on our main goal: testing mention syncing.
We create a new feature test for comment mentions. The first test checks that when a comment is made mentioning two different users, two records are created for those mentions. We run into a little gotcha with the usernames having invalid characters (like dots), and fix this by manually setting the username values to ones we know will work.
From there, we assert not just the count, but that the actual users mentioned are correctly stored. Then we add another test to check that if a mention is removed (e.g., we edit the comment and remove one user from the body), the mentions table is properly updated—making sure it removes users who are unmentioned. After tweaking some assertions, all tests pass.
By the end, we've got solid tests to ensure mentions are synced correctly when comments are created or edited.