Skip to content

Commit

Permalink
Merge pull request #65 from dissto/add-tests-for-default-delete-actions
Browse files Browse the repository at this point in the history
Add tests for the default delete actions (bulk and regular)
  • Loading branch information
CodeWithDennis authored Mar 29, 2024
2 parents eafc604 + fae239c commit a13fb12
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/Commands/FilamentResourceTestsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,31 @@ protected function hasSoftDeletes(Resource $resource): bool
return method_exists($resource->getModel(), 'bootSoftDeletes');
}

protected function getResourceTableActions(Resource $resource): Collection
{
return collect($this->getResourceTable($resource)->getFlatActions());
}

protected function getResourceTableActionNames(Resource $resource): Collection
{
return $this->getResourceTableActions($resource)->map(fn ($action) => $action->getName());
}

protected function getResourceTableBulkActions(Resource $resource): Collection
{
return collect($this->getResourceTable($resource)->getFlatBulkActions());
}

protected function getResourceTableBulkActionNames(Resource $resource): Collection
{
return $this->getResourceTableBulkActions($resource)->map(fn ($action) => $action->getName());
}

protected function getStubs(Resource $resource): array
{
// Base stubs that are always included
$stubs = ['Base', 'RenderPage'];

// Get the columns of the resource table
$columns = collect($this->getResourceTable($resource)->getColumns());

// Add additional stubs based on the columns
if ($this->getTableColumns($resource)->isNotEmpty()) {
$stubs[] = 'HasColumn';
Expand All @@ -166,6 +183,16 @@ protected function getStubs(Resource $resource): array
$stubs[] = 'Trashed';
}

// Check if there is a delete action
if ($this->getResourceTableActionNames($resource)->contains('delete')) {
$stubs[] = ! $this->hasSoftDeletes($resource) ? 'Deleting' : 'DeletingSoftDeletes';
}

// Check if there is a bulk delete action
if ($this->getResourceTableBulkActionNames($resource)->contains('delete')) {
$stubs[] = ! $this->hasSoftDeletes($resource) ? 'BulkDeleting' : 'BulkDeletingSoftDeletes';
}

// Return the stubs
return $stubs;
}
Expand Down
2 changes: 2 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 Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
$MODEL_IMPORT$

use function Pest\Laravel\actingAs;
Expand Down
12 changes: 12 additions & 0 deletions stubs/BulkDeleting.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
it('can bulk delete records', function () {
$records = $MODEL_SINGULAR_NAME$::factory()->count(3)->create();

livewire(List$MODEL_PLURAL_NAME$::class)
->callTableBulkAction(DeleteBulkAction::class, $records);

foreach ($records as $record) {
$this->assertModelMissing($record);
}

expect($MODEL_SINGULAR_NAME$::find($records->pluck('id')))->toBeEmpty();
});
12 changes: 12 additions & 0 deletions stubs/BulkDeletingSoftDeletes.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
it('can bulk soft delete records', function () {
$records = $MODEL_SINGULAR_NAME$::factory()->count(3)->create();

livewire(List$MODEL_PLURAL_NAME$::class)
->callTableBulkAction(DeleteBulkAction::class, $records);

foreach ($records as $record) {
$this->assertSoftDeleted($record);
}

expect($MODEL_SINGULAR_NAME$::find($records->pluck('id')))->toBeEmpty();
});
8 changes: 8 additions & 0 deletions stubs/Deleting.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
it('can delete records', function () {
$record = $MODEL_SINGULAR_NAME$::factory()->create();

livewire(List$MODEL_PLURAL_NAME$::class)
->callTableAction(DeleteAction::class, $record);

$this->assertModelMissing($record);
});
8 changes: 8 additions & 0 deletions stubs/DeletingSoftDeletes.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
it('can soft delete records', function () {
$record = $MODEL_SINGULAR_NAME$::factory()->create();

livewire(List$MODEL_PLURAL_NAME$::class)
->callTableAction(DeleteAction::class, $record);

$this->assertSoftDeleted($record);
});

0 comments on commit a13fb12

Please sign in to comment.