jrdavidson's avatar

Geneating A View for a Test

What I'm attempting to do is test that inside of my Livewire Type component when the type property is changed (wired to an id of an element in form) that it will load the correct view partial underneath it. If I run the test as it will send an error saying that the view doesn't exist which makes sense to me being a factory-generated view.

What would be the preferred to test this? Should I generate a view and place it inside the correct directory?

test('it adds correct partial based on type', function () {
    $type = Type::factory()->create();

    Livewire::test(Form::class)
        ->set('typeId', $type->id)
        ->assertSet('subViewToUse', 'types.'.$type->slug);
});
public function updatedTypeId()
{
    $typeSlug = Type::findOrFail($this->typeId)->slug;

    return $this->subViewToUse = 'types.'.$typeSlug;
}
0 likes
7 replies
jrdavidson's avatar

@webrobert Thanks for the response. Yes, I'm sorry there was a typo and it was fixed in the original post. The typo you saw did not actually exist in my code.

The problem I'm facing is I'm wanting to test that when a selection is made from a dropdown on the form that a view is loaded and displayed below the dropdown. Currently, a type is created from the factory and the slug of the type is used to locate which view to render.

I do have 10 different types in my database that are specific to my application so I'm curious to know if I need to test each specific type so that it does load the correct view or if it's saying to generate the factory like I currently am and then creating a blade and putting it in the right location and then do the check then in the teardown of this test remove the file or if I should go ahead and do a test for each of the types I already have in my DB.

Sinnbeck's avatar

@jrdavidson why not limit the factory to one of the 10 actually views? You an easily ask it to pick a random one from an array

$this->faker->randomElement($viewsArray), 
jrdavidson's avatar

@Sinnbeck SInce these type views is just HTML should I go ahead and just have them inside of views/types/xxx.blade.php or keep them under the livewire/types/xxx.blade.php

jrdavidson's avatar

@Sinnbeck

This is how I'm loading that view in my form. Is this how you would load the view in this situation?

@if ($subViewToUse)
    <div class="mb-10">
        <x-dynamic-component :component="$subViewToUse" class="mt-4" />
    </div>
@endif

Please or to participate in this conversation.