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

Unit test status counts0:00

Okay, let's go ahead and write tests for our status filters here. Let's start with the count, which we did in the last video. And that's going to be a unit test, because we created it on the Status model. So let's go ahead and do that first. php artisan make:test StatusTest, and let's make it a unit test. Okay. And let's go to that StatusTest. And let's get rid of this. Let's import the Laravel test case. So I'll just grab it from this other unit test that we have. Okay. And also we need RefreshDatabase. So we can grab it here. And let's use it over here. And let's import it too. Okay. So first test, or only test, let's name it canGetCountOfEachStatus. Okay. And let me just paste in some setup code here. Okay, so I pasted in a User, a Category, and five different Status instances. So let me just import these. And I don't have the classes specified here. So let's make sure to add a default on our StatusFactory here. So let's just say classes.

and five different statuses. So let me just import these. And I don't have the classes specified here. So let's make sure to add a default on our StatusFactory here. So let's just say classes. And it's just defaulted to whatever say bg-gray-200. Okay. So now I'm going to create 15 different Idea instances that correspond to these categories here. So again, I'll just paste that in. Okay, so I pasted in 15 different Idea instances. And I believe I have one for open, two for considering, 3, 4, 5. And if my math is correct, that should be 15 total Idea instances. So let's go ahead and make some assertions down here. After all of our Idea instances. So let's say this->assertEquals(15, status::getCount()); And remember, this returns an array. And we can grab the all statuses. Okay. And let's see if this works. Let's go ahead and run that. And it does pass. So let's go ahead and make assertions for the

returns an array. And we can grab the all statuses. Okay. And let's see if this works. Let's go ahead and run that. And it does pass. So let's go ahead and make assertions for the other statuses as well. So there should be one for the open status, two for the considering status, and so on and so forth. So let's just try these two. And then I'll paste the rest in. Okay, and like I said, I'll just paste the rest in. Okay, so I pasted the rest in, let's go ahead and test this. And it does seem to work. So just to make sure this is working, let's make a test fail. So I'm going to comment out the first one. So there should be no status open ones. So let's try running that should fail. Okay, so it does fail says 14 does not match 15. Okay, so let's put that back. And let's go ahead and make a test for the actual component. So php artisan make:test,

Component existence tests3:07

Okay, so it does fail says 14 does not match 15. Okay, so let's put that back. And let's go ahead and make a test for the actual component. So php artisan make:test, let's call it StatusFiltersTest. Okay, let's go to that StatusFiltersTest. And let's get rid of this. And first test, as always, is the existence test. So let's say index page contains StatusFilters Livewire component. Actually, let's use refreshDatabase first. refreshDatabase first. And as always, I'm just going to paste in some setup here, just creating a User and an Idea to import all of that. Okay. And let's go ahead and go to that route, or go to the index route, they wrote idea.index. And let's assert see Livewire. And the name of the component is StatusFilters.

to import all of that. Okay. And let's go ahead and go to that route, or go to the index route, they wrote idea.index. And let's assert Livewire. And the name of the component is StatusFilters. Okay, this should work, hopefully. And it does. So the next test is going to be the same thing but for the show page, because it should render on that page as well. So just one difference here. Obviously, the name, change it to showPage. And this is idea.show. And we need a program for this. So let's pass in the idea. And this should pass as well. Okay, so next test I'm going to write is one to check if the statusCount shows correctly. So we already have the unit test. I just want to make sure it renders correctly using the Livewire component. So let's just say shows correct statusCount. Okay. And I'm going to use a different status here. Instead of status.

Verify rendered status counts5:07

I just want to make sure it renders correctly using the Livewire component. So let's just say shows correct status count. Okay. And I'm going to use a different status here. Instead of status open, let's say status implemented, and name is implemented. And I'm going to pass in the ID. And we have to make sure that we force the correct ID here. So for implemented, it's 4. So if you remember, we are grabbing the status count using the ID and 4 is implemented here. Okay, and let's change this status opened to status implemented. Okay. And we don't need this variable. Let's make another Idea here. So it can have the same name doesn't matter. I just want to make sure that the count is correct. So let's get rid of this. And let's go ahead and test the Livewire component. So Livewire test status filters, class, make sure to import this. So we should see

Test query string filtering6:07

So let's get rid of this. And let's go ahead and test the Livewire component. So livewire test status filters, class, make sure to import this. So we should see all ideas to and also whatever status I used to considering or implemented. So let's assert that we see that. So assert, see all ideas to all ideas to. So I forgot to put quotes. Okay. And it's also assert see implemented to. Okay, let's see if this works. Livewire not found. Import that. Try it again. And we are passing. Okay, next test I want to write is checking if the query string actually filters the statuses. So let's grab this or duplicate this. And let's change the name here. Say filtering works when query string in place.

the statuses. So let's grab this or duplicate this. And let's change the name here. Say filtering works when query string in place. So let's get rid of everything here. And I'm going to paste in some setup. So we have a User, we have a Category like before, and we have a bunch of different statuses. So for these two statuses, considering and in progress, I actually have the classes set here. So bg-purple, text-white and bg-yellow text-white, because we're going to assert that we see the actual badge. So let me paste in the different ideas that I have here. And I believe I have five. So I have one with the considering. So actually, I have five. So actually, I have two with the considering status. And I have three with the in progress status. Okay, so let's go ahead and make our assertions. So let's save response.

So actually, I have two with the considering status. And I have three with the in progress status. Okay, so let's go ahead and make our assertions. So let's save response. And let's go ahead and go to the index route. So idea.index. And for now, let's not add anything in the query string. So it should default to all. So let's just say response.assertSuccessful. For now, this should obviously work. Or maybe not. Sorry, should be route idea.index. Okay, try that again. And let's go ahead and make a few more assertions here. So response.assertSee. And in this case, I am testing against the HTML here. So the second parameter is going to be false. So we just want to check that we see the badges for the ideas that have the statuses. So in this case, in progress, and what's the other one considering.

parameter is going to be false. So we just want to check that we see the badges for the Idea that have the statuses. So in this case, in progress, and what's the other one considering. So there should be the HTML for in progress. Again, you can test the Livewire component directly if you want. But I'm just testing the response here. So let's go ahead and take a look at an in progress badge here. Here's one. Go ahead and check the HTML out. So let's copy our HTML and paste that in here. Okay, so that's the markup for in progress. And we should see this because we are grabbing every status here. And false, like I said, doesn't escape the the string here. So it should work. Let's test that out. Okay, let's also assert that we see the other status. So let's grab it for considering as well. There's one. Let's grab that HTML. Let's duplicate this. And let's change this here. And let's paste that in. Let's test that out.

the other status. So let's grab it for considering as well. There's one. Let's grab that HTML. Let's duplicate this. And let's change this here. And let's paste that in. Let's test that out. Okay, but we want to test that the actual filtering works. So let's go ahead and change the query string here. So let's set it to say status is in progress. Okay, and now we should see in progress, but we should not see this status considering. So let's change it to assertDontSee. And let's see if this works. And it does. So if I were to change it to assertSee, this should fail. It does. Okay. Again, if you want, you can test the Livewire component directly. And there's actually a method that helps you do that with the query string. As you see here, you can do Livewire with query params and then pass in a key and a value. And then you can go ahead and test the component. So in this case, we test the IdeasIndex component.

Selected status on pages11:04

string. As you see here, you can do Livewire with query params and then pass in a key and a value. And then you can go ahead and test the component. So in this case, we test the IdeasIndex component. So the one that has all the ideas. And then we can use something like assertSee and see if the statuses exist. But in this case, like I said, we're just doing it directly on the response. Sorry, here. Okay, let's write a few more tests here. It's gonna grab this as our starting point and paste it down here. So the first one is just to check if show page does not show selected status. So if you remember, if we go to the show page here, let me just get rid of this, then there should be no selected status here. But when we're on the index page, there should be a selected one. So let's go ahead and do that. Say $response is this get, let's go to the show page,

should be a selected one. So let's go ahead and do that. Say response is this get, let's go to the show page, the .show and the param is the idea. And let's just assert that we don't see the HTML. In this case, the classes that specify if something is selected. So in this case, I'm just going to do border-blue text-gray-900. border-blue text-gray-900. So this should pass. Let's try that out. And it doesn't undefined variable, we have to do this, run it again. Okay. And we can pretty much do the same thing for the other case for the idea page. So let's duplicate this index page shows selected status. Okay. And we are going to the index page here. So idea index, we don't need this. And let's assert that we do see that.

shows selected status. Okay. And we are going to the index page here. So idea index, we don't need this. And let's assert that we do see that. Okay. And we are passing. So yeah, you can be more specific and test that the actual thing that you clicked on has the selected status. But I'm okay with just this. So let's go ahead and run the entire test suite. And everything is still passing. So as always, let's go ahead and make a commit. This is episode 26. git commit. Episode 26. StatusFiltersTest.

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