Skip to content
Draft
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ This package generates comprehensive PEST tests for your Filament resources. Her
- **ShowsColumnTest** - Tests that explicitly visible columns are shown
- **HidesColumnTest** - Tests that explicitly hidden columns are hidden
- **CanNotDisplayTrashedRecordsByDefault** - Tests that trashed records are not displayed by default if soft deletes are enabled
- **ColumnHasCorrectStateTest** - Tests that columns has the correct state
- **ColumnHasCorrectFormattedStateTest** - Tests that columns has the correct formatted state (additional user input required)

### Table Functionality Tests
- **CanSearchColumnTest** - Tests that searchable columns work correctly
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
it('`:dataset` column has the correct formatted state', function (string $column): void {
$records = {{ $getResourceModel() }}::factory(3)->create();

$record = $records->first();

$value = data_get($record, $column);

// if ($value instanceof Carbon\Carbon || $value instanceof Carbon\CarbonImmutable ) {
// $value = $value->format('Y-m-d H:i:s');
// }

livewire({{ $getPageClass('index') }}::class)
->assertTableColumnFormattedStateSet($column, $value, record: $record)
->assertTableColumnFormattedStateNotSet($column, 'non-existent-value', record: $record);
{{-- TODO: support all column types --}}
})->with([@foreach ($getResourceTableTextColumnKeys() as $column)'{{ $column }}',@endforeach])
@if(count($diff = array_diff($getResourceTableVisibleColumnKeys(), $getResourceTableTextColumnKeys())) > 0)
->todo('The following columns were skipped during generation as their state could not be determined automatically: {{ implode(", ", $diff) }}')
@endif
->skip('This test requires additional attention because the formatted state is likely to differ from the raw state.');
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
it('`:dataset` column has the correct state', function (string $column): void {
$records = {{ $getResourceModel() }}::factory(3)->create();

$record = $records->first();

$value = data_get($record, $column);

if ($value instanceof Carbon\Carbon || $value instanceof Carbon\CarbonImmutable ) {
$value = $value->toDateTimeString();
}

livewire({{ $getPageClass('index') }}::class)
->assertTableColumnStateSet($column, $value, record: $record)
->assertTableColumnStateNotSet($column, 'non-existent-value', record: $record);
})@if(count($diff = array_diff($getResourceTableVisibleColumnKeys(), $getResourceTableTextColumnKeys())) > 0)
->todo('The following columns were skipped during generation as their state could not be determined automatically: {{ implode(", ", $diff) }}')
@endif
{{-- TODO: support all column types --}}
->with([@foreach ($getResourceTableTextColumnKeys() as $column)'{{ $column }}',@endforeach]);
4 changes: 4 additions & 0 deletions src/Commands/FilamentTestsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\CanRenderIndexPageTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\CanSearchColumnTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\CanSortColumnTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\ColumnHasCorrectFormattedStateTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\ColumnHasCorrectStateTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\HasColumnTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\HidesColumnTest;
use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\ShowsColumnTest;
Expand Down Expand Up @@ -71,6 +73,8 @@ protected function getRenderers(): array
CanSearchColumnTest::class,
CanDeleteRecordTest::class,
CanNotDisplayTrashedRecordsByDefault::class,
ColumnHasCorrectStateTest::class,
ColumnHasCorrectFormattedStateTest::class,
CanPaginateRecordsTest::class,
];
}
Expand Down
11 changes: 11 additions & 0 deletions src/Concerns/Resources/InteractsWithTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public function getResourceTableVisibleColumns(): Collection
->filter(fn (Column $column): bool => $column->isVisible());
}

public function getResourceTableTextColumns(): Collection
{
return $this->getResourceTableColumns()
->filter(fn (Column $column): bool => $column instanceof \Filament\Tables\Columns\TextColumn);
}

public function getResourceTableTextColumnKeys(): array
{
return $this->getResourceTableColumnKeysFrom($this->getResourceTableTextColumns());
}

public function getResourceTableVisibleColumnKeys(): array
{
return $this->getResourceTableColumnKeysFrom($this->getResourceTableVisibleColumns());
Expand Down
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 ColumnHasCorrectFormattedStateTest extends BaseTest
{
public ?string $view = 'filament-tests::resources.pages.index.column-has-correct-formatted-state';

public function getShouldRender(): bool
{
return $this->hasPage('index')
&& $this->getResourceTableVisibleColumns()->isNotEmpty();
}
}
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 ColumnHasCorrectStateTest extends BaseTest
{
public ?string $view = 'filament-tests::resources.pages.index.column-has-correct-state';

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