Create EditIdea tests0:00
Okay, let's go ahead and write some tests for our Edit Idea feature. php, artisan, make:test, EditIdeaTest. Okay, let's go into that, EditIdeaTest. Let's use RefreshDatabase, and let's get rid of this. So our first test, as always, is to make sure the Livewire component renders correctly. So let's say, showsEditIdeaLivewireComponentWhenUserHasAuthorization. So let's make a user, User, factory, create. Let's make an Idea, Idea is Idea, factory, create. But let's make sure this Idea uses the User we just created. So user_id is the User we just created.
But let's make sure this Idea uses the User we just created. So $userId is the user we just created. So $userId. So that ensures that we have the correct authorization. And let's go ahead and actingAs $user. Let's go to the Show page. So getRoute('idea.show'), pass in the Idea. And let's assert that we see that Livewire component, assertSeeLivewire, the IdeaEdit component. Let's make sure to import these.
component. Let's make sure to import these. So User and Idea. Okay, let's go ahead and run this test. And it's failing. Sorry, that's the wrong name of the component. It's EditIdea. EditIdea. Try again. And it does pass.
Test authorization rendering1:59
Try again. And it does pass. So essentially, we're just checking if this Livewire component gets rendered correctly if we have the correct authorization. So let's test the opposite case. So when we don't have authorization, it should not render out that Livewire component. So let's say, doesNotShow, doesNotShowEditIdeaLivewireComponent when User does not have authorization, okay? So if we get rid of this, then a new User will get created for this Idea, which is not the same as the User logging in here. So that should work.
Validate edit form2:43
the same as the user logging in here. So that should work. So assertDontSeeLivewireEditIdea. Okay. Okay, next, let's test the validation. And we have something similar in our CreateIdea test. So let's just copy that. So we'll grab this. And we'll paste it in. Import Livewire.
And we'll paste it in. Import Livewire. Change the name. Say EditIdea. FormValidation works. EditIdea. FormValidation. Okay. And we need some setup here. It's basically the same as the first one.
And we need some setup here. It's basically the same as the first one. So this is when authorization is correct. So let's grab that. And I'm going to use the User we created here. So let's say user. We're testing the Idea or EditIdea component. Let's import that. And we are setting these to empty. We're calling the, what's the method called?
And we are setting these to empty. We're calling the, what's the method called? updateIdea method. updateIdea. And we're asserting that we do have errors. And we're asserting against one of those errors. So let's see if this works. Should work. And it does not. I think I have to pass in an Idea here.
And it does not. I think I have to pass in an Idea here. Okay, so let's do that. Idea is this idea. And this should work. And it does. So let's just verify that. If I actually type something here, then this should fail. And it does. Cool.
Test updating an idea4:21
And it does. Cool. Okay. Next, let's test if we can actually edit the Idea. So let's just duplicate this. Let's change the name here. Let's say EditingAnIdeaWorksWhenUserHasAuthorization. So we'll create a User. We'll create an Idea with that User. But let's also add some categories here because I want to change the category from one to
We'll create an Idea with that User. But let's also add some Categories here because I want to change the Category from one to another. So I'm just going to paste that in. So we have two Categories, Category1 and Category2. So let's just make sure to import Category, okay? And let's set the Category to Category1. So categoryId is Category1, okay? Now let's go ahead and try updating the idea. So we're actually going to set the title.
Now let's go ahead and try updating the Idea. So we're actually going to set the title. So let's say MyEditedIdea. We're going to set the category. So this will be Category2ID, okay? Description is ThisIsMyEditedIdea. And we're going to call the updateIdea method, which is equivalent to submitting the form. And let's assert that we see, or not see, assert that we emit that event. So if you remember after all of this here, let me just get rid of this comment. We don't need that.
So if you remember after all of this here, let me just get rid of this comment. We don't need that. Then we emit this event. So Idea was updated is a name. So let me just grab that, save this, let's go back here, assertEmitted, and Idea was updated, okay? So let's see if this works. And it does. Cool. And it's also assert that the database was updated.
Cool. And it's also assert that the database was updated. So we can do this, assertDatabaseHas, and we're checking the ideas table. And we can assert against that new data. So I'm just going to paste that in so you don't have to watch me do that. So it's title, the title we have here, the description we have here, and the new category. So let's try that. Not working. Should be ideas, sorry. Again, and it does pass.
Test edit menu visibility6:46
Should be ideas, sorry. Again, and it does pass. Next let's test if the actual text shows when we can edit the Idea. So for example, it's within the hour here. So 16 minutes ago. So we should see the edit Idea text. So let's add that. Let me just duplicate this, even though we don't need most of it. So let's say shows on menu. So editing an Idea shows on menu when User has authorization, okay?
So let's say shows on menu. So editing an Idea shows on menu when User has authorization, okay? So we don't need the categories, don't need a category. And that edit Idea menu lives in the IdeaShow component. So right here. So we have to test against that. So instead of edit Idea, it's IdeaShow. And we're passing in an Idea and also a Vote scant, which doesn't really matter. So you can just put any number in here, okay? And I just want to assert that we see that text.
So you can just put any number in here, okay? And I just want to assert that we see that text. So let's say, assert, see, Edit, Idea, okay? And we don't need this. Let's try that out. So let's run that test, should pass, does not. I have to import Idea show, okay? Let's run that again, and it does pass. So let's make sure it actually does work by changing the User who created the Idea. So if I change this, then it should fail because we should not see the Edit Idea menu item.
So let's make sure it actually does work by changing the User who created the Idea. So if I change this, then it should fail because we should not see the edit Idea menu item. And that is correct. Actually if you want, we can write a test case for that. So let me put this back and let me make another case here or another test, which is just the opposite of this. Let's say editing an Idea does not show on menu, does not show on menu when User does not have authorization. So it's basically what I just showed you. And then we are asserting that we don't see edit Idea, okay?
Test unauthorized edit cases9:03
So it's basically what I just showed you. And then we are asserting that we don't see editIdea, okay? So next, let's go back up here and work on the other cases in regards to editing the Idea. So this one here is the case where it does work and the User has authorization. So let's add a case for when it doesn't work because the User does not have authorization. And that in itself has two cases. So let's handle one for now. So let's duplicate this and say editing an Idea does not work when User does not have authorization.
So let's duplicate this and say editing an Idea does not work when User does not have authorization. And like I said, there are two cases for this. So let's say because User or because a different User created Idea. So let's say User A, User B. So this is all the same. But now since it's not passing authorization, it should abort here. So we can do, let's get rid of this assertEmitted, and we don't need this database stuff. We can assert, or sorry, not here, assertStatus. And we can say 403 or do response()->httpForbidden(), and it's import Response here, okay? And let's try that out.
And we can say 403 or do response()->httpForbidden(), and it's import response here, okay? And let's try that out. Sorry, that's failing because I forgot to change this. So I want to log in as the other User. So this User should not have access to this User because another User created that Idea. So it should pass now. It does. And now let's handle the other case when the Idea was created over an hour ago. So it's almost the same as this. So let's duplicate this.
So it's almost the same as this. So let's duplicate this. Let's rename it and say because Idea was created longer than an hour ago. So we only need one User here, okay? And this is fine. This is fine. User here. Actually here, we have to overwrite the created_at and make it over an hour ago. So let's say now()->subHours(2), okay?
So let's say now sub hours. So two hours ago, okay? And this should be the same. Okay, so it does work. So let's test the entire file. Everything is passing. Again, let's test everything here. Test php artisan test using parallel and everything is passing. So as always, let's make a commit here. This is episode 40, git add, git commit, episode 40, edit, Idea, test.
So as always, let's make a commit here. This is episode 40, git add, git commit, episode 40, edit, Idea, test.
