Fix Failing Pagination Test0:00
Okay, let's add some tests for the create Idea functionality that we did in the last video. So let's do php artisan make:test, let's call it CreateIdeaTest. Before we work on this, let's fix a failing test because of a change we made in the last video. So let's run test first. And let's find that failing test. Okay, so it's in ShowIdeasTest, and it has to do with the pagination because I changed the order of the ideas when they show up. So let's go to ShowIdeasTest.
the order of the ideas when they show up. So let's go to showIdeasTest. And it's this test right here. So if you remember, I changed the order, and now the latest Post is showing first. So we have to fix our test here. So we have idea1 and idea11. So now idea11 should show on the first page. So we just have to swap this. So let's say assertSee(idea11); assertDontSee(idea1); go to page two, assertSee(idea1);
So let's say assertSee idea 11. assertDontSee idea 1, go to page 2, assertSee idea 1. And then assertDontSee idea 11. And hopefully that fixes it. So let's run this test. And it's passing now. So now we can work on our createIdea test. So let's go to that. And let's use the database. Let's get rid of this.
Test Form Visibility1:30
And let's use the database. Let's get rid of this. Use php artisan migrate:refresh. Okay. And the first test I want to write is the functionality that hides the form when we're not logged in. So let's do that one. So CreateIdeaForm does not show when logged out. Okay. So we can do response this get say route(idea.index).
Okay. So we can do response this get route idea.index. And say it's successful. assertSuccessful. And then we should assert that we see what did I add there. So let's check. So the text that I added was, please log in to create an Idea. So we can assert that we see that. And if you want, you can assert that you don't see the other text. So this text.
And if you want, you can assert that you don't see the other text. So this text. So let's add that. And let's escape the apostrophe here. Okay. And let's run this test. Okay. And we can do the same for the opposite case. So let's just duplicate this. And say createIdeaForm does show when logged in.
So let's just duplicate this. And say createIdeaForm does show when logged in. Okay. So it's basically the same thing. Just swap this around. Don't see and see. Let's run this. Okay. And it fails because we have to log in as a User. So we can use actingAs.
And it fails because we have to log in as a User. So we can use actingAs. And we can just create a User here. Let's say User::factory()->create(). And we can get. And let's try that again to import User. Okay. And let's run that test again. Still failing. Sorry about that.
Still failing. Sorry about that. I changed the wrong one. It should be the other one that I changed. So let's grab this. Let's put it back. And we can replace this. Okay. And this one should pass now. And I forgot to import User again.
And this one should pass now. And I forgot to import User again. Run the test. And it's failing because it's escaping the apostrophe. So let's say false here. Okay. One more time. Okay. Next, since we're working with Livewire here, we want to make sure that the Livewire component renders on a specific page.
Assert Livewire Renders4:26
Next, since we're working with Livewire here, we want to make sure that the Livewire component renders on a specific page. So let's say main page contains createIdea Livewire component. Okay. And we can say this acting, let me just grab this. Okay. And paste that in. And we don't even need the variable because I'm just going to change it on. Let's put this on its own line. And let's just say that we assert C, the Livewire component.
Let's put this on its own line. And let's just say that we assert C, the Livewire component. And that's called CreateIdea. Okay. Let's run this. Should pass. And it does. Next test I want to write is the validation test for our component. So let's do that. Say testCreateIdea, form validation works.
Test Component Validation5:26
So let's do that. Say test, create Idea, form validation works. And you can split this up into multiple methods, but I'm just gonna do it as one method. So I'll do Livewire acting as, and again, let's just make a User here, create. And we are testing. So let's say test the create Idea component. Let's make sure to import create Idea and Livewire as well. And we are going to set the fields. So if we go back to our create Idea, we're going to set these fields to empty and make sure that there are error messages.
So if we go back to our createIdea, we're going to set these fields to empty and make sure that there are error messages. So we can do $title to an empty string, and we can do the same for the others. So $category and $description. Okay. And we can assert that there are errors using assertHasErrors. And we can just use an array here to make sure those fields have errors associated with them. $title, $category, and $description. Okay.
Title, category, and description. Okay. And if you want, you can even assert that you see the errors. So I'll do that for one of them. assertSee the title field is required. Okay. So let's see if this passes should fit everything correctly. Okay. It doesn't. Oh, yeah.
It doesn't. Oh, yeah. So I forgot to call the actual method. So after we set these fields, we want to call the createIdea method here, and then that should trigger the errors. So let's say call createIdea. And this should work now should pass. Okay, cool. Okay. So the next test is actually making sure that the createIdea component works correctly.
Test Idea Creation Flow7:36
Okay. So the next test is actually making sure that the createIdea component works correctly. So let's say creating an Idea works correctly. Okay. So we have a few models to set up here, and I'm just going to paste that in. So you don't have to watch me do that. We're creating a User, we're creating two Categorys, and we're creating one Status. So let's just import these. Okay. And now we can test the actual Livewire component.
Okay. And now we can test the actual Livewire component. So Livewire acting as because we have to be logged in. So that's why we created the User. We are testing the CreateIdea class. And now let's set some fields. So similar to this. So we can just grab this and actually set it to some values. So say myFirstIdea, and then say categoryOneID, obviously, without the quotes. Let's add a description.
So say my first Idea, and then say categoryOneId, obviously, without the quotes. Let's add a description. So this is my first Idea, and it's called createIdea. And within the Livewire component, that should redirect us. So right here, we are being redirected. So we can assert redirect, and that should be on to the homepage. Okay. And let's see if that works for now. This should pass, and it doesn't. So you can see we are getting an error here on 67.
This should pass, and it doesn't. So you can see we are getting an error here on 67. I think I forgot to add the classes here. So let's say classes. I think this one is bg-gray-200. Does that fix it? Let's run this test again. Still fails. And that's because I have a typo here one more time, and we are passing now. So let's add a few more assertions here.
And that's because I have a typo here one more time, and we are passing now. So let's add a few more assertions here. So after the Livewire component, let's go ahead and visit that page. So we can do response this get route idea.index. And I guess we can visit this page as a logged in User actually doesn't matter, but let's do it anyway. So I'm just going to grab something from up here, grab this, and put it here, but let's use the User that we created. Okay. And let's assert successful.
Okay. And let's assert successful. You don't have to do this, but I like doing it to make sure the page loads correctly. And we can assert that we see the title cert C. So what I have, I have my first Idea. And if you want, you can do the description as well. So this is my first Idea. And then you can assert that the view is actually, no, we don't have to do that. Let's see if this works for now. And it does. If you want, you can even take it further and check if the database has the appropriate
And it does. If you want, you can even take it further and check if the database has the appropriate Idea that we just created. So if you want to do that, this cert database has check for the ideas table and check that we have something with a title field named my first idea. And hopefully this passes. And it does not. Should be ideas. Let's run that again. And it does pass now.
Test Unique Slug Generation11:45
Let's run that again. And it does pass now. Let's create one more test here. It's not really necessary in my opinion, but let's write it anyways. So that's if we create an Idea with the same title, it should still work because the slug that it generates should be unique. So should be very similar to this one, except we're going to create two ideas. So actually, let's just grab this entire test and modify it. So let's grab this, paste it in, change the title. So say creating two ideas with the same title still works, but has different slugs.
So let's grab this, paste it in, change the title. So say creating two Ideas with the same title still works, but has different slugs. So I'm going to remove all of these assertions here, and I'm just going to check the database for the appropriate slug so we can check the title. Like we did. But let's also add the slug and the slug for this one is my first idea. So this should pass. Obviously, we're just checking one, but we'll add another one after this passes. Okay, so let's add another one. So let's copy all of this and same title.
Okay, so let's add another one. So let's copy all of this and same title. Actually same everything here. But the only difference is the slug should have a number and this should work, hopefully. Okay, and it does run the entire file. Let's run the entire test suite and it's run parallel. So it's faster and everything is passing. So as always, let's make a commit here. This is episode 16, git add, git commit, episode 16, create IdeaTest.
This is episode 16, git add, git commit, episode 16, createIdeaTest.
