F
Filamentβ€’2w ago
D5300

Field is hidden test - Pest

Hi guys, I have on my tenant create a form which I want to test. It has a depending field which I try to test if its visible or not. But using isVisible or isHidden, both return an error: Failed asserting that a field with the name [company_name] is hidden on the form named [form] on the [App\Filament\Pages\Tenancy\RegisterWebsite] component. Failed asserting that an array does not have the key 'company_name'. My test looks like the following:
it('hides the company name field when you want to create a new company', function () {
$user = newUser();
$this->actingAs($user);
$company = Company::factory(['owner_id' => $user])->create();
Website::factory(['company_id' => $company])->create();

$livewire = livewire(RegisterWebsite::class);

$livewire->fillForm([
'company_id' => Company::first()->id
]);

$livewire->assertFormFieldIsHidden('company_name');
});
it('hides the company name field when you want to create a new company', function () {
$user = newUser();
$this->actingAs($user);
$company = Company::factory(['owner_id' => $user])->create();
Website::factory(['company_id' => $company])->create();

$livewire = livewire(RegisterWebsite::class);

$livewire->fillForm([
'company_id' => Company::first()->id
]);

$livewire->assertFormFieldIsHidden('company_name');
});
The form will be in a seperate message as I dont have discord premium πŸ˜„
1 Reply
D5300
D5300OPβ€’2w ago
public function form(Form $form): Form
{
$companies = Company::getActiveUserCompanies()->toArray();

return $form
->columns(1)
->schema([
Select::make('company_id')
->placeholder('Placeholder text')
->default('new')
->required()
->live()
->reactive()
->hidden(empty($companies))
->selectablePlaceholder(false)
->options(fn() => [
'new' => 'Create new company',
'My companies' => $companies,
]),

TextInput::make('company_name')
->label('Company name')
->visible(function (Get $get) use ($companies) {
if (empty($companies)) {
return true;
}

return $get('company_id') === 'new';
})
->required(),

TextInput::make('domain')->rules('required'),
,
]);
}
public function form(Form $form): Form
{
$companies = Company::getActiveUserCompanies()->toArray();

return $form
->columns(1)
->schema([
Select::make('company_id')
->placeholder('Placeholder text')
->default('new')
->required()
->live()
->reactive()
->hidden(empty($companies))
->selectablePlaceholder(false)
->options(fn() => [
'new' => 'Create new company',
'My companies' => $companies,
]),

TextInput::make('company_name')
->label('Company name')
->visible(function (Get $get) use ($companies) {
if (empty($companies)) {
return true;
}

return $get('company_id') === 'new';
})
->required(),

TextInput::make('domain')->rules('required'),
,
]);
}

Did you find this page helpful?