parfum/tests/Feature/Studio/Master/User/EditTest.php

65 lines
2.2 KiB
PHP

<?php
use App\Enums\UserStatus;
use App\Livewire\Studio\Master\User\Edit;
use App\Models\Employee;
use App\Models\User;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
beforeEach(function () {
$this->setupUser();
$role = Role::firstOrCreate(['name' => 'Owner']);
Permission::firstOrCreate(['name' => 'update user']);
$role->givePermissionTo('update user');
$this->user->assignRole($role);
$this->targetUser = User::factory()->create();
$this->targetEmployee = Employee::factory()->for($this->targetUser)->create();
$this->targetUser->outlets()->attach($this->outlet);
$this->targetUser->assignRole(Role::firstOrCreate(['name' => 'Admin']));
});
it('renders the edit user page correctly', function () {
$this->actingAs($this->user)
->get(route('studio.master.user.edit', $this->targetUser))
->assertOk()
->assertSeeLivewire(Edit::class);
});
it('loads existing user data correctly', function () {
Livewire::actingAs($this->user)
->test(Edit::class, ['user' => $this->targetUser])
->assertSet('form.full_name', $this->targetEmployee->full_name)
->assertSet('form.username', $this->targetUser->username);
});
it('can update a user', function () {
Livewire::actingAs($this->user)
->test(Edit::class, ['user' => $this->targetUser])
->set('form.full_name', 'Pegawai Diperbarui')
->set('form.status', UserStatus::INACTIVE->value)
->call('save')
->assertHasNoErrors()
->assertRedirect(route('studio.master.user.index', ['notification' => 'Pegawai berhasil diperbarui.']));
$this->targetUser->refresh();
$this->targetEmployee->refresh();
expect($this->targetEmployee->full_name)->toBe('Pegawai Diperbarui');
expect($this->targetUser->status)->toBe(UserStatus::INACTIVE);
});
it('cannot update user without permission', function () {
$this->user->roles()->detach();
$this->user->permissions()->detach();
Livewire::actingAs($this->user)
->test(Edit::class, ['user' => $this->targetUser])
->set('form.full_name', 'Unauthorized Edit')
->call('save')
->assertForbidden();
});