Testing schemas
Filling a form in a test
To fill a form with data, pass the data to fillForm()
:
use function Pest\Livewire\livewire;
livewire(CreatePost::class)
->fillForm([
'title' => fake()->sentence(),
// ...
]);
If you have multiple schemas on a Livewire component, you can specify which form you want to fill using
fillForm([...], 'createPostForm')
.
Testing form field and infolist entry state
To check that a form has data, use assertSchemaStateSet()
:
use Illuminate\Support\Str;
use function Pest\Livewire\livewire;
it('can automatically generate a slug from the title', function () {
$title = fake()->sentence();
livewire(CreatePost::class)
->fillForm([
'title' => $title,
])
->assertSchemaStateSet([
'slug' => Str::slug($title),
]);
});
If you have multiple schemas on a Livewire component, you can specify which schema you want to check using
assertSchemaStateSet([...], 'createPostForm')
.
You may also find it useful to pass a function to the assertSchemaStateSet()
method, which allows you to access the form $state
and perform additional assertions:
use Illuminate\Support\Str;
use function Pest\Livewire\livewire;
it('can automatically generate a slug from the title without any spaces', function () {
$title = fake()->sentence();
livewire(CreatePost::class)
->fillForm([
'title' => $title,
])
->assertSchemaStateSet(function (array $state): array {
expect($state['slug'])
->not->toContain(' ');
return [
'slug' => Str::slug($title),
];
});
});
You can return an array from the function if you want Filament to continue to assert the achema state after the function has been run.
Testing form validation
Use assertHasFormErrors()
to ensure that data is properly validated in a form:
use function Pest\Livewire\livewire;
it('can validate input', function () {
livewire(CreatePost::class)
->fillForm([
'title' => null,
])
->call('create')
->assertHasFormErrors(['title' => 'required']);
});
And assertHasNoFormErrors()
to ensure there are no validation errors:
use function Pest\Livewire\livewire;
livewire(CreatePost::class)
->fillForm([
'title' => fake()->sentence(),
// ...
])
->call('create')
->assertHasNoFormErrors();
If you have multiple schemas on a Livewire component, you can pass the name of a specific form as the second parameter like
assertHasFormErrors(['title' => 'required'], 'createPostForm')
orassertHasNoFormErrors([], 'createPostForm')
.
Testing the existence of a form
To check that a Livewire component has a form, use assertFormExists()
:
use function Pest\Livewire\livewire;
it('has a form', function () {
livewire(CreatePost::class)
->assertFormExists();
});
If you have multiple schemas on a Livewire component, you can pass the name of a specific form like
assertFormExists('createPostForm')
.
Testing the existence of form fields
To ensure that a form has a given field, pass the field name to assertFormFieldExists()
:
use function Pest\Livewire\livewire;
it('has a title field', function () {
livewire(CreatePost::class)
->assertFormFieldExists('title');
});
You may pass a function as an additional argument to assert that a field passes a given "truth test". This is useful for asserting that a field has a specific configuration:
use function Pest\Livewire\livewire;
it('has a title field', function () {
livewire(CreatePost::class)
->assertFormFieldExists('title', function (TextInput $field): bool {
return $field->isDisabled();
});
});
To assert that a form does not have a given field, pass the field name to assertFormFieldDoesNotExist()
:
use function Pest\Livewire\livewire;
it('does not have a conditional field', function () {
livewire(CreatePost::class)
->assertFormFieldDoesNotExist('no-such-field');
});
If you have multiple schemas on a Livewire component, you can specify which form you want to check for the existence of the field like
assertFormFieldExists('title', 'createPostForm')
.
Testing the visibility of form fields
To ensure that a field is visible, pass the name to assertFormFieldVisible()
:
use function Pest\Livewire\livewire;
test('title is visible', function () {
livewire(CreatePost::class)
->assertFormFieldVisible('title');
});