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

Create Comments Test Files0:00

Okay, let's work on tests for our comments feature. So we worked on showing comments and adding comments. So let's add tests for those. So I believe I called my IdeaTest, showIdeasTest. So we'll do the same for comments. So php artisan make:test showCommentsTest. Actually, I want to put it in a comments folder. So we'll do this. And I'm not sure if that works, the folder. So showCommentsTest.

And I'm not sure if that works, the folder. So show comments test. Does that work? Yeah, it does work. Okay. And let's add one for addComment test. Okay. So let's start with showing comments and let's get rid of this. Let's use refreshDatabase. And let's write our first test.

Test Comment Components Render0:53

Let's use php artisan migrate:refresh. And let's write our first test. So as always, let's start with the existence test. So if you remember, we have two Livewire components, one for fetching all of the Comments and one for each individual Comment. So let's say IdeaComments, Livewire component renders. So let's go ahead and make an Idea here. So idea is IdeaFactory::create(). We don't care about any of the fields. Let's make a Comment here, say commentOne.

We don't care about any of the fields. Let's make a comment here, say comment one. Actually I don't even think we need the comment, but we'll add one anyways. Comment factory create. And let's just say idea is this one we have here. So idea id. Let's just put a body on it anyways. This is my first comment. Okay. And let's go ahead and go to that route.

Okay. And let's go ahead and go to that route. So this get, let's go to route('idea.show'), and we'll pass through the idea. And let's assert that we see this Livewire component or the IdeaComments Livewire component. Assert, see, Livewire. And we are checking against the IdeaComments component. So let's try this out. Let's import Comment and Idea here. This one, idea. Okay.

This one, Idea. Okay. So let's test that out. Okay. That's pass. Let's do the same thing, but for the other component. So same thing, but for the IdeaComment component. So we have to change this and change this. Okay. Okay.

Test No-Comments Message2:53

Okay. Okay. Okay. Let's add a test for this case when there's no comment. So I believe I have one here with no comment. Yeah, this one has no comments. So let's test this case. So let's duplicate this. Let's call it noCommentsShowsAppropriateMessage. And we don't even need a comment here.

Let's call it noComments shows appropriate message. And we don't even need a comment here. We're going to that page, and let's assert that we see that message. So assert, see, and what's the message? No comments yet. No comments yet. Let's try that out. Okay. Okay. Now let's add a case for actually showing comments.

Test Comments Display and Count3:36

Okay. Now let's add a case for actually showing Comments. So we'll grab this again, put it down here, and let's say list of Comments shows on Idea page. So let's add two Comments here. So let's duplicate this one, commentOne and commentTwo, and they both belong to the same Idea here. Okay. And the changes to second Comment. So let's assert that we see those Comments.

And the changes to second Comment. So let's assert that we see those Comments. So we can say assertSee, and I'm just going to hard code it, but you can use the $variable if you want. This is my first Comment. And it's the same for the other one, too, second. And let's also assert that we see the correct comments count. So that would be this. Let's go to one with actual Comments. So seven Comments in this case, but in our case, we only have two.

Let's go to one with actual comments. So seven comments in this case, but in our case, we only have two. So let's say, or duplicate this, and let's say two comments. Okay. So let's see if this passes. And it does. Cool. So let's go ahead and use the assertEquals method in order and assert that these two appear in order. So let's grab this, get rid of this, and make this an array.

appear in order. So let's grab this, get rid of this, and make this an array. So this one should appear first, and then this one should appear second. Close the array, and this should still work. Okay. Next let's add a case for the commentsCount on the index page. So that would be this right here. So almost the same as this one. So let's just duplicate this. Let's say commentsCount shows correctly on Idea page, or on index page.

So let's just duplicate this. Let's say commentsCount shows correctly on Idea page, or on index page. So we have two Comments. We are going to the index page. So Idea index, don't need this param. We can just assert, see, two Comments. So let's run that. It should pass. And it does. Just to make sure that it does pass, if I were to only have one Comment, this should

And it does. Just to make sure that it does pass, if I were to only have one Comment, this should fail. Cool. Okay. And if you want, you can separate out one for the show page and remove the assertion from here. But I'm okay with how I have it now. Okay. Now let's add a case for the OPBadge.

Test OP Badge Display6:16

Okay. Now let's add a case for the OP badge. So let's look for one of those. Let's go to my ideas here. And I'm not logged in actually. So let's go to my ideas. And if you remember, we have this OP badge. If this person is the person who created this idea. So let's write a case for that. Let's duplicate this one.

So let's write a case for that. Let's duplicate this one. Let's say OP badge shows if author of Idea comments on Idea. Okay. So we need a User for this. And that User has to be logged in. So let's use the factory here. User::factory()->create(). Make sure to import User. And for this Idea, we want to make sure that this User created the Idea.

Make sure to import User. And for this Idea, we want to make sure that this User created the Idea. So we can pass that in. UserID is UserID. Okay. And I'm going to leave commentOne. But commentTwo is going to be the person who created the Idea. So it's a UserID is UserID. Okay. And we're going to the show page.

Okay. And we're going to the show page. So let's make sure we are on that page. Idea. And I just want to make sure that that OP badge shows. So we can do OP. So let's try that out. It does pass. Let's make sure it's not a false positive. So let's remove this userId.

Let's make sure it's not a false positive. So let's remove this userId. And the OP badge should not show. So it should fail. And it does. Cool. So I'm going to use a response variable here like I am for my other tests. This is actually cleaner. But I'm just going to keep it consistent with my other tests. For example, I use a response variable.

But I'm just going to keep it consistent with my other tests. For example, I use a response variable. And then I use this variable to make assertions. So I'm just going to keep it this way. So let's just say response equals this. And then we can use that variable like this. And let's make sure to add a semicolon here. And it should still pass. So yeah, I'm just going to fix the tests above. Okay, so I changed all my tests so far.

Test Add Comment Form8:43

So yeah, I'm just going to fix the tests above. Okay, so I changed all my tests so far. Let's run the entire file. And everything is passing. So let's go to the other test. Add CommentTest. Let's get rid of this. And let's get started for this test. Use RefreshDatabase. And as always, let's start with the existence test.

Use php artisan migrate:refresh. And as always, let's start with the existence test. So let's say AddComment Livewire component renders. Okay, we need an Idea. IdeaFactory. create. And let's go ahead and go to the show page. So response is this get. We are going to the show page. Idea show.

We are going to the show page. Idea show. And pass through the idea. And it's a cert that we see that component. So response cert see Livewire. Add comment. Okay. Does this work? It does not. Did not import Idea.

It does not. Did not import Idea. Okay, try again. Okay. So let me just grab something from here. So we have a Comment already. So next one I'm going to test is if the form renders when we're logged in. So for example, I am logged in now. So it should show the form. But when I'm not logged in, it shows in.

So it should show the form. But when I'm not logged in, it shows in. It shows buttons here. So let's do that. Let's say addCommentForm does not render. When User is logged out. Okay. Actually, let's do the logged in case first. So let's duplicate this. And let's say.

So let's duplicate this. And let's say. Add Comment form renders. When the User is logged in. Okay. We actually don't even need the Comment here. So we just want to assert that we see. Any text here in the form. So let's say Share your thoughts is fine for me. Share your thoughts.

So let's say shareYourThoughts is fine for me. shareYourThoughts. Okay. Try that out. Passes or doesn't pass. Because we have to be logged in. So we have to use the actingAs method. actingAs User. And we need a User as well. So let's add that.

And we need a User as well. So let's add that. So let's duplicate this. Make a User. Using the UserFactory. Okay. Import User. And that should work. Okay. And let's do the other case.

Test Add Comment Validation12:01

Okay. Based on in. And this should pass. Okay. Next let's test the validation. So let's. Copy this or duplicate this. Let's say. Add Comment form. Validation works.

Add comment form. Validation works. So we need a User. So let me just grab this one. And here I'm going to make use of the actual Livewire testing. So instead of this. We'll do. Livewire. Acting as the User. Let's test.

Acting as the user. Let's test. The AddComment component. Okay. Let's import Livewire. And AddComment. Okay. And the AddComment component takes in an Idea. So let's pass that through. idea is.

So let's pass that through. Idea is. Idea. Okay. And let's set the comment. So let me just remind you how that looks. So we have a property for the comment, which is what the user types into the text area. So let's set comment to an empty string. And that should trigger.

So let's set comment to an empty string. And that should trigger a validation error message. Let's make sure to call the method methods called. Add comment right here. Call add method. Or sorry, add comment. And we can assert hasErrors. Sorry.

should be no errors. Okay. And if you look at the validation rules, you'll see that there are two rules here, one for required and one for at least four characters. And you can add separate cases for that, but I'm okay with just this one case. Or actually you can just add more cases here or just chain more on. So if you say. If we set it to something with less than four characters, we should still see errors here.

This is just a simple explanation with no code.

Test Comment Creation14:36

And it does go. And for the last case, let's just make sure that the form actually creates a new Comment. So addComment form works. We have a User. We have an Idea. We are logged in as the User. And we are going to set a comment to. This is my first comment. Call the addComment method.

This is my first Comment. Call the addComment method. Don't need this. Let's assert that. Again, if you remember, we have this event that we're emitting. So let's do that. assertEmitted that event. Okay.

Assert emitted that event. Okay. Comment was added. So this should pass. Okay. And now let's just make sure that it's in the database. So say this assertEquals one. And let's just count the comments so we can grab that from the Idea. Or we can just grab everything because there's no comments. So we can say commentCount.

And if you want, you can make sure that the comments associated with this ID or with this Idea actually have this. So let's do that. So again, assertEquals. So this is my first comment because that's the comment I added. And that should be ideaComments. Let's grab the first one. It's the only one and say body.

Test Comments Pagination16:26

Let's grab the first one. It's the only one and say body. And that should be the same. Okay. Actually, I think I forgot to add the pagination test. So let's do that. So I'm going to put it back in our showComments test and let's add it down here. So I already have one similar to this for the ideas pagination.

here. So I already have one similar to this for the Idea pagination. So let's grab that showIdeasTest. So this one right here, showPagination or ideasPagination works. So let's grab this. Let's paste it in here in our showCommentsTest. Let's say commentsPagination works. And we still need an Idea. And let's go ahead and make some comments here.

And we still need an Idea. And let's go ahead and make some Comments here. So let's say commentOne is Comment::create. And let's make sure to attach it to this Idea. Okay. And we're going to create 20 more using the getPerPage method. So similar to this, but instead of ideas, it's comments. So CommentFactory, let's just use commentOne here, because we have to use an instance to get to use the getPerPage method.

So CommentFactory, let's just use comment one here, because we have to use an instance to get to use the getPerPage method. And we want to make sure to associate it with this Idea. So again, ideaId is ideaId. Okay. And getPerPage is set to 20, I believe. Yep. So now we should have 21 comments for this specific Idea. So now we're going to the show page and we just use the route method here. So idea.show passing in the idea.

So now we're going to the show page and we just use the route method here. So idea.show passing in the idea. Okay. So this is the first page. So we should see the first Comment in the Idea's case. The first page showed the last Comment, but in this case, the first Comment should show here on the first page. So comment1 body. Okay. And it's assert that we don't see the last one on this page.

Okay. And it's assert that we don't see the last one on this page. So we can do Comment, find the last one. We can just do commentCount. That should be the last one. And we can just do body here. Okay. Let's get rid of this for now. And let's see if what I have worked so far. Okay.

And let's see if what I have worked so far. Okay. Looks good. And for page2 or for commentsPage2, let's do that. So we have to pass in variables here. So let's say the idea is idea, but we also want to pass in a page query string parameter, and that's page2. Okay. So we should be on page2 now.

Okay. So we should be on page two now. So this should be opposite. So let's just grab this and do the opposite. So assert that we don't see comment1 on page two. And assert that we do see the last comment on page two. Does this work? It does run the entire file. Actually, it's just run. Everything here should pass.

Actually, it's just run. Everything here should pass. And we are good. So as always, let's go ahead and commit episode 52. git add, git commit episode 52. Show and add comments. Test. Tests.

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