CodeWithDennis / filament-tests

A package that creates PEST tests specifically tailored for your filament resources.
MIT License
45 stars 4 forks source link

Bug: Columns with enums can't be searched #78

Closed CodeWithDennis closed 2 months ago

CodeWithDennis commented 3 months ago
FAILED Tests\Feature\BlogResourceTest > it can search column with data set "('status')"
Livewire\Features\SupportTesting\Testable::Filament\Tables\Testing\{closure}(): Argument #1 ($search) must be of type ?string, App\Enums\Status given, called in /vendor/laravel/framework/src/Illuminate/Macroable/Traits/Macroable.php on line 123
dissto commented 3 months ago

This same bug appears to be present for sorting columns too

dissto commented 3 months ago

@CodeWithDennis A potential fix would be to check if $search is an enum. And additional to check if it hasLabel(). But there remains a few unknowns. What about non backed enumns, what if you dont use the HasLabel trait? 🤔

it('can search column', function (string $column) {
    $records = Group::factory(3)->for($this->club)->create();

    $search = $records->first()->{$column};

    if ($search instanceof UnitEnum) {

        if (method_exists($search, 'getLabel')) {
            $search = $search->getLabel();
        }
    }

    livewire(ListGroups::class)
        ->searchTable($search)
        ->assertCanSeeTableRecords($records->where($column, $search))
        ->assertCanNotSeeTableRecords($records->where($column, '!=', $search));
})->with(['status'])->group('columns', 'index', 'page', 'resource', 'table');

Anyway, this potentially requires logic inside the stub.

Additional refs: https://stackoverflow.com/questions/70237057/how-to-check-if-enum-type