Testing custom table filters
I have a table with the following custom filter:
I'm trying to write a test case for this filter that looks like this:
My problem is that, when I run this test, the data coming into the
How can I set the values of a custom filter using
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);
});
})
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)
);
});
->query()
method is empty
[
'yearFrom' => null
'yearTo' => null
]
[
'yearFrom' => null
'yearTo' => null
]
->filterTable()
?1 Reply
->filterTable('year', [
'yearFrom' => 2017
'yearTo' => 2018
])
->filterTable('year', [
'yearFrom' => 2017
'yearTo' => 2018
])