vincent15000's avatar

assertSet seems to not refresh the view

Hello,

I have a Livewire component to display a list of recipes. In this component I have a checkbox to choose to display or not the shared recipes.

public $withSharedRecipes;

public function render()
{
    $searches = explode(' ', $this->search);
    $searches = array_diff($searches, ['']);

    $recipes = Recipe::
        with('user:id,name')
        ->when($this->category_id, function ($query) {
            $query->where('category_id', $this->category_id);
        })
        ->when(count($searches) > 0, function ($query) use ($searches) {
            foreach ($searches as $search) {
                $query->where(function ($query) use($search) {
                    $query
                        ->where('name', 'like', '%'.$search.'%');
                });
            }
        })
        ->when($this->onlySelections, function ($query) {
            $query->whereIn('id', auth()->user()->meals->pluck('id')->toArray());
        })
        ->where(function ($query) {
            $query
                ->where('user_id', auth()->id())
                ->when($this->withSharedRecipes, function ($query) {
                    $ownerIds = User::whereRelation('sharings', 'friend_id', auth()->id())->pluck('id');
                    $query->orWhereIn('user_id', $ownerIds);
                })
                ->when($this->withPublishedRecipes, function ($query) {
                    $query->orWhere('published', true);
                });
        })
        ->orderBy('name')
        ->paginate(12);

    return view('livewire.pages.recipes', compact('recipes'));
}

To test the checkbox in this component, I have this code.

/** @test */
public function user_can_see_shared_recipes_if_checked(): void
{
    $category = new Category;
    $category->name = 'Une catégorie';
    $category->save();

    $recipe = new Recipe;
    $recipe->category_id = $category->id;
    $recipe->name = 'Recette user 2';
    $recipe->preparation_time = 30;
    $recipe->rest_time = 60;
    $recipe->cooking_time = 45;
    $recipe->portions = 4;
    $recipe->user_id = $this->user_2->id;
    $recipe->save();

    $this->user_2->sharings()->attach($this->user_1->id);

    Livewire::
        actingAs($this->user_1)
        ->test(Recipes::class)
        ->set('withSharedRecipes', true)
		->assertSet('withSharedRecipes', true)
        ->assertSee('Recette user 2');
}

When I set the $withSharedRecipes in the test, it's set to true, but when I check if I see Recette user 2, the test fails, whereas it should be successful. For me that means that the component isn't rerendered when setting a property.

I have tried to force with adding ->refresh() just after having set the property in the test, but I get an error saying that refresh() doesn't exist.

Do you have any idea about what I can do so that the test is successful ?

Thanks a lot for your help.

V

0 likes
4 replies
tisuchi's avatar

@vincent15000 I think you can use call() method to invoke the render.

For example:

Livewire::
    actingAs($this->user_1)
    ->test(Recipes::class)
    ->set('withSharedRecipes', true)
    ->call('render')
    ->assertSee('Recette user 2');
1 like
vincent15000's avatar

@tisuchi Thank for your help. Well ... I had already tried to call the render() method, but I get an error saying that the render() method is not found on this component.

vincent15000's avatar

@tisuchi The only solution I have found is to set the property to the opposite boolean value, then call the toggle method from the Livewire controller.

Livewire::
    actingAs($this->user_1)
    ->test(Recipes::class)
    ->set('withSharedRecipes', false)
	->call('toggleWithSharedRecipes')
    ->assertSet('toggleWithSharedRecipes', true)
    ->assertSee('Recette user 2');

This solution is temporary.

Do you see any other solution that would be better ?

Please or to participate in this conversation.