Skip to content

Commit

Permalink
Merge pull request #100 from dissto/add-render-test-create-page
Browse files Browse the repository at this point in the history
Add render tests for `create` and `edit` page
  • Loading branch information
CodeWithDennis authored Apr 3, 2024
2 parents aac722f + 85d27d4 commit 0e2fb0a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ won't be generated.
**Resources**
- [x] [It can render page](https://filamentphp.com/docs/3.x/tables/testing#render)
- [x] [It can render create page](https://filamentphp.com/docs/3.x/panels/testing#create)
- [x] [It can render edit page](https://filamentphp.com/docs/3.x/panels/testing#edit)
- [x] [It can not render page](https://filamentphp.com/docs/3.x/tables/testing#render)
- [x] [It can sort column](https://filamentphp.com/docs/3.x/tables/testing#sorting)
- [x] [It can render column](https://filamentphp.com/docs/3.x/tables/testing#columns)
Expand Down
20 changes: 20 additions & 0 deletions src/Commands/FilamentResourceTestsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ public function handle(): int
return self::SUCCESS;
}

protected function getResourcePages(Resource $resource): Collection
{
return collect($resource::getPages())->keys();
}

protected function hasPage(string $name, Resource $resource): bool
{
return $this->getResourcePages($resource)->contains($name);
}

protected function getTableColumns(Resource $resource): Collection
{
return collect($this->getResourceTable($resource)->getColumns());
Expand Down Expand Up @@ -211,6 +221,16 @@ protected function getStubs(Resource $resource): array
// Base stubs that are always included
$stubs = ['Base', 'RenderPage'];

// Check if there is a create page
if ($this->hasPage('create', $resource)) {
$stubs[] = 'RenderCreatePage';
}

// Check if there is an edit page
if ($this->hasPage('edit', $resource)) {
$stubs[] = 'RenderEditPage';
}

// Add additional stubs based on the columns
if ($this->getTableColumns($resource)->isNotEmpty()) {
$stubs[] = 'HasColumn';
Expand Down
3 changes: 3 additions & 0 deletions stubs/Base.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use App\Filament\Resources\{{ RESOURCE }}\Pages\List{{ MODEL_PLURAL_NAME }};
use App\Filament\Resources\{{ RESOURCE }};

use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Actions\RestoreAction;
Expand All @@ -12,6 +14,7 @@ use Filament\Tables\Actions\ForceDeleteBulkAction;

use function Pest\Laravel\actingAs;
use function Pest\Laravel\assertModelMissing;
use function Pest\Laravel\get;
use function Pest\Livewire\livewire;

uses()->group('filament-resource-tests');
Expand Down
3 changes: 3 additions & 0 deletions stubs/RenderCreatePage.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it('can render the create page', function () {
get({{ RESOURCE }}::getUrl('create'))->assertSuccessful();
})->group('rendering');
5 changes: 5 additions & 0 deletions stubs/RenderEditPage.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
it('can render the edit page', function () {
$record = {{ MODEL_SINGULAR_NAME }}::factory()->create();

get({{ RESOURCE }}::getUrl('edit', ['record' => $record]))->assertSuccessful();
})->group('rendering');

0 comments on commit 0e2fb0a

Please sign in to comment.