Summer Sale! All accounts are 50% off this week.

Annaro's avatar

Testing Livewire component that is updated if event is fired

I have a Livewire component called AllJobsInProgress, which contains another Livewire component called CheckIfJobCompleted. When component CheckIfJobCompleted fires event 'refreshDisplay', then parent component AllJobsInProgress should run function 'refreshDisplay'.

The whole thing works well.

But i have issues with writing the tests. When the event 'refreshDisplay' is fired by the CheckIfJobCompleted component (this works), the function 'refreshDisplay' from parent component AllJobsInProgress component is not called. WHY? HOW CAN I TEST THIS?

Also, with the following code, of course the last assertion does not work, because the content evaluated for the $component variable is the same at the end as it is at the beginning. But i'd like to know if this is normal simply because of the problem i am having now, e.g. the component does not run the 'refreshDisplay' function ? Or if it is always going to be the case, because the variable $component just stores the content that was passed to it at the beginning, and then keeps that same content even if the content of the component is refreshed? How can i check the new content of this AllJobsInProgress component?

$component = Livewire::test(AllJobsInProgress::class, [ 'user' => User::first() ])
->assertSee('display_progress_job_'.JobStatus::first()->id);

sleep(35);
        
Livewire::test(CheckIfJobCompleted::class, [ 
            'jobStatusId' => JobStatus::first()->id,
            'jobStatusJobId' => $first_job_id
        ])->assertEmitted('refreshDisplay');

$component->assertSee('display_info_completed_job_'.JobStatus::first()->id);

Note that i CANNOT do the following, instead of the last assertion, because it would "mount" my component, which makes a brand new component and not a "refresh" of the old one.

Livewire::test(AllJobsInProgress::class, [ 'user' => User::first() ])
->assertSee('display_info_completed_job_'.JobStatus::first()->id);

Thanks!

0 likes
0 replies

Please or to participate in this conversation.