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

99 lines
3.3 KiB
PHP

<?php
use App\Livewire\Studio\Master\User\Index;
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' => 'view user']);
Permission::firstOrCreate(['name' => 'delete user']);
Permission::firstOrCreate(['name' => 'reset password user']);
$role->givePermissionTo(['view user', 'delete user', 'reset password user']);
$this->user->assignRole($role);
});
it('renders the user index page correctly', function () {
$this->actingAs($this->user)
->get(route('studio.master.user.index'))
->assertOk()
->assertSeeLivewire(Index::class);
});
it('displays list of employees correctly', function () {
$employees = collect();
foreach (range(1, 3) as $i) {
$employees->push(Employee::factory()->create());
}
Livewire::actingAs($this->user)
->test(Index::class)
->assertViewHas('employees', function ($viewEmployees) use ($employees) {
return $viewEmployees->count() >= 3;
})
->assertSee($employees->first()->full_name)
->assertSee($employees->last()->full_name);
});
it('can search employees by name', function () {
$targetEmployee = Employee::factory()->create(['full_name' => 'Pegawai Spesifik']);
$otherEmployee = Employee::factory()->create(['full_name' => 'Biasa Saja']);
Livewire::actingAs($this->user)
->test(Index::class)
->set('search', 'Spesifik')
->assertSee($targetEmployee->full_name)
->assertDontSee($otherEmployee->full_name);
});
it('can filter employees by status', function () {
$activeUser = User::factory()->active()->create();
$activeEmployee = Employee::factory()->for($activeUser)->create(['full_name' => 'Pegawai Aktif']);
$inactiveUser = User::factory()->inactive()->create();
$inactiveEmployee = Employee::factory()->for($inactiveUser)->create(['full_name' => 'Pegawai Nonaktif']);
Livewire::actingAs($this->user)
->test(Index::class)
->set('status', [\App\Enums\UserStatus::ACTIVE->value])
->assertSee($activeEmployee->full_name)
->assertDontSee($inactiveEmployee->full_name);
});
it('can delete a user', function () {
$userToDelete = User::factory()->create();
Employee::factory()->for($userToDelete)->create();
Livewire::actingAs($this->user)
->test(Index::class)
->call('delete', $userToDelete->id);
$this->assertSoftDeleted('users', ['id' => $userToDelete->id]);
$this->assertSoftDeleted('employees', ['user_id' => $userToDelete->id]);
});
it('can reset user password', function () {
$userToReset = User::factory()->create(['password' => Hash::make('old-password')]);
Livewire::actingAs($this->user)
->test(Index::class)
->call('resetPassword', $userToReset->id);
$userToReset->refresh();
expect(Hash::check(config('myconfig.password_default'), $userToReset->password))->toBeTrue();
});
it('cannot access index page without permission', function () {
$this->user->roles()->detach();
$this->user->permissions()->detach();
$this->actingAs($this->user)
->get(route('studio.master.user.index'))
->assertForbidden();
});