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

11 messages · Page 1 of 1 (latest)

final ether
#

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'),

and the following test:

    $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().

ember canopyBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

final ether
#

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']);

I neeeded to use ->set() instead of formFill().

Hopefully this helps someone. 🙂

sullen spindle
#

Yea, for clarity formFill() is the initial state of the form. So it doesn’t ‘update’ the state.

final ether
#

Thank you @sullen spindle. That makes much more sense now why it wasn't working. 🙂

sullen spindle
#

👍 it’s the same as calling $this->form->fill() in a mount method.

worthy wraith
#

@final ether Hi, does your test work? I just found out that the assertDispatched() syntax to test parameters is not

->assertDispatched('applyThemeColors', ['primary_color' => '#007bff']);

but

->assertDispatched('applyThemeColors', primary_color: '#007bff');
final ether
#

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;
    }

The spread operator I think does the magic.

#

Are you using pest?

use function Pest\Livewire\livewire;

worthy wraith
#

I'm using PHPUnit and it works with that:

->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