How to test Counting relationships

I'm counting the number of related records in a text column based on the documentation and everything works fine.
Tables\Columns\TextColumn::make('messages_count')
->counts('messages'),
Tables\Columns\TextColumn::make('messages_count')
->counts('messages'),
How can I test the relationship count with Pest? I tried to test it by chaining assertTableColumnStateSet method to the table component, but no luck so far. Please tell me if I should provide any further details. The error: Failed asserting that a table column with name [messages_count] has value of [1] for record [1] on the [App\Filament\Resources\TelegramUserResource\Pages\ListTelegramUsers] component. The test:
it('can show the messages count in the table.', function () {
$message = Message::factory()
->has(TelegramUser::factory(), 'telegram_user')
->create();

$telegram_user = $message->telegram_user;

livewire(TelegramUserResource\Pages\ListTelegramUsers::class)
->assertTableColumnStateSet(
name: 'messages_count',
value: 1,
record: $telegram_user,
);
});
it('can show the messages count in the table.', function () {
$message = Message::factory()
->has(TelegramUser::factory(), 'telegram_user')
->create();

$telegram_user = $message->telegram_user;

livewire(TelegramUserResource\Pages\ListTelegramUsers::class)
->assertTableColumnStateSet(
name: 'messages_count',
value: 1,
record: $telegram_user,
);
});
Solution:
Not sure, but try using the record ID instead:
record: $telegram_user->id,
record: $telegram_user->id,
...
Jump to solution
4 Replies
Patrick Boivin
Patrick Boivin15mo ago
Just out of curiosity, are you sure you need this test? Maybe I'm missing something but it seems like this is just testing that the ->counts() method works. Filament likely has a test for that internally.
fsamapoor
fsamapoorOP15mo ago
I just wanted to make sure. Now that you mentioned it, I should probably focus on testing the relationship itself, rather than testing the Filament which we already know works fine. Nevertheless, do you think it is possible to test it?
Solution
Patrick Boivin
Patrick Boivin15mo ago
Not sure, but try using the record ID instead:
record: $telegram_user->id,
record: $telegram_user->id,
fsamapoor
fsamapoorOP15mo ago
It works! Not what I expected, but gonna close this as resolved anyway. Thank you very much, Patrick!
Want results from more Discord servers?
Add your server