F
Filament5mo ago
CGM

How do I test MorphToSelect fields in forms?

I'm trying to write a test to ensure my MorphToSelect field is ->required(), but it is not behaving as I would expect. Are you all aware of any examples of testing the MorphToSelect field in custom forms, or even in panel forms? Here is the field for reference: Here is the example field for reference:
MorphToSelect::make('followup')
->required()
->types([
MorphToSelect\Type::make(CustomMessage::class)
->getOptionLabelFromRecordUsing(fn (CustomMessage $record): string => "{$record->name}")
->titleAttribute('name'),
MorphToSelect\Type::make(Redirect::class)
->getOptionLabelFromRecordUsing(fn (Redirect $record): string => "{$record->name}")
->titleAttribute('name'),
])
MorphToSelect::make('followup')
->required()
->types([
MorphToSelect\Type::make(CustomMessage::class)
->getOptionLabelFromRecordUsing(fn (CustomMessage $record): string => "{$record->name}")
->titleAttribute('name'),
MorphToSelect\Type::make(Redirect::class)
->getOptionLabelFromRecordUsing(fn (Redirect $record): string => "{$record->name}")
->titleAttribute('name'),
])
1 Reply
CGM
CGMOP5mo ago
Ahh, it's actually two fields that need to be tested together. Here is the overkill flow if anyone else runs into this.
test('the followup field is required', function () {
$user = User::factory()->isGlobalFullAccess()->create();
$this->actingAs($user);
$followup = Redirect::factory()->create();

livewire(CreateReviewFlow::class)
->fillForm([
'followup_type' => null
])
->call('create')
->assertHasFormErrors([
'followup_type' => 'required'
])
->fillForm([
'followup_type' => ''
])
->call('create')
->assertHasFormErrors([
'followup_type' => 'required'
])
->fillForm([
'followup_type' => 'App\Models\Redirect'
])
->call('create')
->assertHasNoFormErrors([
'followup_type' => 'required'
])
->assertHasFormErrors([
'followup_id' => 'required'
])
->fillForm([
'followup_type' => 'App\Models\Redirect',
'followup_id' => null
])
->call('create')
->assertHasNoFormErrors([
'followup_type' => 'required'
])
->assertHasFormErrors([
'followup_id' => 'required'
])
->fillForm([
'followup_type' => 'App\Models\Redirect',
'followup_id' => $followup->id
])
->assertHasNoFormErrors([
'followup_type' => 'required',
'followup_id' => 'required'
]);
});
test('the followup field is required', function () {
$user = User::factory()->isGlobalFullAccess()->create();
$this->actingAs($user);
$followup = Redirect::factory()->create();

livewire(CreateReviewFlow::class)
->fillForm([
'followup_type' => null
])
->call('create')
->assertHasFormErrors([
'followup_type' => 'required'
])
->fillForm([
'followup_type' => ''
])
->call('create')
->assertHasFormErrors([
'followup_type' => 'required'
])
->fillForm([
'followup_type' => 'App\Models\Redirect'
])
->call('create')
->assertHasNoFormErrors([
'followup_type' => 'required'
])
->assertHasFormErrors([
'followup_id' => 'required'
])
->fillForm([
'followup_type' => 'App\Models\Redirect',
'followup_id' => null
])
->call('create')
->assertHasNoFormErrors([
'followup_type' => 'required'
])
->assertHasFormErrors([
'followup_id' => 'required'
])
->fillForm([
'followup_type' => 'App\Models\Redirect',
'followup_id' => $followup->id
])
->assertHasNoFormErrors([
'followup_type' => 'required',
'followup_id' => 'required'
]);
});
Want results from more Discord servers?
Add your server