Skip to content

Instantly share code, notes, and snippets.

@CodeWithDennis
Created November 11, 2024 09:09
Show Gist options
  • Save CodeWithDennis/4b2fa103cbc628b23c514ea581baa51e to your computer and use it in GitHub Desktop.
Save CodeWithDennis/4b2fa103cbc628b23c514ea581baa51e to your computer and use it in GitHub Desktop.
A sample resource test file for your FilamentPHP app, demonstrating how to test with a "company" resource.
<?php
use App\Filament\Resources\CompanyResource\Pages\CreateCompany;
use App\Filament\Resources\CompanyResource\Pages\EditCompany;
use App\Filament\Resources\CompanyResource\Pages\ListCompanies;
use App\Models\Company;
use Filament\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
use Illuminate\Support\Str;
use function Pest\Livewire\livewire;
it('can render the index page', function () {
livewire(ListCompanies::class)
->assertSuccessful();
});
it('can render the create page', function () {
livewire(CreateCompany::class)
->assertSuccessful();
});
it('can render the edit page', function () {
$record = Company::factory()->create();
livewire(EditCompany::class, ['record' => $record->getRouteKey()])
->assertSuccessful();
});
it('has column', function (string $column) {
livewire(ListCompanies::class)
->assertTableColumnExists($column);
})->with(['name', 'email', 'phone', 'status', 'created_at', 'updated_at']);
it('can render column', function (string $column) {
livewire(ListCompanies::class)
->assertCanRenderTableColumn($column);
})->with(['name', 'email', 'phone', 'status', 'created_at', 'updated_at']);
it('can sort column', function (string $column) {
$records = Company::factory(5)->create();
livewire(ListCompanies::class)
->sortTable($column)
->assertCanSeeTableRecords($records->sortBy($column), inOrder: true)
->sortTable($column, 'desc')
->assertCanSeeTableRecords($records->sortByDesc($column), inOrder: true);
})->with(['name', 'status', 'created_at', 'updated_at']);
it('can search column', function (string $column) {
$records = Company::factory(5)->create();
$search = data_get($records->first(), $column);
livewire(ListCompanies::class)
->searchTable($search instanceof BackedEnum ? $search->value : $search)
->assertCanSeeTableRecords($records->filter(fn (Company $record) => data_get($record, $column) == $search));
})->with(['name', 'email', 'phone']);
it('can create a company', function () {
$record = Company::factory()->make();
livewire(CreateCompany::class)
->fillForm([
'name' => $record->name,
'email' => $record->email,
'phone' => $record->phone,
'status' => $record->status,
])
->assertActionExists('create')
->call('create')
->assertHasNoFormErrors();
$this->assertDatabaseHas(Company::class, [
'name' => $record->name,
'email' => $record->email,
]);
});
it('can update a company', function () {
$record = Company::factory()->create();
$newRecord = Company::factory()->make();
livewire(EditCompany::class, ['record' => $record->getRouteKey()])
->fillForm([
'name' => $newRecord->name,
'email' => $newRecord->email,
'phone' => $newRecord->phone,
'status' => $newRecord->status,
])
->assertActionExists('save')
->call('save')
->assertHasNoFormErrors();
$this->assertDatabaseHas(Company::class, [
'name' => $newRecord->name,
'email' => $newRecord->email,
'phone' => $newRecord->phone,
'status' => $newRecord->status,
]);
});
it('can delete a company', function () {
$record = Company::factory()->create();
livewire(EditCompany::class, ['record' => $record->getRouteKey()])
->assertActionExists('delete')
->callAction(DeleteAction::class);
$this->assertModelMissing($record);
});
it('can bulk delete companies', function () {
$records = Company::factory(5)->create();
livewire(ListCompanies::class)
->assertTableBulkActionExists('delete')
->callTableBulkAction(DeleteBulkAction::class, $records);
foreach ($records as $record) {
$this->assertModelMissing($record);
}
});
it('can validate required', function (string $column) {
livewire(CreateCompany::class)
->fillForm([$column => null])
->assertActionExists('create')
->call('create')
->assertHasFormErrors([$column => ['required']]);
})->with(['name', 'email', 'status']);
it('can validate email', function (string $column) {
livewire(CreateCompany::class)
->fillForm(['email' => Str::random()])
->assertActionExists('create')
->call('create')
->assertHasFormErrors([$column => ['email']]);
})->with(['email']);
it('can validate max length', function (string $column) {
livewire(CreateCompany::class)
->fillForm([$column => Str::random(256)])
->assertActionExists('create')
->call('create')
->assertHasFormErrors([$column => ['max:255']]);
})->with(['name', 'email', 'phone']);
it('can validate status enum', function () {
$record = Company::factory()->make();
livewire(CreateCompany::class)
->fillForm([
'name' => $record->name,
'email' => $record->email,
'phone' => $record->phone,
'status' => 'invalid-status',
])
->assertActionExists('create')
->call('create')
->assertHasFormErrors([
'status' => ['Illuminate\Validation\Rules\Enum'],
]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment