Skip to content

Commit d3d0bab

Browse files
committed
adds tests
Took 21 minutes
1 parent f9102b5 commit d3d0bab

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

tests/Http/Livewire/CatsTable.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
4+
namespace Rappasoft\LaravelLivewireTables\Tests\Http\Livewire;
5+
6+
7+
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Database\Eloquent\Relations\Relation;
9+
use Rappasoft\LaravelLivewireTables\DataTableComponent;
10+
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
11+
use Rappasoft\LaravelLivewireTables\Views\Column;
12+
13+
class CatsTable extends DataTableComponent
14+
{
15+
public function query(): Relation
16+
{
17+
/** @var Species $dogSpecimen */
18+
$dogSpecimen = Species::query()->where('name', 'Cat')->first();
19+
20+
return $dogSpecimen->pets()->with('species')->with('breed');
21+
}
22+
23+
public function columns(): array
24+
{
25+
return [
26+
Column::make('Name', 'name')
27+
->searchable(),
28+
Column::make('Age', 'age')
29+
->searchable(function (Builder $query, $search) {
30+
$query->orWhere('age', '=', $search);
31+
}),
32+
Column::make('Last Visit', 'last_visit')
33+
->searchable(),
34+
Column::make('Species', 'species.name')
35+
->searchable(),
36+
Column::make('Breed', 'breed.name')
37+
->searchable(),
38+
];
39+
}
40+
}

tests/Models/Species.php

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

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\HasMany;
67

78
/**
89
* @property int $id
@@ -38,4 +39,9 @@ class Species extends Model
3839
'id',
3940
'name',
4041
];
42+
43+
public function pets(): HasMany
44+
{
45+
return $this->hasMany(Pet::class);
46+
}
4147
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Tests;
4+
5+
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
6+
use Illuminate\Support\Collection;
7+
use Rappasoft\LaravelLivewireTables\DataTableComponent;
8+
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\CatsTable;
9+
10+
class RelationshipDataTableComponentTest extends TestCase
11+
{
12+
protected DataTableComponent $table;
13+
14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$this->table = new CatsTable();
19+
}
20+
21+
/** @test */
22+
public function bootstrap_test_datatable(): void
23+
{
24+
$this->assertInstanceOf(DataTableComponent::class, $this->table);
25+
}
26+
27+
/** @test */
28+
public function columns(): void
29+
{
30+
$columns = $this->table->columns();
31+
32+
$this->assertCount(5, $columns);
33+
}
34+
35+
/** @test */
36+
public function rows(): void
37+
{
38+
$rows = $this->table->rows;
39+
40+
$this->assertInstanceOf(LengthAwarePaginator::class, $rows);
41+
$this->assertEquals(2, $this->table->getRowsProperty()->total());
42+
}
43+
44+
/** @test */
45+
public function pagination_default(): void
46+
{
47+
$this->assertInstanceOf(LengthAwarePaginator::class, $this->table->rows);
48+
$this->assertEquals(10, $this->table->perPage);
49+
$this->assertTrue($this->table->paginationEnabled);
50+
$this->assertTrue($this->table->showPerPage);
51+
}
52+
53+
/** @test */
54+
public function pagination(): void
55+
{
56+
$this->table->perPage = 1;
57+
$this->assertEquals(1, $this->table->rows->currentPage());
58+
$this->assertEquals(1, $this->table->rows->count());
59+
$this->assertEquals(2, $this->table->rows->lastPage());
60+
}
61+
62+
/** @test */
63+
public function pagination_disabled(): void
64+
{
65+
$this->table->paginationEnabled = false;
66+
$this->table->perPage = 2;
67+
$this->assertInstanceOf(Collection::class, $this->table->rows);
68+
$this->assertCount(2, $this->table->rows);
69+
}
70+
71+
/** @test */
72+
public function search_filter(): void
73+
{
74+
$this->table->filters['search'] = 'Cartman';
75+
$this->assertEquals(1, $this->table->getRowsProperty()->total());
76+
}
77+
78+
/** @test */
79+
public function search_filter_reset(): void
80+
{
81+
$this->table->filters['search'] = 'Cartman';
82+
$this->table->resetFilters();
83+
$this->assertEquals(1, $this->table->rows->total());
84+
}
85+
86+
/** @test */
87+
public function search_filter_remove(): void
88+
{
89+
$this->table->filters['search'] = 'Cartman';
90+
$this->table->removeFilter('search');
91+
$this->assertEquals(2, $this->table->rows->total());
92+
}
93+
94+
/** @test */
95+
public function search_filter_callback(): void
96+
{
97+
$this->table->filters['search'] = '22';
98+
$this->assertEquals(1, $this->table->getRowsProperty()->total());
99+
}
100+
}

0 commit comments

Comments
 (0)