Create DeleteIdea tests0:00
Okay, let's go ahead and write some tests for our deleteIdea functionality. So I'm going to base it off our editIdea test, which sort of has the same functionality. So let's duplicate this file. Let's name it DeleteIdeaTest. And let's change this DeleteIdeaTest. And let's start with this test over here. Okay, so let's change this to showsDeleteIdeaLivewireComponentWhenUserHasAuthorization. So we're creating a User, we're creating an Idea with that User. We're logging in as that User, going to the show page. And we're asserting that we see the deleteIdea Livewire component.
We're logging in as that User going to the show page. And we're asserting that we see the deleteIdea Livewire component. So that should work. Okay. Next one is the opposite case. So let's change this to doesNotShowDeleteIdea. Okay. So we're creating a User, but the Idea is a different User. So that should not show the deleteIdea Livewire component, or shouldn't render it in this case.
Test deletion redirect1:07
So that should not show the DeleteIdea Livewire component, or shouldn't render it in this case. Okay. Next is we don't need this. This is the form validation from the editIdea test. Next is editing an Idea works. So let's change this to deleting an Idea works. Okay, so we don't need categories here. Let's change this to deleteIdea. And we are going to call the deleteIdea method.
Let's change this to delete Idea. And we are going to call the delete Idea method. If you remember, this is the component and we are just deleting it within here. And if you want, you can extract these two lines to the actual Idea model. But in this case, I'm only doing it in one place. So I'm okay with just keeping it here in the Livewire component. So we are calling that method. And let's assert that we are redirected. So assertRedirect to the index page. So it's a route idea.index.
So assert redirect to the index page. So it's a route idea.index. And I'm just going to assert that the count of ideas is zero. Since we started with one up here, and then we deleted it within here, and now it should be zero. So first, let's test this. I think I have to import deleteIdea if I didn't. Okay. Save this. I forgot a closing bracket.
Save this. I forgot a closing bracket. Save. Run this test. Okay. It does pass. And let's add one more thing here. Say assertEquals. Let's make sure that the idea count is zero. Because we deleted it.
Handle votes on delete2:56
Let's make sure that the idea count is zero. Because we deleted it. Okay. And this should pass. Okay. So let's also make sure that it works with the case when the idea has votes. So let's duplicate this. And let's rename it. Deleting an idea with votes. Okay.
Deleting an Idea with votes. Okay. And let's go ahead and add one vote here. So let's use the VoteFactory method. So let's say VoteFactory::create. And the userId is the $userId that we have here. So $userId. And then the ideaId is this one here. So we have one vote on this Idea. Okay.
So we have one Vote on this Idea. Okay. Let's make sure to import Vote. So this is the same. Let's also assert that the vote count is zero as well after we delete the Idea. So idea count and vote count. Okay. So let me save this. And if I were to remove this code that actually deleted the votes associated with the Idea, then this should result in an error.
And if I were to remove this code that actually deleted the votes associated with the Idea, then this should result in an error. And it does. We should get some sort of database error. And there it is there. Okay. So let's put that back. And it should pass now. Let's run this test again. And it does pass.
Admin can delete ideas4:29
Let's run this test again. And it does pass. And let's add one more test to check if the admin can delete an Idea that they did not create. So let's duplicate this or copy it at least. And let's put that one actually let's put that one first. So deleting an Idea works when user is admin. Okay. And I have this admin state here, which I can use to create an User with admin role. The Idea is going to belong to someone else.
And I have this admin state here, which I can use to create an admin User. The Idea is going to belong to someone else. So I'll remove this call and it should generate a new User. So we're going to log in as admin. We are going to delete the Idea and it should work. So run this test and it does pass. So just to make sure it works, let's remove the admin call here and it should fail because the User did not create that Idea. Okay, cool. So let's put that back.
Menu visibility tests5:25
Okay, cool. So let's put that back. Okay. What's next here? Okay. This we don't need. This one we don't need as well. And the other ones are just related to showing the delete Idea on the menu. So let's add those. So this should be deleting an Idea, pretty much the same thing as this.
So let's add those. So this should be deleting an Idea, pretty much the same thing as this. So this is the same assert, see, deleteIdea when we are authorized. And we are because we're creating an Idea with this User and then log in as a User. And this is the opposite case. Deleting an Idea, okay. And we assert that we don't see deleteIdea and this should work and it does. Let's run the entire file and let's make sure everything is still passing here. And it is cool. So let's go ahead and make a commit.
Commit the changes6:28
And it is cool. So let's go ahead and make a commit. This is episode 42, delete_idea_test. So git add, git commit, episode 42, delete_idea_test.
