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 schema 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').