Testing custom table filters

I have a table with the following custom filter:
Tables\Filters\Filter::make('year')
->form([
Forms\Components\TextInput::make('yearFrom')
->label('From Year'),
Forms\Components\TextInput::make('yearTo')
->label('To Year')
])
->query(function (Builder $query, array $data) {
return $query
->when($data['yearFrom'], function (Builder $query, $yearFrom) {
$query->where('year', '>=', $yearFrom);
})
->when($data['yearTo'], function (Builder $query, $yearTo) {
$query->where('year', '<=', $yearTo);
});
})
Tables\Filters\Filter::make('year')
->form([
Forms\Components\TextInput::make('yearFrom')
->label('From Year'),
Forms\Components\TextInput::make('yearTo')
->label('To Year')
])
->query(function (Builder $query, array $data) {
return $query
->when($data['yearFrom'], function (Builder $query, $yearFrom) {
$query->where('year', '>=', $yearFrom);
})
->when($data['yearTo'], function (Builder $query, $yearTo) {
$query->where('year', '<=', $yearTo);
});
})
I'm trying to write a test case for this filter that looks like this:
it('can filter posts by a year from', function () {

$cars = Car::factory()->count(3)
->state(new Sequence(
['year' => 2016],
['year' => 2017],
['year' => 2018],
))
->for(Brand::factory())
->create();

$yearFrom = 2017;

Livewire::test(CarList::class)
->assertCanSeeTableRecords($cars)
->filterTable('year', $yearFrom )
->assertCanSeeTableRecords(
$cars->where('year', '>=', $yearFrom)
)
->assertCanNotSeeTableRecords(
$cars->where('year', '<', $yearFrom)
);
});
it('can filter posts by a year from', function () {

$cars = Car::factory()->count(3)
->state(new Sequence(
['year' => 2016],
['year' => 2017],
['year' => 2018],
))
->for(Brand::factory())
->create();

$yearFrom = 2017;

Livewire::test(CarList::class)
->assertCanSeeTableRecords($cars)
->filterTable('year', $yearFrom )
->assertCanSeeTableRecords(
$cars->where('year', '>=', $yearFrom)
)
->assertCanNotSeeTableRecords(
$cars->where('year', '<', $yearFrom)
);
});
My problem is that, when I run this test, the data coming into the ->query() method is empty
[
'yearFrom' => null
'yearTo' => null
]
[
'yearFrom' => null
'yearTo' => null
]
How can I set the values of a custom filter using ->filterTable()?
1 Reply
Dan Harrin
Dan Harrin8mo ago
->filterTable('year', [
'yearFrom' => 2017
'yearTo' => 2018
])
->filterTable('year', [
'yearFrom' => 2017
'yearTo' => 2018
])
looks like you are currently passing just 1 year, no array.
Want results from more Discord servers?
Add your server