Testing modal form fields.

Given an action with a modal, with a form schema, I'd like to test that the form has specific fields. How is that done? Example action
Action::make('export')
->label('Export Records')
->form([
Select::make('export_format')
->label('Export Format')
->options([
'csv' => 'CSV',
])
->required(),
])
->modalWidth('lg')
->modalSubmitActionLabel('Export')
->icon('heroicon-o-cloud-arrow-down')
->color('gray')
->link();
Action::make('export')
->label('Export Records')
->form([
Select::make('export_format')
->label('Export Format')
->options([
'csv' => 'CSV',
])
->required(),
])
->modalWidth('lg')
->modalSubmitActionLabel('Export')
->icon('heroicon-o-cloud-arrow-down')
->color('gray')
->link();
Example test
/** @test */
public function export_action_modal_has_select_field_for_format()
{
Livewire::test(DomainActions::class, ['record' => $this->domain])
->mountAction('export')
->assertFormExists('form') // works
->assertFormFieldExists('export_format') // fails;
}
/** @test */
public function export_action_modal_has_select_field_for_format()
{
Livewire::test(DomainActions::class, ['record' => $this->domain])
->mountAction('export')
->assertFormExists('form') // works
->assertFormFieldExists('export_format') // fails;
}
Fails with Failed asserting that a field with the name [export_format] exists on the form with the name [form] on the [App\Filament\App\Resources\DomainResource\Widgets\DomainActions] component. Failed asserting that null is an instance of class Filament\Forms\Components\Field.
5 Replies
N@meless
N@meless4mo ago
/** @test */ public function export_action_modal_has_select_field_for_format() { Livewire::test(DomainActions::class, ['record' => $this->domain]) ->mountAction('export') ->assertActionHasFormField('export', 'export_format'); }
reppair
reppairOP4mo ago
BadMethodCallException: Method Illuminate\Http\JsonResponse::assertActionFormFieldExists does not exist.
BadMethodCallException: Method Illuminate\Http\JsonResponse::assertActionFormFieldExists does not exist.
N@meless
N@meless4mo ago
/** @test */ public function export_action_modal_has_select_field_for_format() { Livewire::test(DomainActions::class, ['record' => $this->domain]) ->mountAction('export') ->assertSet('mountedAction', 'export') ->assertSet('mountedActionFormComponent', 'export') ->assertSet('mountedActionFormData.export_format', null); } or /** @test */ public function export_action_modal_has_select_field_for_format() { $component = Livewire::test(DomainActions::class, ['record' => $this->domain]) ->mountAction('export'); $formData = $component->get('mountedActionFormData'); $this->assertArrayHasKey('export_format', $formData); }
reppair
reppairOP4mo ago
So I take it there is no simple and straight forward way of testing forms in modals?
Kevin
Kevin3mo ago
I think this should work for you:
Livewire::test(DomainActions::class, ['record' => $this->domain])
->mountAction('export')
->assertFormFieldExists('export_format', 'mountedActionForm');
Livewire::test(DomainActions::class, ['record' => $this->domain])
->mountAction('export')
->assertFormFieldExists('export_format', 'mountedActionForm');

Did you find this page helpful?