feat(test): membuat testing untuk module forgot dan reset password
This commit is contained in:
parent
eca3a7cd7b
commit
40681b4347
78
tests/Feature/Livewire/Auth/ForgotPasswordTest.php
Normal file
78
tests/Feature/Livewire/Auth/ForgotPasswordTest.php
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Livewire\Auth\ForgotPassword;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\Password;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
|
||||||
|
uses(RefreshDatabase::class);
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Helpers
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
function attemptSendResetLink(string $email)
|
||||||
|
{
|
||||||
|
return Livewire::test(ForgotPassword::class)
|
||||||
|
->set('email', $email)
|
||||||
|
->call('sendLink');
|
||||||
|
}
|
||||||
|
|
||||||
|
function mockPasswordResetStatus(string $status, string $email = 'user@example.com'): void
|
||||||
|
{
|
||||||
|
Password::shouldReceive('sendResetLink')
|
||||||
|
->once()
|
||||||
|
->with(['email' => $email])
|
||||||
|
->andReturn($status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Component Rendering
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
it('renders forgot password component successfully', function () {
|
||||||
|
Livewire::test(ForgotPassword::class)
|
||||||
|
->assertViewIs('livewire.auth.forgot-password')
|
||||||
|
->assertSet('email', '');
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Validation Rules
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
it('validates email field is required', function () {
|
||||||
|
Livewire::test(ForgotPassword::class)
|
||||||
|
->set('email', '')
|
||||||
|
->call('sendLink')
|
||||||
|
->assertHasErrors(['email' => 'required']);
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Send Reset Link
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
it('sends reset link successfully', function () {
|
||||||
|
$email = 'user@example.com';
|
||||||
|
|
||||||
|
mockPasswordResetStatus(Password::ResetLinkSent, $email);
|
||||||
|
|
||||||
|
attemptSendResetLink($email)
|
||||||
|
->assertHasNoErrors();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles failure when sending reset link', function () {
|
||||||
|
$email = 'user@example.com';
|
||||||
|
|
||||||
|
mockPasswordResetStatus('passwords.user', $email);
|
||||||
|
|
||||||
|
attemptSendResetLink($email)
|
||||||
|
->assertHasNoErrors();
|
||||||
|
});
|
||||||
102
tests/Feature/Livewire/Auth/ResetPasswordTest.php
Normal file
102
tests/Feature/Livewire/Auth/ResetPasswordTest.php
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
<?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 mockPasswordResetStatus(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 () {
|
||||||
|
mockPasswordResetStatus(Password::PasswordReset);
|
||||||
|
|
||||||
|
attemptResetPassword()
|
||||||
|
->assertHasNoErrors()
|
||||||
|
->assertRedirect(route('login', [
|
||||||
|
'message' => 'Kata sandi berhasil diperbarui, silakan masuk kembali.',
|
||||||
|
]));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles failure when resetting password', function () {
|
||||||
|
mockPasswordResetStatus('passwords.token');
|
||||||
|
|
||||||
|
attemptResetPassword()
|
||||||
|
->assertHasNoErrors();
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user