filament-tests icon indicating copy to clipboard operation
filament-tests copied to clipboard

Add check for `->searchable()` for relationships

Open dissto opened this issue 1 year ago • 1 comments

For example

it('can sort column', function (string $column) {
    Transaction::factory(3)->create();

    livewire(ListTransactions::class)
        ->sortTable($column)
        ->assertCanSeeTableRecords(Transaction::orderBy($column)->get(), inOrder: true)
        ->sortTable($column, 'desc')
        ->assertCanSeeTableRecords(Transaction::orderBy($column, 'desc')->get(), inOrder: true);
})->with(['amount', 'enteredBy.name', 'created_at', 'updated_at'])->group('resource', 'page', 'index', 'table', 'column');


// 
public static function getEnteredByColumn(): TextColumn
{
    return TextColumn::make('enteredBy.name')
        ->searchable()
        ->label(__('resources/transaction.table.columns.entered_by.label'))
        ->sortable();
}

//
public function enteredBy(): BelongsTo
{
    return $this->belongsTo(User::class, 'entered_by_id');
}

will blow up with Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such column: enteredBy.name (Connection: sqlite, SQL: select * from "transactions" where "transactions"."deleted_at" is null order by "enteredBy"."name" asc)

dissto avatar May 10 '24 22:05 dissto

I had a problem like this before. When we use relationships, it can cause errors. But if we use eloquent to sort, it might fail because it's sensitive to capitalization. Maybe we should split the tests into ones with relationships and ones without.

CodeWithDennis avatar May 12 '24 11:05 CodeWithDennis