Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Tiskiel's avatar

How test a sortable column based to an enum ?

Hi everyone ,

I have this TextColumn in my table

Tables\Columns\TextColumn::make('category')
                    ->label(__('rates.category'))
                    ->formatStateUsing(fn(CategoryEnum $state): string => $state->getLabel())
                    ->sortable()
                    ->searchable(),

I want to assert with Pest that I can see the records, but it doesn't work. The other tests work. I think my test doesn't work because I use an enum.

it('can sort rates by category', function () {
    $livewire = livewire(ManageRates::class)
        ->assertCanSeeTableRecords($this->rates)
        ->assertTableColumnFormattedStateSet('category', $this->rate->category->getLabel(), record: $this->rate)
        ->assertTableColumnFormattedStateNotSet('category', $this->rate->category->value, record: $this->rate);

    // $this->rates->map(function ($rate) {
    //     $rate->category = $rate->category->getLabel();
    //     return $rate;
    // });

    $livewire
        ->sortTable('category')
        ->assertCanSeeTableRecords($this->rates->sortBy('category'), inOrder: true);
})->only();

First part of my test work but not the second.

Any one have an idea ?

Thank you in advance

0 likes
4 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

The error should output the full html for you to compare. Are you able to spot a difference in it ?

Btw. It could be because it isn't deterministic. If multiple rows have the same column, you cannot be 100% sure that the order of those always are the same. So you need to sort by a secondary column also (like id). Also there is a chance that sortBy() and orderBy() does not return the same if category is a string

1 like
Tiskiel's avatar

@Sinnbeck following your advice, I've modified the columns in my table

Like this :

Tables\Columns\TextColumn::make('type')
                    ->label(__('rates.type'))
                    ->formatStateUsing(fn(TypeEnum $state): string => $state->getLabel())
                    ->sortable(['id', 'type'])
                    ->searchable(),
                Tables\Columns\TextColumn::make('category')
                    ->label(__('rates.category'))
                    ->formatStateUsing(fn(CategoryEnum $state): string => $state->getLabel())
                    ->sortable(['id', 'category'])
                    ->searchable(),

But my test wipe again

it('can sort rates by category', function () {
    livewire(ManageRates::class)
        ->assertCanSeeTableRecords($this->rates)
        ->assertTableColumnFormattedStateSet('category', $this->rate->category->getLabel(), record: $this->rate)
        ->assertTableColumnFormattedStateNotSet('category', $this->rate->category->value, record: $this->rate)
        ->sortTable('category')
        ->assertCanSeeTableRecords($this->rates->sortBy([['id', 'asc'],['category', 'asc']]), inOrder: true)
        ->sortTable('category', 'desc')
        ->assertCanSeeTableRecords($this->rates->sortByDesc([['id', 'desc'],['category', 'desc']]), inOrder: true);
})->only();

Did you see what I'm doing wrong?

Tiskiel's avatar

Thank you both,

Here my working solution

Tables\Columns\TextColumn::make('category')
                    ->label(__('rates.category'))
                    ->formatStateUsing(fn(CategoryEnum $state): string => $state->getLabel())
                    ->sortable(['category', 'id'])
                    ->searchable(),

it('can sort rates by category', function () {
    livewire(ManageRates::class)
        ->sortTable('category')
        ->assertCanSeeTableRecords($this->rates->sortBy([['id', 'asc'], ['category', 'asc']]), inOrder: true)
        ->sortTable('category', 'desc')
        ->assertCanSeeTableRecords($this->rates->sortByDesc([['id', 'desc'], ['category', 'desc']]), inOrder: true);
});

Please or to participate in this conversation.