Setting Up Edit Tests0:00
As always, let's go ahead and write some tests for editing and deleting Comments. So I'm going to base it off our editIdeaTest. So let's open this up. And let's duplicate this actually. And let's say, editCommentTest. Okay, and that's good. Actually I want it in the comments folder, so let's put it there. And let's change the name, editCommentTest, and let's change the namespace here, comments. Okay, so the first one is shows edit Comment Livewire component when User has auth. So the User and Idea, User doesn't have to create the same Idea.
Okay, so the first one is shows editComment Livewire component when User has auth. So the User and Idea, User doesn't have to create the same Idea. And we're just making sure that the Livewire component exists on the show page. So even without a Comment, it should still render, say editComment. So this should pass, okay. So the next one is, does not show editComment Livewire component when the User does not have auth. So pretty much the same thing, editComment, but we should not be logged in. So let's remove the acting as, and this should pass hopefully, and it does, cool. Next is the form validation, but before that, let's make a new test here.
Testing setEditComment1:30
So let's remove the acting as, and this should pass hopefully, and it does, cool. Next is the form validation, but before that, let's make a new test here. So if you remember, we have this new method called setEditComment, and we have one for delete as well, which sets the comment here based on whichever comment the User tries to edit here. So let's make sure this method works. So back to our test, let's say, or sorry, testEditComment is set correctly when User clicks it from menu. Well we're not actually clicking it from the menu, but we're just testing the method that gets called.
Well we're not actually clicking it from the menu, but we're just testing the method that gets called. So let's grab this. We need a User, we need an Idea, and now we actually need a Comment. So let's go ahead and make one, Comment::factory()->create(), okay. And we can do idea_id is this idea here. I believe we can just do the idea as well, but I'll just stick to the id. User id is the user_id, okay. And I don't think we need a body, but we'll just add one anyways. This is my first comment, okay.
And I don't think we need a body, but we'll just add one anyways. This is my first Comment, okay. Now let's use Livewire testing here. Say php artisan test, or acting as the User. We're testing the EditComment class. We're calling this method, setEditComment. So let's call setEditComment with the commentId as the param. And let's assert set. Remember when we do this, we're setting the body here, so let's assert that that's set correctly.
Remember when we do this, we're setting the body here, so let's assert that that's set correctly. Body should be whatever the comment body is. So I'll just use the variable here, or you can hard code it if you like. And we can assert that this event was emitted, okay. Assert emitted this event. Okay, so I think I have to import these. Okay, Livewire. And let's try this out. Comment not found.
And let's try this out. Comment not found. Import this one. Try again. And it does pass. Cool. Moving on. Let's work on this. Edit comment. Form validation works.
Edit Form Validation4:51
Edit Comment. Form validation works. So the Idea doesn't need a User, or it does need one, but it's auto-generated for us. Let's grab the comment from the last test. And Livewire acting as User. Let's just grab what we have above. For testing the component, let's grab this. Okay. And we're testing the form validation here. So we do have to be logged in.
And we're testing the form validation here. So we do have to be logged in. We are setting the comment to this one here. Let's set the body to an empty string. Let's call the updateComment method. So again, updateComment is what gets called after we submit a form for editing the comment. So updateComment. And we can do assertHasErrors. And it should have errors on the body. So we can say body here.
And it should have errors on the body. So we can say body here. And let's see if this passes. And it does. Cool. We can also add the case for when there's less than four characters. So we can do this again. And test the other case. So let's add two characters here. And again, if you don't remember, we have this rule here that says at least four characters.
So let's add two characters here. And again, if you don't remember, we have this rule here that says at least four characters. So this should fail as well. Or pass in this case. But it should have errors is what I meant. Cool. Okay. Next is make I'm just making sure that I changed the name here to Comment instead of Idea. Editing a Comment works when User has off. Okay.
Editing Comment Authorization6:46
Editing a Comment works when User has off. Okay. So we have a User. We need an Idea. Let's grab this one. And again, we need a Comment. So let's grab that as well. Don't need any of these. And it's going to be the same or almost the same as this one. So we're going to grab this.
And it's going to be the same or almost the same as this one. So we're going to grab this. So remove this. Okay. So we're testing this EditComment class again. We're setting the comment. Now we're setting the body to something that's valid. So say this is my updated comment. Okay. We're calling updateComment, and we can assert emitted the event that gets emitted.
Okay. We're calling updateComment, and we can assert emitted the event that gets emitted. So this event here Comment was updated. Okay. And we can let me just comment this out for now. This should pass. Hopefully it does. And we can use assertDatabaseHas like I have here, or I'm just going to do assertEquals instead, because there's only one Comment anyways. So this assertEquals.
equals instead, because there's only one Comment anyways. So this assertEquals. So there should be a Comment with this body. So this is my updated Comment. And there's only one at this point. So we can do comment first body. Okay. This should pass. It does. Cool.
It does. Cool. Next is editing a Comment changes does not work. When User does not have authorization. And there's only one case for this. That's when they're not logged in. Actually, there's two cases when they're not logged in. And when a different User who didn't create the Comment is trying to update the Comment. So let's grab everything here, actually, remove this. Okay.
So let's grab everything here, actually, remove this. Okay. And I'll just test the case when it's a different User here. So we can remove this, and it will generate another User. And now we can do assertStatus. And we can just do request or response, sorry, HTTP Forbidden, like we've been doing in our other tests. Make sure to import response. Make sure to spell that correctly. And let's give that a try.
Testing Edit Menu Display10:06
So editing a Comment shows on menu when User has off. So again, let's grab all of this for setup. Okay. And this time, the userId should be valid. So the person logging in is the one who created this Comment, userId, okay. This time, we're not testing the EditComment class, because this menu lives on the IdeaComment component. So this component here, okay. And we're testing if this shows. So essentially, we're just testing if this works right here.
And we're testing if this shows. So essentially, we're just testing if this works right here. Okay. So IdeaComment. And that takes in, what does it take in? IdeaComment takes in a comment and a ideaUserId. So say comment is the comment. And the ideaUserId is the ideaUserId. Okay. And we're just testing if the menu item appears.
Okay. And we're just testing if the menu item appears. So we can do assert, Comment::edit(comment). Okay. Hopefully this works. I think I have to import this, try that out. It does work. So the last one is pretty much this case, but does not show when the User does not have off. So let's get rid of this.
So again, a new User will be generated here, and it's not the same one as the person logging in here. So we can do assert, don't C, editComment, and this should pass, hopefully. And it does. Cool. So let's run the entire file and everything is working. So let's quickly do deleteComment as well. You will pretty much have the same cases as this, but for our deleteComment component. So I'm going to duplicate this, let's call it DeleteCommentTest. Okay.
Writing Delete Comment Tests12:52
So I'm going to duplicate this, let's call it deleteCommentTest. Okay. Let's change this to deleteCommentTest. And let's start with this, should delete or shows deleteComment should be the same. Okay, we're good. This one should be the same thing as well. deleteComment does not show delete, okay, should pass, does editComment. Same thing. Yep. deleteComment is set correctly.
I'm sorry, we don't need this. Okay, you don't need form validation. So we can get rid of this test. Next is deleting a Comment works. Okay. So we have a Comment here. We are using the deleteComment component. We're calling the setDeleteComment method. D L E T E. Okay.
D L E T E. Okay. So we don't need to set anything. We need to call the delete comment method. Comment was deleted. And we can assert that the count is zero. So we start with one here. And after we delete it, there should be no more comments. So we can do commentCount is zero, hopefully that should work. Let's give that a try.
So we can do commentCount is zero, hopefully that should work. Let's give that a try. Does work. Next one is pretty much the same thing. But making it work for our component here. DeleteComponent, delete, set, deleteComment. Don't need to set anything. deleteComment. And it should have this status as well. Okay.
And delete. Okay. Does that work? Okay. Okay. So let's run the entire file. Let's run the entire test suite. And everything is still green. So as always, let's make a commit here. This is episode 55.
