Skip to content
87 changes: 77 additions & 10 deletions packages/tables/src/Testing/TestsColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Tables\Testing;

use Closure;
use Filament\Support\ArrayRecord;
use Filament\Tables\Columns\Column;
use Filament\Tables\Columns\SelectColumn;
use Filament\Tables\Columns\TextColumn;
Expand Down Expand Up @@ -63,6 +64,22 @@ function ($record) use ($livewire, $livewireId, $name): string {
};
}

protected function assertTableRecordKeyExists(): Closure
{
return function (?string $recordKey): static {
$record = $this->instance()->getTableRecord($recordKey);

$livewireClass = $this->instance()::class;

Assert::assertNotEmpty(
$record,
"Failed asserting that a table row with key {$recordKey} exists on the [{$livewireClass}] component.",
);

return $this;
};
}

public function assertTableColumnExists(): Closure
{
return function (string $name, ?Closure $checkColumnUsing = null, $record = null): static {
Expand Down Expand Up @@ -180,7 +197,12 @@ public function assertTableColumnStateSet(): Closure
$column = $this->instance()->getTable()->getColumn($name);

if (! ($record instanceof Model)) {
/** @phpstan-ignore-next-line */
$this->assertTableRecordKeyExists((string) $record);
$record = $this->instance()->getTableRecord($record);
$recordKey = $record[ArrayRecord::getKeyName()];
} else {
$recordKey = $record->getKey();
}

$column->record($record);
Expand All @@ -204,7 +226,7 @@ public function assertTableColumnStateSet(): Closure
Assert::assertEquals(
$state,
$actualState,
"Failed asserting that a table column with name [{$name}] has value of [{$displayState}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",
"Failed asserting that a table column with name [{$name}] has value of [{$displayState}] for record [{$recordKey}] on the [{$livewireClass}] component.",
);

return $this;
Expand All @@ -220,7 +242,12 @@ public function assertTableColumnStateNotSet(): Closure
$column = $this->instance()->getTable()->getColumn($name);

if (! ($record instanceof Model)) {
/** @phpstan-ignore-next-line */
$this->assertTableRecordKeyExists((string) $record);
$record = $this->instance()->getTableRecord($record);
$recordKey = $record[ArrayRecord::getKeyName()];
} else {
$recordKey = $record->getKey();
}

$column->record($record);
Expand All @@ -244,7 +271,7 @@ public function assertTableColumnStateNotSet(): Closure
Assert::assertNotEquals(
$state,
$actualState,
"Failed asserting that a table column with name [{$name}] does not have a value of [{$displayState}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",
"Failed asserting that a table column with name [{$name}] does not have a value of [{$displayState}] for record [{$recordKey}] on the [{$livewireClass}] component.",
);

return $this;
Expand All @@ -261,7 +288,12 @@ public function assertTableColumnFormattedStateSet(): Closure
$column = $this->instance()->getTable()->getColumn($name);

if (! ($record instanceof Model)) {
/** @phpstan-ignore-next-line */
$this->assertTableRecordKeyExists((string) $record);
$record = $this->instance()->getTableRecord($record);
$recordKey = $record[ArrayRecord::getKeyName()];
} else {
$recordKey = $record->getKey();
}

$column->record($record);
Expand All @@ -273,7 +305,7 @@ public function assertTableColumnFormattedStateSet(): Closure
Assert::assertEquals(
$state,
$column->formatState($column->getState()),
"Failed asserting that a table column with name [{$name}] has a formatted state of [{$state}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",
"Failed asserting that a table column with name [{$name}] has a formatted state of [{$state}] for record [{$recordKey}] on the [{$livewireClass}] component.",
);

return $this;
Expand All @@ -290,7 +322,12 @@ public function assertTableColumnFormattedStateNotSet(): Closure
$column = $this->instance()->getTable()->getColumn($name);

if (! ($record instanceof Model)) {
/** @phpstan-ignore-next-line */
$this->assertTableRecordKeyExists((string) $record);
$record = $this->instance()->getTableRecord($record);
$recordKey = $record[ArrayRecord::getKeyName()];
} else {
$recordKey = $record->getKey();
}

$column->record($record);
Expand All @@ -302,7 +339,7 @@ public function assertTableColumnFormattedStateNotSet(): Closure
Assert::assertNotEquals(
$state,
$column->formatState($column->getState()),
"Failed asserting that a table column with name [{$name}] does not have a formatted state of [{$state}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",
"Failed asserting that a table column with name [{$name}] does not have a formatted state of [{$state}] for record [{$recordKey}] on the [{$livewireClass}] component.",
);

return $this;
Expand All @@ -318,7 +355,12 @@ public function assertTableColumnHasExtraAttributes(): Closure
$column = $this->instance()->getTable()->getColumn($name);

if (! ($record instanceof Model)) {
/** @phpstan-ignore-next-line */
$this->assertTableRecordKeyExists((string) $record);
$record = $this->instance()->getTableRecord($record);
$recordKey = $record[ArrayRecord::getKeyName()];
} else {
$recordKey = $record->getKey();
}

$column->record($record);
Expand All @@ -330,7 +372,7 @@ public function assertTableColumnHasExtraAttributes(): Closure
Assert::assertEquals(
$attributes,
$column->getExtraAttributes(),
"Failed asserting that a table column with name [{$name}] has extra attributes [{$attributesString}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",
"Failed asserting that a table column with name [{$name}] has extra attributes [{$attributesString}] for record [{$recordKey}] on the [{$livewireClass}] component.",
);

return $this;
Expand All @@ -346,7 +388,12 @@ public function assertTableColumnDoesNotHaveExtraAttributes(): Closure
$column = $this->instance()->getTable()->getColumn($name);

if (! ($record instanceof Model)) {
/** @phpstan-ignore-next-line */
$this->assertTableRecordKeyExists((string) $record);
$record = $this->instance()->getTableRecord($record);
$recordKey = $record[ArrayRecord::getKeyName()];
} else {
$recordKey = $record->getKey();
}

$column->record($record);
Expand All @@ -358,7 +405,7 @@ public function assertTableColumnDoesNotHaveExtraAttributes(): Closure
Assert::assertNotEquals(
$attributes,
$column->getExtraAttributes(),
"Failed asserting that a table column with name [{$name}] does not have extra attributes [{$attributesString}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",
"Failed asserting that a table column with name [{$name}] does not have extra attributes [{$attributesString}] for record [{$recordKey}] on the [{$livewireClass}] component.",
);

return $this;
Expand All @@ -375,7 +422,12 @@ public function assertTableColumnHasDescription(): Closure
$column = $this->instance()->getTable()->getColumn($name);

if (! ($record instanceof Model)) {
/** @phpstan-ignore-next-line */
$this->assertTableRecordKeyExists((string) $record);
$record = $this->instance()->getTableRecord($record);
$recordKey = $record[ArrayRecord::getKeyName()];
} else {
$recordKey = $record->getKey();
}

$column->record($record);
Expand All @@ -387,7 +439,7 @@ public function assertTableColumnHasDescription(): Closure
Assert::assertEquals(
$description,
$actualDescription,
"Failed asserting that a table column with name [{$name}] has description [{$description}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",
"Failed asserting that a table column with name [{$name}] has description [{$description}] for record [{$recordKey}] on the [{$livewireClass}] component.",
);

return $this;
Expand All @@ -404,7 +456,12 @@ public function assertTableColumnDoesNotHaveDescription(): Closure
$column = $this->instance()->getTable()->getColumn($name);

if (! ($record instanceof Model)) {
/** @phpstan-ignore-next-line */
$this->assertTableRecordKeyExists((string) $record);
$record = $this->instance()->getTableRecord($record);
$recordKey = $record[ArrayRecord::getKeyName()];
} else {
$recordKey = $record->getKey();
}

$column->record($record);
Expand All @@ -416,7 +473,7 @@ public function assertTableColumnDoesNotHaveDescription(): Closure
Assert::assertNotEquals(
$description,
$actualDescription,
"Failed asserting that a table column with name [{$name}] does not have description [{$description}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",
"Failed asserting that a table column with name [{$name}] does not have description [{$description}] for record [{$recordKey}] on the [{$livewireClass}] component.",
);

return $this;
Expand All @@ -433,7 +490,12 @@ public function assertTableSelectColumnHasOptions(): Closure
$column = $this->instance()->getTable()->getColumn($name);

if (! ($record instanceof Model)) {
/** @phpstan-ignore-next-line */
$this->assertTableRecordKeyExists((string) $record);
$record = $this->instance()->getTableRecord($record);
$recordKey = $record[ArrayRecord::getKeyName()];
} else {
$recordKey = $record->getKey();
}

$column->record($record);
Expand All @@ -445,7 +507,7 @@ public function assertTableSelectColumnHasOptions(): Closure
Assert::assertEquals(
$options,
$column->getOptions(),
"Failed asserting that a table column with name [{$name}] has options [{$optionsString}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",
"Failed asserting that a table column with name [{$name}] has options [{$optionsString}] for record [{$recordKey}] on the [{$livewireClass}] component.",
);

return $this;
Expand All @@ -462,7 +524,12 @@ public function assertTableSelectColumnDoesNotHaveOptions(): Closure
$column = $this->instance()->getTable()->getColumn($name);

if (! ($record instanceof Model)) {
/** @phpstan-ignore-next-line */
$this->assertTableRecordKeyExists((string) $record);
$record = $this->instance()->getTableRecord($record);
$recordKey = $record[ArrayRecord::getKeyName()];
} else {
$recordKey = $record->getKey();
}

$column->record($record);
Expand All @@ -474,7 +541,7 @@ public function assertTableSelectColumnDoesNotHaveOptions(): Closure
Assert::assertNotEquals(
$options,
$column->getOptions(),
"Failed asserting that a table column with name [{$name}] does not have options [{$optionsString}] for record [{$record->getKey()}] on the [{$livewireClass}] component.",
"Failed asserting that a table column with name [{$name}] does not have options [{$optionsString}] for record [{$recordKey}] on the [{$livewireClass}] component.",
);

return $this;
Expand Down
69 changes: 69 additions & 0 deletions tests/src/Fixtures/Livewire/CustomDataTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Filament\Tests\Fixtures\Livewire;

use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Contracts\HasSchemas;
use Filament\Tables;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Contracts\View\View;
use Livewire\Component;

class CustomDataTable extends Component implements HasActions, HasSchemas, Tables\Contracts\HasTable
{
use InteractsWithActions;
use InteractsWithSchemas;
use Tables\Concerns\InteractsWithTable;

public function table(Table $table): Table
{
return $table
->records(fn (): array => [
1 => [
'title' => 'First item',
'slug' => 'first-item',
'is_featured' => true,
],
2 => [
'title' => 'Second item',
'slug' => 'second-item',
'is_featured' => false,
],
3 => [
'title' => 'Third item',
'slug' => 'third-item',
'is_featured' => true,
],
])
->columns([
TextColumn::make('title'),
TextColumn::make('slug'),
IconColumn::make('is_featured')
->boolean(),
TextColumn::make('formatted_state')
->formatStateUsing(fn () => 'formatted state'),
TextColumn::make('extra_attributes')
->extraAttributes([
'class' => 'text-danger-500',
]),
TextColumn::make('with_description')
->description('description below')
->description('description above', 'above'),

Tables\Columns\SelectColumn::make('with_options')
->options([
'red' => 'Red',
'blue' => 'Blue',
]),
]);
}

public function render(): View
{
return view('livewire.table');
}
}
Loading
Loading