Skip to content

Commit 8dc509c

Browse files
authored
Add initial tests for LivewireComponentArrayFilter (rappasoft#2170)
* Add initial tests for LivewireComponentArrayFilter * Fix styling * Add LivewireComponentArrayFilter isEmpty check * Fix styling --------- Co-authored-by: lrljoe <[email protected]>
1 parent 19b3723 commit 8dc509c

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/Views/Filters/LivewireComponentArrayFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function validate(array $value): array|bool
2525

2626
public function isEmpty(array $value = []): bool
2727
{
28-
return empty($value);
28+
return empty($value) || (count($value) == 1 && (is_null($value[0]) || $value[0] == ''));
2929
}
3030

3131
/**
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Tests\Unit\Views\Filters;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use PHPUnit\Framework\Attributes\Group;
7+
use Rappasoft\LaravelLivewireTables\Views\Filters\LivewireComponentArrayFilter;
8+
9+
#[Group('Filters')]
10+
final class LivewireComponentArrayFilterTest extends FilterTestCase
11+
{
12+
protected function setUp(): void
13+
{
14+
parent::setUp();
15+
self::$filterInstance = LivewireComponentArrayFilter::make('Active');
16+
}
17+
18+
public function test_can_get_filter_callback(): void
19+
{
20+
$this->assertFalse(self::$filterInstance->hasFilterCallback());
21+
22+
self::$filterInstance
23+
->filter(function (Builder $builder, array $values) {
24+
return $builder->whereIn('name', $values);
25+
});
26+
27+
$this->assertTrue(self::$filterInstance->hasFilterCallback());
28+
$this->assertIsCallable(self::$filterInstance->getFilterCallback());
29+
}
30+
31+
public function test_can_set_livewire_component_filter_to_text(): void
32+
{
33+
$this->assertSame(['test'], self::$filterInstance->validate(['test']));
34+
$this->assertSame([123], self::$filterInstance->validate([123]));
35+
36+
}
37+
38+
public function test_can_get_if_livewire_component_filter_empty(): void
39+
{
40+
$this->assertTrue(self::$filterInstance->isEmpty());
41+
$this->assertTrue(self::$filterInstance->isEmpty([]));
42+
$this->assertTrue(self::$filterInstance->isEmpty(['']));
43+
$this->assertFalse(self::$filterInstance->isEmpty(['123']));
44+
$this->assertFalse(self::$filterInstance->isEmpty(['test']));
45+
$this->assertFalse(self::$filterInstance->isEmpty([1234]));
46+
47+
}
48+
49+
public function test_can_get_filter_default_value(): void
50+
{
51+
$this->assertSame([], self::$filterInstance->getDefaultValue());
52+
}
53+
}

0 commit comments

Comments
 (0)