F
Filamentβ€’3mo ago
lazydog

Testing with Tenancy

How can I make test with tenancy? Got this error now
it('has a form', function () {
livewire(CreateTag::class)
->assertFormExists();
});
it('has a form', function () {
livewire(CreateTag::class)
->assertFormExists();
});
Missing required parameter for [Route: filament.client.resources.tags.index] [URI: client/{tenant}/tags] [Missing parameter: tenant].
Solution:
not taking credit for everyone shard on that thread, thank you all ❀️ but feels this can help other if it marks as resolved and got indexed it also looks like a good "trick" to add πŸ˜‚ so I am copy it here...
Jump to solution
4 Replies
Skrypt
Skryptβ€’3mo ago
Hi, have you found a solution? I have the same problem Answered here: https://discord.com/channels/883083792112300104/1223555189420720180
Solution
Lara Zeus
Lara Zeusβ€’3mo ago
not taking credit for everyone shard on that thread, thank you all ❀️ but feels this can help other if it marks as resolved and got indexed it also looks like a good "trick" to add πŸ˜‚ so I am copy it here test render tenant owned records
use function Pest\Livewire\livewire;

it('can only render tenant owned records', function () { // Edit logic here ??
$posts = Post::factory()->count(4)->create();
$trashedPosts = Post::factory()->trashed()->count(6)->create();

livewire(PostResource\Pages\ListPosts::class)
->assertCanSeeTableRecords($posts)
->assertCanNotSeeTableRecords($trashedPosts)
->assertCountTableRecords(4);
});
use function Pest\Livewire\livewire;

it('can only render tenant owned records', function () { // Edit logic here ??
$posts = Post::factory()->count(4)->create();
$trashedPosts = Post::factory()->trashed()->count(6)->create();

livewire(PostResource\Pages\ListPosts::class)
->assertCanSeeTableRecords($posts)
->assertCanNotSeeTableRecords($trashedPosts)
->assertCountTableRecords(4);
});
set tenant for tests
beforeEach(function ((){
Filament::setTenant(Mytenantmodel::factory()->create());
});
beforeEach(function ((){
Filament::setTenant(Mytenantmodel::factory()->create());
});
Skrypt
Skryptβ€’3mo ago
Also, how would you go about testing a "Simple Resource" (where there is only a ManageRecords page)? How would you test the CreateAction modals and test whether the record is created? EDIT: Okay, not sure if it's the best way, but this works (sorry example is in PHPUnit, I find Pest utterly confusing πŸ˜„ )
Livewire::test(ManageUsers::class)
->mountAction('create')
->setActionData([
'name' => 'Test',
])
->callAction('create')
->assertHasNoFormComponentActionErrors();

$this->assertDatabaseHas(Record::class, [
'name' => 'Test',
]);
Livewire::test(ManageUsers::class)
->mountAction('create')
->setActionData([
'name' => 'Test',
])
->callAction('create')
->assertHasNoFormComponentActionErrors();

$this->assertDatabaseHas(Record::class, [
'name' => 'Test',
]);
lazydog
lazydogβ€’3mo ago
Thank you all πŸ™‚