Skip to content
Merged
10 changes: 10 additions & 0 deletions resources/views/resources/pages/index/can-sort-column.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
it('can sort `:dataset` column', function (string $column): void {
$records = {{ $getResourceModel() }}::factory(3)->create();

livewire({{ $getPageClass('index') }}::class)
@if($isResourceTableLoadingGloballyDeferred())->loadTable()@endif
->sortTable($column)
->assertCanSeeTableRecords($records->sortBy($column), inOrder: true)
->sortTable($column, 'desc')
->assertCanSeeTableRecords($records->sortByDesc($column), inOrder: true);
})->with([@foreach ($getResourceTableSortableColumnKeys() as $column)'{{ $column }}',@endforeach]);
10 changes: 0 additions & 10 deletions resources/views/table-column-sorting.blade.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/Commands/FilamentTestsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\CanNotRenderColumnTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\CanRenderColumnTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\CanRenderIndexPageTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\CanSortColumnTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\HasColumnTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\HidesColumnTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\ShowsColumnTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\View\CanRenderViewPageTest;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class FilamentTestsCommand extends Command
{
protected Collection $panels;

protected Collection $resources;

protected bool $tableLoadingGloballyDeferred = false;

use InteractsWithFilesystem;
use InteractsWithUserInput;

Expand All @@ -32,6 +40,7 @@ public function handle(): void
{
$this->panels = $this->askUserToSelectPanels();
$this->resources = $this->askUserToSelectResourcesFromTheSelectedPanels();
$this->tableLoadingGloballyDeferred = $this->askUserIfTableLoadingIsGloballyDeferred();

$this->generateTests();
$this->showGenerationSummary();
Expand All @@ -54,6 +63,7 @@ protected function getRenderers(): array
HasColumnTest::class,
ShowsColumnTest::class,
HidesColumnTest::class,
CanSortColumnTest::class,
];
}
}
2 changes: 1 addition & 1 deletion src/Concerns/Commands/InteractsWithFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function renderTestsForResource(string $resource): array
$output = $renderers
->map(fn (string $renderer) =>
/** @var BaseTest $renderer */
$renderer::build($resource)->render())
$renderer::build($resource)->tableLoadingGloballyDeferred($this->tableLoadingGloballyDeferred)->render())
->prepend('<?php')
->implode("\n\n");

Expand Down
18 changes: 14 additions & 4 deletions src/Concerns/Commands/InteractsWithUserInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
use Illuminate\Support\Collection;

use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\select;

trait InteractsWithUserInput
{
protected Collection $panels;

protected Collection $resources;

protected function getSelectedPanels(): Collection
{
return $this->panels ??= collect();
Expand Down Expand Up @@ -68,4 +65,17 @@ protected function askUserToSelectResourcesFromTheSelectedPanels(): Collection

return $selectedResources;
}

protected function askUserIfTableLoadingIsGloballyDeferred(): bool
{
return (bool) select(
label: 'Do you globally defer table loading in your Filament app?',
options: [
1 => 'Yes',
0 => 'No',
],
hint: 'If you set `deferLoading` individually on your resource tables, you can select "No" here.',
required: true,
);
}
}
10 changes: 10 additions & 0 deletions src/Concerns/Resources/InteractsWithTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public function getResourceTableSortableColumns(): Collection
->filter(fn (Column $column): bool => $column->isSortable());
}

public function getResourceTableSortableColumnKeys(): array
{
return $this->getResourceTableColumnKeysFrom($this->getResourceTableSortableColumns());
}

public function getResourceTableActions(): array
{
return $this->getResourceTable()->getRecordActions();
Expand All @@ -95,4 +100,9 @@ public function getResourceTableVisibleActions(): array
{
return array_filter($this->getResourceTableActions(), fn (Action $action): bool => ! $this->getPrivateProperty($action, 'isHidden'));
}

public function isResourceTableLoadingGloballyDeferred(): bool
{
return $this->isTableLoadingGlobalyDeferred() ?: $this->getResourceTable()->isLoadingDeferred();
}
}
14 changes: 14 additions & 0 deletions src/TestRenderers/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,29 @@ abstract class BaseTest implements HasFilamentResources
use ExposesPublicMethodsToViews;
use InteractsWithResources;

public bool $tableLoadingGloballyDeferred = false;

public function __construct(
public ?string $resourceClass = null,
) {}

public function tableLoadingGloballyDeferred(bool $tableLoadingGloballyDeferred): static
{
$this->tableLoadingGloballyDeferred = $tableLoadingGloballyDeferred;

return $this;
}

public static function build(string $resourceClass): static
{
return new static($resourceClass);
}

public function isTableLoadingGlobalyDeferred(): bool
{
return $this->tableLoadingGloballyDeferred;
}

public function getResourceClass(): ?string
{
return $this->resourceClass;
Expand Down
16 changes: 16 additions & 0 deletions src/TestRenderers/Resources/Pages/Index/CanSortColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index;

use CodeWithDennis\FilamentTests\TestRenderers\BaseTest;

class CanSortColumnTest extends BaseTest
{
public ?string $view = 'filament-tests::resources.pages.index.can-sort-column';

public function getShouldRender(): bool
{
return $this->hasPage('index')
&& $this->getResourceTableSortableColumns()->isNotEmpty();
}
}