refactor(user): menjadikan fungsi untuk kode yang berulang kali

This commit is contained in:
Yoga Pangestu 2025-10-15 20:54:44 +07:00
parent 70b28b335c
commit 1573ac21ec

View File

@ -17,9 +17,13 @@
$this->user = User::factory()->create();
});
function mountCreateUser(User $admin)
{
return Livewire::actingAs($admin)->test(Create::class);
}
it('renders create user page', function () {
Livewire::actingAs($this->user)
->test(Create::class)
mountCreateUser($this->user)
->assertViewIs('livewire.studio.master.user.form')
->assertViewHas('pageTitle', 'Tambah Pegawai');
});
@ -27,8 +31,7 @@
it('mounts outlets correctly', function () {
$outlets = Outlet::factory()->count(3)->create();
$component = Livewire::actingAs($this->user)
->test(Create::class);
$component = mountCreateUser($this->user);
expect($component->outlets)->toHaveCount(3);
foreach ($outlets as $outlet) {
@ -40,8 +43,7 @@
it('prevents creation when unauthorized', function () {
Gate::define('create user', fn () => false);
Livewire::actingAs($this->user)
->test(Create::class)
mountCreateUser($this->user)
->call('save')
->assertForbidden();
});
@ -67,8 +69,7 @@
'outlet_ids' => $outlets->pluck('id')->toArray(),
]);
Livewire::actingAs($this->user)
->test(Create::class)
mountCreateUser($this->user)
->set('form.full_name', $data['full_name'])
->set('form.username', $data['username'])
->set('form.email', $data['email'])