|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Livewire\User\Profile; |
| 4 | +use App\Models\User; |
| 5 | +use Illuminate\Support\Facades\Hash; |
| 6 | +use Livewire\Livewire; |
| 7 | + |
| 8 | +beforeEach(function () { |
| 9 | + $this->user = User::factory()->create(); |
| 10 | + |
| 11 | + $this->actingAs($this->user); |
| 12 | +}); |
| 13 | + |
| 14 | +it('renders successfully', function () { |
| 15 | + Livewire::test(Profile::class) |
| 16 | + ->assertStatus(200) |
| 17 | + ->assertViewIs('livewire.user.profile'); |
| 18 | +}); |
| 19 | + |
| 20 | +it('mounts with authenticated user data', function () { |
| 21 | + Livewire::test(Profile::class) |
| 22 | + ->assertSet('user.id', $this->user->id) |
| 23 | + ->assertSet('user.name', $this->user->name); |
| 24 | +}); |
| 25 | + |
| 26 | +it('validates required name', function () { |
| 27 | + Livewire::test(Profile::class) |
| 28 | + ->set('user.name', '') |
| 29 | + ->call('save') |
| 30 | + ->assertHasErrors(['user.name' => 'required']); |
| 31 | +}); |
| 32 | + |
| 33 | +it('validates maximum length of name', function () { |
| 34 | + Livewire::test(Profile::class) |
| 35 | + ->set('user.name', str_repeat('a', 256)) |
| 36 | + ->call('save') |
| 37 | + ->assertHasErrors(['user.name' => 'max']); |
| 38 | +}); |
| 39 | + |
| 40 | +it('validates password confirmation', function () { |
| 41 | + Livewire::test(Profile::class) |
| 42 | + ->set('password', 'newpassword') |
| 43 | + ->set('password_confirmation', 'wrongconfirmation') |
| 44 | + ->call('save') |
| 45 | + ->assertHasErrors(['password' => 'confirmed']); |
| 46 | +}); |
| 47 | + |
| 48 | +it('allows updating name without changing password', function () { |
| 49 | + Livewire::test(Profile::class) |
| 50 | + ->set('user.name', 'Updated Name') |
| 51 | + ->call('save') |
| 52 | + ->assertHasNoErrors() |
| 53 | + ->assertDispatched('updated'); |
| 54 | + |
| 55 | + expect($this->user->refresh()->name)->toBe('Updated Name'); |
| 56 | +}); |
| 57 | + |
| 58 | +it('updates password when provided', function () { |
| 59 | + Hash::shouldReceive('isHashed') |
| 60 | + ->once() |
| 61 | + ->withAnyArgs() |
| 62 | + ->andReturn(false); |
| 63 | + |
| 64 | + Hash::shouldReceive('make')->andReturn('hashed-password'); |
| 65 | + |
| 66 | + Livewire::test(Profile::class) |
| 67 | + ->set('password', 'newpassword') |
| 68 | + ->set('password_confirmation', 'newpassword') |
| 69 | + ->call('save') |
| 70 | + ->assertHasNoErrors() |
| 71 | + ->assertDispatched('updated'); |
| 72 | + |
| 73 | + expect($this->user->refresh()->password)->toBe('hashed-password'); |
| 74 | +}); |
| 75 | + |
| 76 | +it('does not update password when null', function () { |
| 77 | + $originalPassword = $this->user->password; |
| 78 | + |
| 79 | + Livewire::test(Profile::class) |
| 80 | + ->set('password', null) |
| 81 | + ->call('save') |
| 82 | + ->assertHasNoErrors(); |
| 83 | + |
| 84 | + expect($this->user->fresh()->password)->toBe($originalPassword); |
| 85 | +}); |
| 86 | + |
| 87 | +it('dispatches success alert after saving', function () { |
| 88 | + Livewire::test(Profile::class) |
| 89 | + ->set('user.name', 'Updated Again') |
| 90 | + ->call('save') |
| 91 | + ->assertDispatched('updated') |
| 92 | + ->assertDispatched('tallstackui:dialog', function (string $event, array $params) { |
| 93 | + return $event === 'tallstackui:dialog' && |
| 94 | + $params['type'] === 'success' && |
| 95 | + $params['title'] === 'Done!' && |
| 96 | + $params['description'] === 'Task completed successfully.'; |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +it('resets password fields after saving', function () { |
| 101 | + Livewire::test(Profile::class) |
| 102 | + ->set('password', 'newpassword') |
| 103 | + ->set('password_confirmation', 'newpassword') |
| 104 | + ->call('save') |
| 105 | + ->assertSet('password', null) |
| 106 | + ->assertSet('password_confirmation', null); |
| 107 | +}); |
0 commit comments