parfum/tests/Feature/Livewire/Auth/ResetPasswordTest.php
Yoga Pangestu 2b5983fb2a fix(user): memperbaiki attach role dan mengubha tipe data prop gender ke int
- kasih otorisasi saat delete dan reset password
- memperbaiki testnya
- kasih kondisi untuk data kosong jangan di tampilkan di view
2025-12-10 08:36:29 +07:00

103 lines
2.9 KiB
PHP

<?php
use App\Livewire\Auth\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Password;
use Livewire\Livewire;
uses(RefreshDatabase::class);
/*
|--------------------------------------------------------------------------
| Helpers
|--------------------------------------------------------------------------
*/
function makeResetComponent(string $token = 'sample-token', string $email = 'user@example.com')
{
return Livewire::withQueryParams([
'token' => $token,
'email' => $email,
])->test(ResetPassword::class);
}
function attemptResetPassword(
string $token = 'sample-token',
string $email = 'user@example.com',
string $password = 'password123',
string $passwordConfirmation = 'password123'
) {
return makeResetComponent($token, $email)
->set('form.password', $password)
->set('form.password_confirmation', $passwordConfirmation)
->call('resetPassword');
}
function mockPasswordReset(string $status): void
{
Password::shouldReceive('reset')
->once()
->andReturn($status);
}
/*
|--------------------------------------------------------------------------
| Component Rendering & Mount
|--------------------------------------------------------------------------
*/
it('renders reset password component successfully', function () {
$token = 'sample-token';
$email = 'user@example.com';
makeResetComponent($token, $email)
->assertViewIs('livewire.auth.reset-password')
->assertSet('token', $token)
->assertSet('email', $email);
});
/*
|--------------------------------------------------------------------------
| Validation Rules
|--------------------------------------------------------------------------
*/
it('validates password fields are required', function () {
makeResetComponent()
->set('form.password', '')
->set('form.password_confirmation', '')
->call('resetPassword')
->assertHasErrors(['form.password', 'form.password_confirmation']);
});
it('validates password confirmation must match', function () {
makeResetComponent()
->set('form.password', 'password123')
->set('form.password_confirmation', 'different123')
->call('resetPassword')
->assertHasErrors(['form.password' => 'confirmed']);
});
/*
|--------------------------------------------------------------------------
| Reset Password
|--------------------------------------------------------------------------
*/
it('resets password successfully', function () {
mockPasswordReset(Password::PasswordReset);
attemptResetPassword()
->assertHasNoErrors()
->assertRedirect(route('login', [
'message' => 'Kata sandi berhasil diperbarui, silakan masuk kembali.',
]));
});
it('handles failure when resetting password', function () {
mockPasswordReset('passwords.token');
attemptResetPassword()
->assertHasNoErrors();
});