Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: assertTableFilterExists to allow passing in a callback #15302

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
'role' => 'tab',
])
->class([
'fi-tabs-item group flex items-center justify-center gap-x-2 rounded-lg px-3 py-2 text-sm font-medium outline-none whitespace-nowrap transition duration-75',
'fi-tabs-item group flex items-center justify-center gap-x-2 whitespace-nowrap rounded-lg px-3 py-2 text-sm font-medium outline-none transition duration-75',
$inactiveItemClasses => (! $hasAlpineActiveClasses) && (! $active),
$activeItemClasses => (! $hasAlpineActiveClasses) && $active,
])
Expand Down
2 changes: 2 additions & 0 deletions packages/tables/.stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public function removeTableFilter(string $filter, ?string $field = null): static

public function removeTableFilters(): static {}

public function assertTableFilterExists(string $name, ?Closure $checkFilterUsing = null): static {}

public function assertCanSeeTableRecords(array | Collection $records, bool $inOrder = false): static {}

public function assertCanNotSeeTableRecords(array | Collection $records): static {}
Expand Down
27 changes: 27 additions & 0 deletions packages/tables/docs/12-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,33 @@ it('can remove all table filters', function () {
});
```

### Existence

To ensure that a filter exists, you can use the `assertTableFilterExists()` method:

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

it('has an author filter', function () {
livewire(PostResource\Pages\ListPosts::class)
->assertTableFilterExists('author');
});
```

You may pass a function as an additional argument in order to assert that a filter passes a given "truth test". This is useful for asserting that a filter has a specific configuration:

```php
use function Pest\Livewire\livewire;
use Filament\Tables\Filters\SelectFilter;

it('has an author filter', function () {
livewire(PostResource\Pages\ListPosts::class)
->assertTableFilterExists('author', function (SelectFilter $column): bool {
return $column->getLabel() === 'Select author';
});
});
```

## Actions

### Calling actions
Expand Down
9 changes: 8 additions & 1 deletion packages/tables/src/Testing/TestsFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function removeTableFilters(): Closure

public function assertTableFilterExists(): Closure
{
return function (string $name): static {
return function (string $name, ?Closure $checkFilterUsing = null): static {
$name = $this->instance()->parseTableFilterName($name);

$filter = $this->instance()->getTable()->getFilter($name);
Expand All @@ -96,6 +96,13 @@ public function assertTableFilterExists(): Closure
message: "Failed asserting that a table filter with name [{$name}] exists on the [{$livewireClass}] component.",
);

if ($checkFilterUsing) {
Assert::assertTrue(
$checkFilterUsing($filter),
"Failed asserting that a table filter with name [{$name}] and provided configuration exists on the [{$livewireClass}] component.",
);
}

return $this;
};
}
Expand Down
8 changes: 8 additions & 0 deletions tests/src/Tables/Filters/FilterTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Filament\Tables\Filters\Filter;
use Filament\Tests\Models\Post;
use Filament\Tests\Tables\Fixtures\PostsTable;
use Filament\Tests\Tables\TestCase;
Expand Down Expand Up @@ -101,3 +102,10 @@
->filterTable('select_filter_attribute', true)
->assertCanNotSeeTableRecords($unpublishedPosts);
});

it('can assert a filter exists with a given configuration', function () {
livewire(PostsTable::class)
->assertTableFilterExists('is_published', function (Filter $filter): bool {
return $filter->getLabel() === 'Is published';
});
});
Loading