Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/10-testing/03-testing-tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,20 @@ it('displays author in red', function () {
});
```

When using image columns, the extra image attributes can also be tested to ensure that a column has the correct extra img attributes, you can use the `assertTableColumnHasExtraImgAttributes()` and `assertTableColumnDoesNotHaveExtraImgAttributes()` methods:

```php
use function Pest\Livewire\livewire;

it('displays author in red', function () {
$post = Post::factory()->create();

livewire(PostsTable::class)
->assertTableColumnHasExtraImgAttributes('author.image.url', ['class' => 'border border-emerald-500'], $post)
->assertTableColumnDoesNotHaveExtraImgAttributes('author.image.url', ['class' => 'border border-red-500'], $post);
});
```

### Testing the options in a `SelectColumn`

If you have a select column, you can ensure it has the correct options with `assertTableSelectColumnHasOptions()` and `assertTableSelectColumnDoesNotHaveOptions()`:
Expand Down
4 changes: 4 additions & 0 deletions packages/tables/.stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function assertTableColumnHasExtraAttributes(string $name, array $attribu

public function assertTableColumnDoesNotHaveExtraAttributes(string $name, array $attributes, $record): static {}

public function assertTableColumnHasExtraImgAttributes(string $name, array $attributes, $record): static {}

public function assertTableColumnDoesNotHaveExtraImgAttributes(string $name, array $attributes, $record): static {}

public function assertTableColumnHasDescription(string $name, $description, $record, $position = 'below'): static {}

public function assertTableColumnDoesNotHaveDescription(string $name, $description, $record, $position = 'below'): static {}
Expand Down
76 changes: 76 additions & 0 deletions packages/tables/src/Testing/TestsColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Filament\Tables\Testing;

use Closure;
use Filament\Support\ArrayRecord;
use Filament\Tables\Columns\Column;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\SelectColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Contracts\HasTable;
Expand Down Expand Up @@ -365,6 +367,80 @@ public function assertTableColumnDoesNotHaveExtraAttributes(): Closure
};
}

public function assertTableColumnHasExtraImgAttributes(): Closure
{
return function (string $name, array $attributes, $record) {
/** @phpstan-ignore-next-line */
$this->assertTableColumnExists($name);

$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);

$attributesString = print_r($attributes, true);

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

if (! $column instanceof ImageColumn) {
Assert::fail('Extra Image attributes can only be asserted on columns of type ' . ImageColumn::class);
}

Assert::assertEquals(
$attributes,
$column->getExtraImgAttributes(),
"Failed asserting that a table column with name [{$name}] has extra img attributes [{$attributesString}] for record [{$recordKey}] on the [{$livewireClass}] component.",
);

return $this;
};
}

public function assertTableColumnDoesNotHaveExtraImgAttributes(): Closure
{
return function (string $name, array $attributes, $record) {
/** @phpstan-ignore-next-line */
$this->assertTableColumnExists($name);

$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);

$attributesString = print_r($attributes, true);

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

if (! $column instanceof ImageColumn) {
Assert::fail('Extra Image attributes can only be asserted on columns of type ' . ImageColumn::class);
}

Assert::assertNotEquals(
$attributes,
$column->getExtraImgAttributes(),
"Failed asserting that a table column with name [{$name}] does not have extra img attributes [{$attributesString}] for record [{$recordKey}] on the [{$livewireClass}] component.",
);

return $this;
};
}

public function assertTableColumnHasDescription(): Closure
{
return function (string $name, $description, $record, string $position = 'below') {
Expand Down
4 changes: 4 additions & 0 deletions tests/src/Fixtures/Livewire/UsersTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public function table(Table $table): Table
->label('Language')
->sortable()
->searchable(),
Tables\Columns\ImageColumn::make('image')
->state(fn (User $record) => $record->image?->url)
->extraImgAttributes(['class' => 'border border-emerald-500'])
->label('Image'),
Tables\Columns\TextColumn::make('image.url')
->label('Image URL')
->sortable()
Expand Down
28 changes: 28 additions & 0 deletions tests/src/Tables/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,34 @@
->assertTableColumnDoesNotHaveExtraAttributes('extra_attributes', ['class' => 'text-primary-500'], $post);
});

it('can state whether a image column has extra image attributes', function (): void {
$post = Post::factory()->create();

livewire(UsersTable::class)
->assertTableColumnHasExtraImgAttributes('image', ['class' => 'border border-emerald-500'], $post)
->assertTableColumnDoesNotHaveExtraImgAttributes('image', ['class' => 'border border-red-500'], $post);
});

it('cannot assert for extra image attributes on non image column', function (): void {
$post = Post::factory()->create();

$this->expectException('PHPUnit\Framework\AssertionFailedError');
$this->expectExceptionMessage('Extra Image attributes can only be asserted on columns of type Filament\Tables\Columns\ImageColumn');

livewire(UsersTable::class)
->assertTableColumnHasExtraImgAttributes('image.url', ['class' => 'border border-emerald-500'], $post);
});

it('cannot assert for not having extra image attributes on non image column', function (): void {
$post = Post::factory()->create();

$this->expectException('PHPUnit\Framework\AssertionFailedError');
$this->expectExceptionMessage('Extra Image attributes can only be asserted on columns of type Filament\Tables\Columns\ImageColumn');

livewire(UsersTable::class)
->assertTableColumnDoesNotHaveExtraImgAttributes('image.url', ['class' => 'border border-emerald-500'], $post);
});

it('can state whether a column has a description', function (): void {
$post = Post::factory()->create();

Expand Down