Test Livewire assertDispatched and assertSee after event dispatched

Hello,

I have the following field :

Forms\Components\Select::make('licenses')
    ->label(__('vmc.licenses'))
    ->preload()
    ->relationship('licenses', 'name', fn (Builder $query) => $query->orderBy('name'))
    ->multiple()
    ->searchable()
    ->live()
    ->afterStateHydrated(function (Component $livewire, ?array $state) {
        $livewire->dispatch('updatedSelectedLicenses', selectedLicenses: $state);
    })
    ->afterStateUpdated(function (Component $livewire, ?array $state) {
        $livewire->dispatch('updatedSelectedLicenses', selectedLicenses: $state);
    })


And I have the following test :

$license = License::factory()->create(['cost' => 22.1234]);

Livewire::test(CreateVirtualMachine::class)
    ->assertFormFieldExists('licenses')
    ->set('data.licenses', [$license->id])
    ->assertDispatched('updatedSelectedLicenses', ['selectedLicenses' => [$license->id]])
    ->assertSee('22,12');


When I run my test, I have the following fails :

Failed asserting that an event [updatedSelectedLicenses] was fired with parameters: [{"selectedLicenses":[2]}]


and

Failed asserting that '<bunch of html>' [UTF-8](length: 83059) contains "22,12" [ASCII](length: 5).


  1. I don't understand why the first assertion fails and why the parameters aren't correct as I send the $state when I dispatch the event.
  2. I don't understand why 22,12 is not in the HTML as when I test it in my browser, it works like a charm.
Solution
I answer to myself, I found a topic on Laracast for the second problem I have : https://laracasts.com/discuss/channels/livewire/livewire-testing-nested-components

For the first issue, I don't use the good syntax, which must be ->assertDispatched('updatedSelectedLicenses', selectedLicenses: [$license->id])
Laracasts
Was this page helpful?