M a k s
M a k s
FFilament
Created by M a k s on 2/2/2025 in #❓┊help
Filter test FAILED `filterTable` doesn't work
I found the reason why it didn't work Here is part of the filter I have
Filter::make('name')
->form([
TextInput::make('name')
->placeholder('Enter name to filter')
->label('Name'),
])
->query(
fn (Builder $query, array $data) => isset($data['name']) && is_string($data['name'])
? $query->whereLike('name', "%{$data['name']}%")
: $query
),
Filter::make('name')
->form([
TextInput::make('name')
->placeholder('Enter name to filter')
->label('Name'),
])
->query(
fn (Builder $query, array $data) => isset($data['name']) && is_string($data['name'])
? $query->whereLike('name', "%{$data['name']}%")
: $query
),
and here is a test that runs successfully
it('applies the `name` filter correctly', function () {
$users = User::factory(5)->create();
$filteredUser = $users->first();
$nonFilteredUsers = $users->reject(fn($user) => $user->name === $filteredUser->name);

livewire(ListUsers::class)
->assertCanSeeTableRecords($users)
->filterTable('name', ['name' => $filteredUser->name])
->assertCountTableRecords(1)
->assertCanSeeTableRecords([$filteredUser])
->assertCanNotSeeTableRecords($nonFilteredUsers);
});
it('applies the `name` filter correctly', function () {
$users = User::factory(5)->create();
$filteredUser = $users->first();
$nonFilteredUsers = $users->reject(fn($user) => $user->name === $filteredUser->name);

livewire(ListUsers::class)
->assertCanSeeTableRecords($users)
->filterTable('name', ['name' => $filteredUser->name])
->assertCountTableRecords(1)
->assertCanSeeTableRecords([$filteredUser])
->assertCanNotSeeTableRecords($nonFilteredUsers);
});
pay attention to the correct writing of the filterTable method with the passing of the necessary parameters
4 replies