F
Filament5mo ago
CGM

In a test, should fillForm() be firing afterStateUpdated()?

I have the following field:
ColorPicker::make('primary_color')
->live()
->afterStateUpdated(function ($state) {
$this->dispatch('applyThemeColors', [
'primary_color' => $state,
]);
})
->hex()
->hexColor()
->nullable()
->helperText('Leave blank to use default colors'),
ColorPicker::make('primary_color')
->live()
->afterStateUpdated(function ($state) {
$this->dispatch('applyThemeColors', [
'primary_color' => $state,
]);
})
->hex()
->hexColor()
->nullable()
->helperText('Leave blank to use default colors'),
and the following test:
$component = livewire(BrandingSettings::class, ['location' => null])
->assertSuccessful()
->fillForm(['primary_color' => '#007bff'])
->assertDispatched('applyThemeColors');
$component = livewire(BrandingSettings::class, ['location' => null])
->assertSuccessful()
->fillForm(['primary_color' => '#007bff'])
->assertDispatched('applyThemeColors');
On the live site it all works, I assumed this test would have picked it up, however it doesn't. I'm just wondering is there something I need to do in my test to ensure the afterStateUpdated() is fired? The field is set to live().
8 Replies
CGM
CGMOP5mo ago
Ok, maybe I'm doing something dumb. The code is being called, when I dd() inside of that logic block it fails where I expect. I guess I just expected to be able to assert that that browser event was dispatched, but ->assertDispatched() doesn't seem to be doing what I think it should. Anyone see what i'm doing wrong here? Alright. After a bunch of poking around, it looks like this works:
livewire(BrandingSettings::class, ['location' => null])
->assertSuccessful()
->set('data.primary_color', '#007bff')
->assertDispatched('applyThemeColors', ['primary_color' => '#007bff']);
livewire(BrandingSettings::class, ['location' => null])
->assertSuccessful()
->set('data.primary_color', '#007bff')
->assertDispatched('applyThemeColors', ['primary_color' => '#007bff']);
I neeeded to use ->set() instead of formFill(). Hopefully this helps someone. 🙂
awcodes
awcodes5mo ago
Yea, for clarity formFill() is the initial state of the form. So it doesn’t ‘update’ the state.
CGM
CGMOP5mo ago
Thank you @awcodes. That makes much more sense now why it wasn't working. 🙂
awcodes
awcodes5mo ago
👍 it’s the same as calling $this->form->fill() in a mount method.
Adysone
Adysone4mo ago
@CGM Hi, does your test work? I just found out that the assertDispatched() syntax to test parameters is not
->assertDispatched('applyThemeColors', ['primary_color' => '#007bff']);
->assertDispatched('applyThemeColors', ['primary_color' => '#007bff']);
but
->assertDispatched('applyThemeColors', primary_color: '#007bff');
->assertDispatched('applyThemeColors', primary_color: '#007bff');
CGM
CGMOP4mo ago
It does.
public function assertDispatched($event, ...$params)
{
$result = $this->testDispatched($event, $params);

PHPUnit::assertTrue($result['test'], "Failed asserting that an event [{$event}] was fired{$result['assertionSuffix']}");

return $this;
}
public function assertDispatched($event, ...$params)
{
$result = $this->testDispatched($event, $params);

PHPUnit::assertTrue($result['test'], "Failed asserting that an event [{$event}] was fired{$result['assertionSuffix']}");

return $this;
}
The spread operator I think does the magic. Are you using pest? use function Pest\Livewire\livewire;
Adysone
Adysone4mo ago
I'm using PHPUnit and it works with that:
->assertDispatched('applyThemeColors', primary_color: '#007bff');
->assertDispatched('applyThemeColors', primary_color: '#007bff');
I just read this syntax in the docs of Livewire, so that's good for me. The other syntax doesn't work for me. But that's ok! Maybe it's because I have an array as value
CGM
CGMOP4mo ago
Could be. I'm not a huge fan of the Pest addin, but made the mistake of using it for a bunch of tests originally. If it works though I would just go with it. 🙂
Want results from more Discord servers?
Add your server