Test Form Component Action

I have a Custom Page which includes a header Action. The Action is a slide over, which shows a form to create a model. I want to test if the action executes as expected an create the record, as well as if the fields that are conditionally disabled are really disabled when they should be. My test looks like this :
it('can create tariff', function() {
$tariff = Tariff::factory()->make();
livewire(ListLocationTariffs::class, ['record' => $this->location->uuid])
->callAction('createTariff')
->setActionData($tariff->toArray())
->assertFormExists()
// ->assertFormFieldExists('mountedActionsData.0.name')
->callMountedAction();

$this->assertDatabaseHas('tariffs', [... $tariff->toArray(), 'location_id' => $this->location->id]);
});
it('can create tariff', function() {
$tariff = Tariff::factory()->make();
livewire(ListLocationTariffs::class, ['record' => $this->location->uuid])
->callAction('createTariff')
->setActionData($tariff->toArray())
->assertFormExists()
// ->assertFormFieldExists('mountedActionsData.0.name')
->callMountedAction();

$this->assertDatabaseHas('tariffs', [... $tariff->toArray(), 'location_id' => $this->location->id]);
});
and passes when executed.
2 Replies
KarlBoosterBär
KarlBoosterBärOP4mo ago
The Form looks like this:
public static function form(Form $form): Form
{

return $form
->schema([

/**
* GENERAL SECTION
*/
Section::make()
->heading('Allgemein')
->description('Geben Sie hier die Informationen ein, die zur Auffindbarkeit des Tarifs und zur korrekten Zuordnung benötigt werden.')
->columns(2)
->schema([

/**
* TYPE FIELD
*/
Select::make('type')
->label('Medium')
->columnSpan(1)
->options(MediumType::class)
->searchable()
->required()
->live()
->disabled(function (string $operation): bool {
return $operation !== 'create';
}),

/**
* NAME FIELD
*/
TextInput::make('name')
->label('Name')
->placeholder('Stromtarif')
->columnSpan(1)
->required()
->autocomplete(false)
->minLength(3)
->maxLength(255),

/**
* DESCRIPTION FIELD
*/
TextInput::make('description')
->label('Beschreibung')
->placeholder('Wird vssl. zum Ende des Jahres gekündigt')
->columnSpan(2)
->nullable(),
...
public static function form(Form $form): Form
{

return $form
->schema([

/**
* GENERAL SECTION
*/
Section::make()
->heading('Allgemein')
->description('Geben Sie hier die Informationen ein, die zur Auffindbarkeit des Tarifs und zur korrekten Zuordnung benötigt werden.')
->columns(2)
->schema([

/**
* TYPE FIELD
*/
Select::make('type')
->label('Medium')
->columnSpan(1)
->options(MediumType::class)
->searchable()
->required()
->live()
->disabled(function (string $operation): bool {
return $operation !== 'create';
}),

/**
* NAME FIELD
*/
TextInput::make('name')
->label('Name')
->placeholder('Stromtarif')
->columnSpan(1)
->required()
->autocomplete(false)
->minLength(3)
->maxLength(255),

/**
* DESCRIPTION FIELD
*/
TextInput::make('description')
->label('Beschreibung')
->placeholder('Wird vssl. zum Ende des Jahres gekündigt')
->columnSpan(2)
->nullable(),
...
I have got a second test to assert the validation errors which passes too:
it('has form errors on create', function() {
livewire(ListLocationTariffs::class, ['record' => $this->location->uuid])
->callAction('createTariff')
->setActionData([])
->callMountedAction()
->assertHasErrors(['mountedActionsData.0.name' => 'required','mountedActionsData.0.type' => 'required', 'mountedActionsData.0.starts_at' => 'required']);
});
it('has form errors on create', function() {
livewire(ListLocationTariffs::class, ['record' => $this->location->uuid])
->callAction('createTariff')
->setActionData([])
->callMountedAction()
->assertHasErrors(['mountedActionsData.0.name' => 'required','mountedActionsData.0.type' => 'required', 'mountedActionsData.0.starts_at' => 'required']);
});
Now when i add the ->assertFormFieldExists('mountedActionsData.0.name') function the test fails with this error message: "Failed asserting that a field with the name [data.mountedActionsData.0.name] exists on the form with the name [form] on the [App\Filament\Customer\Resources\LocationResource\Pages\ListLocationTariffs] component." I don't know if my test overall is correct and how i can test the form fields. Would be really nice if someone could help me with this.
KingStalker
KingStalker4mo ago
i think its like this
->assertHasErrors([
'name' => ['required' => 'The name field is required.'],
'type' => ['required' => 'The type field is required.'],
'starts_at' => ['required' => 'The start At field is required.']
]);

// or

->assertHasErrors([
'name' => ['required'],
'type' => ['required'],
'starts_at' => ['required']
]);
->assertHasErrors([
'name' => ['required' => 'The name field is required.'],
'type' => ['required' => 'The type field is required.'],
'starts_at' => ['required' => 'The start At field is required.']
]);

// or

->assertHasErrors([
'name' => ['required'],
'type' => ['required'],
'starts_at' => ['required']
]);
Want results from more Discord servers?
Add your server