Skip to content

Commit

Permalink
Merge pull request #15310 from chester-sykes/new/add-assertTableFilte…
Browse files Browse the repository at this point in the history
…rHidden-and-assertTableFitlerVisiable

New: Add `assertTableFilterHidden` and `assertTableFilterVisible` test methods
  • Loading branch information
danharrin authored Jan 10, 2025
2 parents 9416993 + 72f1f97 commit 4585d62
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/tables/.stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ public function removeTableFilter(string $filter, ?string $field = null): static

public function removeTableFilters(): static {}

public function assertTableFilterVisible(string $name): static {}

public function assertTableFilterHidden(string $name): static {}

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

public function assertCanSeeTableRecords(array | Collection $records, bool $inOrder = false): static {}
Expand Down
15 changes: 14 additions & 1 deletion packages/tables/docs/12-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,20 @@ it('can remove all table filters', function () {
});
```

### Existence
### Hidden filters

To ensure that a particular user cannot see a filter, you can use the `assertTableFilterVisible()` and `assertTableFilterHidden()` methods:

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

it('shows the correct filters', function () {
livewire(PostsTable::class)
->assertTableFilterVisible('created_at')
->assertTableFilterHidden('author');
```

### Filter existence

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

Expand Down
10 changes: 7 additions & 3 deletions packages/tables/src/Table/Concerns/HasFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,21 @@ public function persistFiltersInSession(bool | Closure $condition = true): stati
/**
* @return array<string, BaseFilter>
*/
public function getFilters(): array
public function getFilters(bool $withHidden = false): array
{
if ($withHidden) {
return $this->filters;
}

return array_filter(
$this->filters,
fn (BaseFilter $filter): bool => $filter->isVisible(),
);
}

public function getFilter(string $name): ?BaseFilter
public function getFilter(string $name, bool $withHidden = false): ?BaseFilter
{
return $this->getFilters()[$name] ?? null;
return $this->getFilters($withHidden)[$name] ?? null;
}

public function getFiltersForm(): Form
Expand Down
42 changes: 42 additions & 0 deletions packages/tables/src/Testing/TestsFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,46 @@ public function assertTableFilterExists(): Closure
return $this;
};
}

public function assertTableFilterVisible(): Closure
{
return function (string $name): static {
$name = $this->instance()->parseTableFilterName($name);

$filter = $this->instance()->getTable()->getFilter(
name: $name,
withHidden: true,
);

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

Assert::assertTrue(
$filter->isVisible(),
message: "Failed asserting that a table filter with name [{$name}] is visible on the [{$livewireClass}] component."
);

return $this;
};
}

public function assertTableFilterHidden(): Closure
{
return function (string $name): static {
$name = $this->instance()->parseTableFilterName($name);

$filter = $this->instance()->getTable()->getFilter(
name: $name,
withHidden: true,
);

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

Assert::assertTrue(
$filter->isHidden(),
message: "Failed asserting that a table filter with name [{$name}] is hidden on the [{$livewireClass}] component."
);

return $this;
};
}
}
10 changes: 10 additions & 0 deletions tests/src/Tables/Filters/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,13 @@
return $filter->getLabel() === 'Is published';
});
});

it('can check if a filter is visible', function (): void {
livewire(PostsTable::class)
->assertTableFilterVisible('is_published');
});

it('can check if a filter is hidden', function (): void {
livewire(PostsTable::class)
->assertTableFilterHidden('hidden_filter');
});
2 changes: 2 additions & 0 deletions tests/src/Tables/Fixtures/PostsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public function table(Table $table): Table
])
->attribute('is_published'),
Tables\Filters\TrashedFilter::make(),
Tables\Filters\SelectFilter::make('hidden_filter')
->hidden(),
])
->persistFiltersInSession()
->headerActions([
Expand Down

0 comments on commit 4585d62

Please sign in to comment.