تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Faking and Asserting Notifications0:00

Okay, let's go ahead and write some tests for our notification feature. So if you remember, we added this line here to adding a comment, and this notifies the User who created the Idea anytime someone comments on their Idea. So I removed the mail driver here, but if it were in here, then you definitely want to add a fake, so mail isn't sent out when you run your tests. So let's go to the addCommentTest. And in here, we can do a Notification::fake(). So let's do this. And we can also assert that nothing was sent before we perform that action. So let's grab this.

And we can also assert that nothing was sent before we perform that action. So let's grab this. So before we do this, we can say Notification fake and assert that nothing was sent to startImportNotification. Okay. And after we perform that action, so after we add a Comment, we just want to make sure that this notification was triggered. So we can do it like this. Or you can also pass in a callback like this if you want to inspect more details about the notification.

Or you can also pass in a callback like this if you want to inspect more details about the notification. But I'm okay with just this way. Okay. So after it's done, then we can I'm sorry, I copied the wrong one. I want assertSent to space down in. So it should be sent to the person who created the Idea. So that would be idea->user. And we want to make sure that it's the CommentAdded notification. CommentAdded.

Creating Notifications Test Class1:36

And we want to make sure that it's the Comment added notification. Comment added. Okay. Let's import that. And let's see if this test still passes. Okay, cool. Now let's go ahead and make a class for the rest of our tests. So php artisan make:test, let's call it CommentNotificationsTest. Okay. Let's go to that CommentNotificationsTest.

Testing Component Rendering2:00

Okay. Let's go to that CommentNotificationsTest. Let's use php artisan migrate --refresh. And as always, let's start with the existence test. So we want to just make sure that this shows up when we're logged in and doesn't show up when we're not logged in. So let's do that. So let's just grab it from another test here. Grab it from AddCommentTest the first one. So let's grab this.

Grab it from addComment test the first one. So let's grab this. Let's say CommentNotifications Livewire component renders when User logged in. Okay. So we need a User here. So let's do UserFactory. Okay. Import User. And this appears on both pages, but I'll just make it work on the index page. Okay.

And this appears on both pages, but I'll just make it work on the index page. Okay. Actually, we need to log in as this user. So this actingAs user. Okay. And we can assert that we see that component. So comment notifications. Okay. This should pass and it does. And let's do the other case for when we're not logged in.

This should pass and it does. And let's do the other case for when we're not logged in. So CommentNotifications Livewire component does not render when User is not logged in. Okay. So same thing, but we don't need a User here and we're not logging in as that User. So let's remove this. Okay. And assertDontSee livewire. Try that.

Verifying Notification List Data3:52

And assert don't see Livewire. Try that. Okay. Okay. Next, let's say notifications show. So notifications show for logged in User. Okay. So we need some setup here and let me just paste that in to save some time. So we have a User and we have an Idea and this User created this Idea. So this is the User that would be receiving the notifications.

So we have a User and we have an Idea and this User created this Idea. So this is the User that would be receiving the notifications. And we have two other Users that are commenting on this Idea here. So we have a Comment factory, but we don't have a notification factory and we want to make sure that let me just show you here. This data in this notification table is correct. So all of this data here, I want to be accurate. So the test actually verifies the correct functionality. So you can write a notification factory, but I'm just going to do the equivalent of manually entering a Comment here and then this should trigger the notification and then this data

So you can write a notification factory, but I'm just going to do the equivalent of manually entering a Comment here and then this should trigger the notification and then this data should be correct. So let me show you what I mean here. So the equivalent of entering a Comment would be this. Actually, let's just grab it from the AddCommentTest. So down here, we're actually testing that it works. We can do something like this and this would be the equivalent of filling out the form here. Okay.

here. Okay. So let's go ahead and do that. Let's just put this on the same line. So it's a bit cleaner. Let's import Livewire. So let's set the comment to this is the first comment and we're calling addComment and we don't need this assertEmitted. And this would make sure that it actually adds a comment and actually populates this notification table correctly with all the right data.

And this would make sure that it actually adds a comment and actually populates this notification table correctly with all the right data. Okay. And it's going to be User a commenting that makes this comment. Let's add a second comment here. Make sure to add the semicolon. So User B would be the one creating this comment. It's a second comment. It doesn't really matter actually. Okay.

It doesn't really matter actually. Okay. So now we should have two Comment models in the database with the corresponding notifications in the notifications table. So now we can test the CommentNotifications Livewire component. So let's do that. Let's grab this and let's say actingAs $user. So remember the $user is the one receiving. So this $user is the one receiving notifications. Okay.

So this User is the one receiving notifications. Okay. We're testing the CommentNotifications class. Make sure to import that. Okay. And this doesn't have any params and let's go to that actually CommentNotifications. Remember we have to call this method here. getNotifications. So let's call that getNotifications.

So let's call that call. Get notifications. Okay. And now we can assert. See that those comments exist. So let's say this is the first comment because we have it up here and then we can do another one for a second. Okay. Let's try that. Hopefully I did everything correctly and it should be green.

So let me remove these. Okay. Because like I said, these two are created at the same time. So the easiest fix for this would be to add a sleep between them. So one second. And now it passes. But this slows our test down. So what we can do is just set the timestamp on this first one to be earlier. So let me just paste that in here. So I'm grabbing the notification, the first one, and I'm just updating the created that.

So let me just paste that in here. So I'm grabbing the notification, the first one, and I'm just updating the created_at field to one minute before. So hopefully this should pass as well. Okay. And it does. So we can get rid of this. And it's also add the notification count here. You can have a separate test for this, but I'm okay with just combining it with this test.

You can have a separate test for this, but I'm okay with just combining it with this test. So say notificationCount. And there should be two comments. Sorry. What am I doing here? Insert set. And we're asserting the notificationCount. And that should be two. So let's try this out.

And that should be two. So let's try this out. Sorry, I forgot the comma. Try again. Okay. And let's make sure it works. If I put something else in there, it should fail. Okay. Okay. And next let's add a test for the threshold.

Testing Threshold Display Logic9:49

Okay. And next let's add a test for the threshold. So if you remember, I added the threshold says 20 plus if there are more than 20 comments or more than 20 notifications. So let's start with this and just duplicate it. Let's name it notificationCountGreaterThanThresholdShowsForLoggedInUser. So we can keep this. We don't need a second comment here. Let's just stick with one. Actually not one.

Let's just stick with one. Actually not one. We're going to make 21. So it's over the threshold. We don't need this. Let's assert set that the notificationCount is 20 plus because we're going to have 21 comments in here or 21 notifications. And we can also assert C 20 plus the string since that should be unique enough. Okay. So obviously this should fail because there's only one notification so far.

Okay. So obviously this should fail because there's only one notification so far. Okay. So let's go ahead and make a loop here. Say for each say range, we'll make 21 say as item or whatever. And then we'll run this code here. So this should create 21 comments and notifications. So hopefully this passes. Okay. And it does.

Okay. And it does. We shouldn't hard code the 20 and 21 here. We should grab it from the threshold. Let's do that. We can grab it from comment notifications. It's called notificationThreshold. And in here we can use this threshold plus 1. Okay. And down here we can just say threshold and append plus that should turn it into a string.

Okay. And down here we can just say threshold and append + that should turn it into a string. And same with this threshold and append +. Okay. Spell this wrong. Run that. And it still passes. Okay. Next one. Let's grab the test above again.

Testing Read and Redirect Behavior12:20

Next one. Let's grab the test above again. And let's paste it underneath. But this one will be for markAllAsRead. Okay. So this is fine. This is fine. We have two comments and two notifications to remove this. And all we have to do here is call that method. So if you don't remember, it's called markAllAsRead right here.

And all we have to do here is call that method. So if you don't remember, it's called markAllAsRed right here. Okay. So let's call that markAllAsRed and we can get rid of these. And now we can assert that. So say this->assertEquals there should be zero for the user onRedNotifications. So we can do $user->onRedNotificationsCount. Let's try that out. Okay. Failed.

Okay. Failed. It still says there's two. So we might have to get a fresh User here. So let's try that out. Okay. And that works. Okay. Now let's test. We can mark individual notifications as read.

Now let's test. We can mark individual notifications as red. So we can grab this again, duplicate and say it can mark individual notification as red. Okay. So this is all the same. Instead of markAllAsRed, we can call the markAsRed method. And that takes in a notificationId. So let's just grab it directly from the model. So DatabaseNotification, let's say first()->id.

So let's just grab it directly from the model. So databaseNotification, let's say firstId. And if you remember, we are redirecting here to the show page. So let's do that. So let's say assertRedirect. So it's going to be route idea.show. And it's going to be idea. And we have the idea. Okay. So let's see if this works.

Okay. So let's see if this works. So we're marking this notification as red. Oh, yeah. And we want to change this to 1 because we had 2 initially, we marked 1 as red. And now there should be 1. So let's try this out. Okay. And pass says we need to be on page 1. So let's just hard code that in page 1.

And pass says we need to be on page one. So let's just hard code that in page one. Does that work? Okay. And it does. Okay. Two more tests here. So let's duplicate this again. So the first one is when the Idea is deleted. So notification idea deleted redirects to index page.

So the first one is when the Idea is deleted. So notification idea deleted redirects to index page. So this setup is the same. I only want one Comment here. So let's remove the second one. And instead of redirecting to this page, we're going to redirect to the index page when the Idea is deleted. So let's get rid of this, and we don't need this. Okay. So obviously, this is going to fail because I did not delete the Idea yet.

Okay. So obviously, this is going to fail because I did not delete the Idea yet. So let's do that. So we can delete the Idea in here. So let's just say idea delete. And I think this is not going to work because it has a Comment associated with it. So let's try that out. And let's see here. Yeah, we get this foreign key constraint. So we can delete the comments like this idea comments delete.

Yeah, we get this foreign key constraint. So we can delete the comments like this idea comments delete. Okay. And now hopefully this passes, and it does cool. So the better way would be to create a on delete cascade for the comments as well as the votes. So we don't have to delete the comments manually. And I'll probably add that eventually, but I'm not going to do it now. So the last test is the same thing, but for comments. So let's change this to notification comment deleted.

So the last test is the same thing, but for comments. So let's change this to notification comment deleted. Okay. We direct to the next page. So it's exactly the same thing, but we're just deleting this line here. So we're just deleting the comments for this Idea. Okay. And this should pass, and it does cool. Let's go ahead and run our entire test suite. Everything looks like it's passing.

Let's go ahead and run our entire test suite. Everything looks like it's passing. So let's go ahead and make a commit here. This is episode 64, episode 64 notifications testing or tests.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟