Skip to content

Commit ac0cbf5

Browse files
committed
add tests for filters
1 parent ae7044f commit ac0cbf5

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

tests/DataTableComponentTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
namespace Rappasoft\LaravelLivewireTables\Tests;
44

55
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
6+
use Illuminate\Encryption\Encrypter;
67
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Facades\Hash;
9+
use Illuminate\View\View;
10+
use Livewire\Livewire;
711
use Rappasoft\LaravelLivewireTables\DataTableComponent;
812
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsAltQueryTable;
913
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
@@ -115,6 +119,42 @@ public function search_filter_alt_query_relation()
115119
$this->assertEquals(2, $this->tableAltQuery->rows->total());
116120
}
117121

122+
/** @test */
123+
public function custom_filter()
124+
{
125+
$this->table->filters['species.name'] = 1;
126+
127+
$this->assertTrue($this->table->getRowsProperty()->where('name', 'Cartman')->isNotEmpty());
128+
$this->assertTrue($this->table->getRowsProperty()->where('name', 'Tux')->isNotEmpty());
129+
$this->assertFalse($this->table->getRowsProperty()->where('May', 'Tux')->isNotEmpty());
130+
$this->assertFalse($this->table->getRowsProperty()->where('Ben', 'Tux')->isNotEmpty());
131+
$this->assertFalse($this->table->getRowsProperty()->where('Chico', 'Tux')->isNotEmpty());
132+
}
133+
134+
/** @test */
135+
public function custom_filters_pills_label_use_column_name_when_possible()
136+
{
137+
config()->set('app.key', Encrypter::generateKey(config('app.cipher')));
138+
139+
Livewire::test(PetsTable::class)
140+
->set('filters', [
141+
'species.name' => 1,
142+
])
143+
->assertSeeTextInOrder(['Applied Filters:', 'Species:', 'Cat', 'Filters']);
144+
}
145+
146+
/** @test */
147+
public function custom_filters_pills_label_use_filter_name_when_is_not_bound_to_a_column()
148+
{
149+
config()->set('app.key', Encrypter::generateKey(config('app.cipher')));
150+
151+
Livewire::test(PetsTable::class)
152+
->set('filters', [
153+
'breed_id' => 1
154+
])
155+
->assertSeeTextInOrder(['Applied Filters:', 'Filter Breed:', 'American Shorthair', 'Filters']);
156+
}
157+
118158
/** @test */
119159
public function bulk_actions_defined_through_function()
120160
{

tests/Http/Livewire/PetsTable.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,30 @@
66
use Rappasoft\LaravelLivewireTables\DataTableComponent;
77
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
88
use Rappasoft\LaravelLivewireTables\Views\Column;
9+
use Rappasoft\LaravelLivewireTables\Views\Filter;
910

1011
class PetsTable extends DataTableComponent
1112
{
13+
public function filters(): array
14+
{
15+
return [
16+
'species.name' => Filter::make('Filter Species')->select([
17+
'' => 'All',
18+
1 => 'Cat',
19+
2 => 'Dog',
20+
3 => 'Horse',
21+
4 => 'Bird',
22+
]),
23+
'breed_id' => Filter::make('Filter Breed')->select([
24+
'' => 'All',
25+
1 => 'American Shorthair',
26+
2 => 'Maine Coon',
27+
3 => 'Persian',
28+
4 => 'Norwegian Forest',
29+
]),
30+
];
31+
}
32+
1233
public function bulkActions(): array
1334
{
1435
return ['count' => 'Count selected'];
@@ -17,11 +38,13 @@ public function bulkActions(): array
1738
/**
1839
* @return Builder
1940
*/
20-
public function query() : Builder
41+
public function query(): Builder
2142
{
2243
return Pet::query()
2344
->with('species')
24-
->with('breed');
45+
->with('breed')
46+
->when($this->getFilter('species.name'), fn (Builder $query, $specimen_id) => $query->where('species_id', $specimen_id))
47+
->when($this->getFilter('breed_id'), fn (Builder $query, $breed_id) => $query->where('breed_id', $breed_id));
2548
}
2649

2750
public function columns(): array

0 commit comments

Comments
 (0)