This episode is for members only

Sign up to access "Laravel Mentions" right now.

Get started
Already a member? Sign in to continue
Playing
09. Testing mention notifications

Transcript

00:00
Rather than spend too much time manually testing the notifications that we send and opening up our local mailbox Let's write out two tests very similar to the first two that we've already created
00:11
The first one we need to make sure that it does actually send a notification Which we already pretty much know is the case But the second test is really important and we want to make sure that we don't send a notification when we're modifying a comment So if we create a comment and we go ahead and mention someone in there and then we edit that comment to mention
00:31
Someone else we don't want to mention this original user Now we kind of know that's not going to happen because over in comment where we used our pivot attached event We know this is only going to be invoked when this pivot is attached Not when we update a comment, but it's worth having a test in there just so we can verify this just in case things change
00:54
So let's start with the most basic it sends a notification to mentioned Users, so the first thing we want to do is call notification fake In our test because we don't actually want to send the notification
01:11
We are going to need a user. So let's go ahead and print out a user in here Who's actually going to create the comment and actually once again We can pretty much use the majority of what we've got here the setups the first user and second user That we mentioned so we're going to pull them back in and of course we could create a helper for that as well
01:30
But I like to be pretty explicit in the test when we create them Then we can do exactly the same thing again So we can go ahead and create out a comment and again, we could extract all this functionality if we wanted to But we've got a comment now
01:44
Let's go ahead and run that test So let's run comment mention test and yeah, the last one is just risky because we don't have any assertions What do we want to check here? Well, we want to use the notification Facade which has an assert sent to method and we want to know that this
02:01
Sends a notification to the first user and the second user and what is the notification? Well, it's comment Mention, so that's basically it That's all we need to do if we mention these two users make sure it's sent to them two users. That's it Okay, let's go ahead and run our test here. And yeah, sure enough great. We get green now
02:21
The next one is that it doesn't send a notification when modifying a comment. This is probably the most important one so does not send a notification when modifying a
02:36
A Comment and what do we need to do? Well again, we are going to use notification fake because we don't actually want to send a notification This is actually a lot simpler. So we do need a user who is going to create the initial comment
02:52
But we really only need one test user In here, so we don't have to really bother too much with this so we could just say user Factory and create and because this is a lot simpler. We don't need a sequence so we can just set the username directly Inside of create here just so we don't get any trouble from our factory
03:13
Now we want to go ahead and create a comment. So like we did before so let's say comment and We'll say user comments Create and we just want to mention that one user now because we only really care about One thing and it being removed so mentioning
03:33
Let's say first user Username then we want to go ahead and update
03:42
That comment with a new body and that body is going to be saying mentioning In fact, we can just take this whole thing here. We want to mention them again with a modified Comment so basically this should send the notification
03:59
But this shouldn't so how do we test for this? Well, we can be specific here and we can put sort of assertions in between each of these But really all we need to do here is say notification assert sent times or assert count I think it is let's just have a look here. So it's sent. Oh, yes, it's certain times
04:18
So we're going to say comment mention and one we only want it to be sent once within this test So that will be here not here. Let's try it out So let's go ahead and run our tests and yeah, sure enough that passes So we're pretty confident now that this is only going to be sent once when we initially create the comment
04:37
Okay, so that's pretty much our tests done in terms of our mention functionality for each of our comments Mention functionality for each of our comments I think that pretty much covers everything in terms of what we would want to test here Just to make sure this works as expected
15 episodes1 hr 52 mins

Overview

Add mentionable functionality to your Laravel applications and mention users, projects, issues… literally anything.

We’ll start by setting up a simple comment system with Livewire, then detect, sync, notify and test mentions step-by-step. Not using Livewire? Don’t worry, the core functionality works with any stack.

If you are using Alpine/Livewire, we’ll add mention support to textareas to get a list of users we’re able to mention when we hit a trigger key.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!