Help with table action test

public function test_table_actions()
{
$user = User::factory()->create();

Livewire::test(ListUsers::class)
->callTableAction(DeleteAction::class, $user)
->assertHasNoTableActionErrors();

$this->assertSoftDeleted('users', ['id' => $user->id]);

$user2 = User::factory()->create();

Livewire::test(ListUsers::class)
->assertTableActionHasUrl('view', UserResource::getUrl('view', ['record' => $user2]))
->assertTableActionHasUrl('edit', UserResource::getUrl('edit', ['record' => $user2]));

}
public function test_table_actions()
{
$user = User::factory()->create();

Livewire::test(ListUsers::class)
->callTableAction(DeleteAction::class, $user)
->assertHasNoTableActionErrors();

$this->assertSoftDeleted('users', ['id' => $user->id]);

$user2 = User::factory()->create();

Livewire::test(ListUsers::class)
->assertTableActionHasUrl('view', UserResource::getUrl('view', ['record' => $user2]))
->assertTableActionHasUrl('edit', UserResource::getUrl('edit', ['record' => $user2]));

}
FAILED Tests\Feature\Software\Resources\UserResource\ListUsersTest > table actions TypeError Filament\Resources\Pages\ListRecords::Filament\Resources\Pages{closure}(): Argument #1 ($record) must be of type Illuminate\Database\Eloquent\Model, null given, called in D:\Work\laragon\www\myapp\vendor\filament\support\src\Concerns\EvaluatesClosures.php on line 35
$user2 = User::factory()->create();

Livewire::test(ListUsers::class)
->assertTableActionHasUrl('view', UserResource::getUrl('view', ['record' => $user2]))
->assertTableActionHasUrl('edit', UserResource::getUrl('edit', ['record' => $user2]));
$user2 = User::factory()->create();

Livewire::test(ListUsers::class)
->assertTableActionHasUrl('view', UserResource::getUrl('view', ['record' => $user2]))
->assertTableActionHasUrl('edit', UserResource::getUrl('edit', ['record' => $user2]));
the test fails here i'm not sure why can someone maybe help me
dd($user2);

//result
App\Models\Software\User {#5115
#attributes: array:8 [
"name" => "Pietro Hand"
"email" => "[email protected]"
"email_verified_at" => "2024-06-18 19:55:40"
"password" => "$2y$04$2xYahuL1RFq8aI2IPZuuJ.hXNWr5B.p5DX/TDM0OQJsv1CVs4L7FO"
"remember_token" => "uLr3LbCKDe"
"updated_at" => "2024-06-18 19:55:40"
"created_at" => "2024-06-18 19:55:40"
"id" => 2
]
dd($user2);

//result
App\Models\Software\User {#5115
#attributes: array:8 [
"name" => "Pietro Hand"
"email" => "[email protected]"
"email_verified_at" => "2024-06-18 19:55:40"
"password" => "$2y$04$2xYahuL1RFq8aI2IPZuuJ.hXNWr5B.p5DX/TDM0OQJsv1CVs4L7FO"
"remember_token" => "uLr3LbCKDe"
"updated_at" => "2024-06-18 19:55:40"
"created_at" => "2024-06-18 19:55:40"
"id" => 2
]
9 Replies
KingStalker
KingStalkerOP7mo ago
any one can help?
LeandroFerreira
LeandroFerreira7mo ago
Does it pass before $user2 = User::factory()->create(); ?
KingStalker
KingStalkerOP7mo ago
@Leandro Ferreira yes @Leandro Ferreira for now im using this so it can just pass
public function test_table_actions()
{
Livewire::test(ListUsers::class)
->assertTableActionExists('view');

Livewire::test(ListUsers::class)
->assertTableActionExists('edit');

Livewire::test(ListUsers::class)
->assertTableActionExists('delete')
->callTableAction(DeleteAction::class, $this->user)
->assertHasNoTableActionErrors();

$this->assertSoftDeleted('users', ['id' => $this->user->id]);

Livewire::test(ListUsers::class)
->assertTableActionExists('restore');

}
public function test_table_actions()
{
Livewire::test(ListUsers::class)
->assertTableActionExists('view');

Livewire::test(ListUsers::class)
->assertTableActionExists('edit');

Livewire::test(ListUsers::class)
->assertTableActionExists('delete')
->callTableAction(DeleteAction::class, $this->user)
->assertHasNoTableActionErrors();

$this->assertSoftDeleted('users', ['id' => $this->user->id]);

Livewire::test(ListUsers::class)
->assertTableActionExists('restore');

}
LeandroFerreira
LeandroFerreira7mo ago
ok but you want to test it creating a user1, deleting it and creating a user2, right?
KingStalker
KingStalkerOP7mo ago
no just that it appears with corect url its a EditAction \ ViewAction i think this is fine enough do you agree?
public function test_table_actions()
{
Livewire::test(ListUsers::class)
->assertTableActionExists('view');

Livewire::test(ListUsers::class)
->assertTableActionExists('edit');

Livewire::test(ListUsers::class)
->assertTableActionExists('delete')
->callTableAction(DeleteAction::class, $this->user)
->assertHasNoTableActionErrors();

$this->assertSoftDeleted('users', ['id' => $this->user->id]);

Livewire::test(ListUsers::class)
->assertTableActionExists('restore');

}
public function test_table_actions()
{
Livewire::test(ListUsers::class)
->assertTableActionExists('view');

Livewire::test(ListUsers::class)
->assertTableActionExists('edit');

Livewire::test(ListUsers::class)
->assertTableActionExists('delete')
->callTableAction(DeleteAction::class, $this->user)
->assertHasNoTableActionErrors();

$this->assertSoftDeleted('users', ['id' => $this->user->id]);

Livewire::test(ListUsers::class)
->assertTableActionExists('restore');

}
LeandroFerreira
LeandroFerreira7mo ago
I think you could use
->assertTableActionExists(['view', 'edit', 'delete'])
->callTableAction(DeleteAction::class, $this->user)
->assertHasNoTableActionErrors();
->assertTableActionExists(['view', 'edit', 'delete'])
->callTableAction(DeleteAction::class, $this->user)
->assertHasNoTableActionErrors();
KingStalker
KingStalkerOP7mo ago
thanks il give it a shot
LeandroFerreira
LeandroFerreira7mo ago
tip: https://pestphp.com/docs/plugins#content-livewire livewire(ListUsers::class) instead of Livewire::test(ListUsers::class)
Plugins | Pest - The elegant PHP Testing Framework
In this section, we will discuss the official and community developed plugins that we endorse. Plugins primarily offer namespaced functions, console commands, custom expectations, and additional command-line options to augment the default Pest experience.
KingStalker
KingStalkerOP7mo ago
@Leandro Ferreira thank you
Want results from more Discord servers?
Add your server