F
Filamentβ€’4w ago
jlove1672

assertCanSeeTableRecords fails on tables with Custom data

Having issues using the assertCanSeeTableRecords when writing tests that use custom data Error: ErrorException: Array to string conversion Location Filament\Tables\Testing\TestsRecords line 29
Solution:
Yeah so i key valued the arrays in the static data and checked against those keys and it worked so ```...
Jump to solution
6 Replies
Dennis Koch
Dennis Kochβ€’4w ago
Maybe share some code.
jlove1672
jlove1672OPβ€’4w ago
public function table(Table $table): Table
{
$data = [
[
'first_name' => 'Foo',
'last_name' => 'Bar',
],
[
'first_name' => 'Hello',
'last_name' => 'World',
],
];

return $table
->records(fn(): array => $data)
->columns([
TextColumn::make('first_name'),
TextColumn::make('last_name'),
]);
}
public function table(Table $table): Table
{
$data = [
[
'first_name' => 'Foo',
'last_name' => 'Bar',
],
[
'first_name' => 'Hello',
'last_name' => 'World',
],
];

return $table
->records(fn(): array => $data)
->columns([
TextColumn::make('first_name'),
TextColumn::make('last_name'),
]);
}
Test
it('can render table records', function () {

$data = [
[
'first_name' => 'Foo',
'last_name' => 'Bar',
],
[
'first_name' => 'Hello',
'last_name' => 'World',
],
];

Livewire::test(Dashboard::class)
->assertCanSeeTableRecords($data)
->assertSuccessful();
});
it('can render table records', function () {

$data = [
[
'first_name' => 'Foo',
'last_name' => 'Bar',
],
[
'first_name' => 'Hello',
'last_name' => 'World',
],
];

Livewire::test(Dashboard::class)
->assertCanSeeTableRecords($data)
->assertSuccessful();
});
Error - ErrorException: Array to string conversion Fails at Location Filament\Tables\Testing\TestsRecords line 29
Dennis Koch
Dennis Kochβ€’4w ago
I don't know how exactly assertCanSeeTableRecords() works with static data. But with non-static data you pass a list of record ids. Not the full data.
Solution
jlove1672
jlove1672β€’4w ago
Yeah so i key valued the arrays in the static data and checked against those keys and it worked so
$data = [
'key1' => [
'first_name' => 'Foo',
'last_name' => 'Bar',
],
'key2' => [
'first_name' => 'Hello',
'last_name' => 'World',
],
];
$data = [
'key1' => [
'first_name' => 'Foo',
'last_name' => 'Bar',
],
'key2' => [
'first_name' => 'Hello',
'last_name' => 'World',
],
];
Livewire::test(Dashboard::class)
->assertCanSeeTableRecords(['key1', 'key2'])
->assertSuccessful();
Livewire::test(Dashboard::class)
->assertCanSeeTableRecords(['key1', 'key2'])
->assertSuccessful();
Works πŸ‘
jlove1672
jlove1672OPβ€’4w ago
Maybe it was just my misunderstanding of how this works but is it worth addding this to the docs somewhere? Like under the https://filamentphp.com/docs/4.x/tables/custom-data section?

Did you find this page helpful?