F
Filament9mo ago
Jo

Testing relation managers

I have followed the info at https://filamentphp.com/docs/3.x/panels/resources/relation-managers#conditionally-showing-relation-managers to conditionally show a relation manager. However, I am facing a problem where testing always passes even when the relation manager is not rendered. As an extreme example, consider this code on the relation manager:
public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
{
return false;
}
public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
{
return false;
}
But this test passes:
use App\Filament\Resources\CategoryResource\Pages\EditCategory;
use function Pest\Livewire\livewire;

it('can render relation manager', function () {
$category = Category::factory()
->has(Post::factory()->count(10))
->create();

livewire(CategoryResource\RelationManagers\PostsRelationManager::class, [
'ownerRecord' => $category,
'pageClass' => EditCategory::class,
])
->assertSuccessful();
});
use App\Filament\Resources\CategoryResource\Pages\EditCategory;
use function Pest\Livewire\livewire;

it('can render relation manager', function () {
$category = Category::factory()
->has(Post::factory()->count(10))
->create();

livewire(CategoryResource\RelationManagers\PostsRelationManager::class, [
'ownerRecord' => $category,
'pageClass' => EditCategory::class,
])
->assertSuccessful();
});
In fact, to be even more extreme, I went into the CategoryResource class and removed the getRelations() function entirely so now I definitely don't have any relation manager displaying but that test is still passing. The only way I can get the test to fail is by deleting the PostsRelationManager entirely.
1 Reply
Jo
JoOP9mo ago
Anyone have any idea about this? It seems like I'm inadvertently testing whether the relationmanager works, when what I am really trying to do is test to see whether it's being displayed on a given page. This is because I have logic to hide the relation manager when the record has a certain status. So I want to test for that.
Want results from more Discord servers?
Add your server