|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Livewire\Customer; |
| 4 | + |
| 5 | +use App\Features\ShowAuthButtons; |
| 6 | +use App\Livewire\Customer\Notifications; |
| 7 | +use App\Models\User; |
| 8 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 9 | +use Illuminate\Notifications\DatabaseNotification; |
| 10 | +use Illuminate\Support\Str; |
| 11 | +use Laravel\Pennant\Feature; |
| 12 | +use Livewire\Livewire; |
| 13 | +use Tests\TestCase; |
| 14 | + |
| 15 | +class NotificationsTest extends TestCase |
| 16 | +{ |
| 17 | + use RefreshDatabase; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + parent::setUp(); |
| 22 | + |
| 23 | + Feature::define(ShowAuthButtons::class, true); |
| 24 | + } |
| 25 | + |
| 26 | + // --- Page rendering --- |
| 27 | + |
| 28 | + public function test_notifications_page_renders_successfully(): void |
| 29 | + { |
| 30 | + $user = User::factory()->create(); |
| 31 | + |
| 32 | + $response = $this->withoutVite()->actingAs($user)->get('/dashboard/notifications'); |
| 33 | + |
| 34 | + $response->assertStatus(200); |
| 35 | + } |
| 36 | + |
| 37 | + public function test_notifications_page_requires_authentication(): void |
| 38 | + { |
| 39 | + $response = $this->withoutVite()->get('/dashboard/notifications'); |
| 40 | + |
| 41 | + $response->assertRedirect('/login'); |
| 42 | + } |
| 43 | + |
| 44 | + public function test_notifications_component_renders_headings(): void |
| 45 | + { |
| 46 | + $user = User::factory()->create(); |
| 47 | + |
| 48 | + Livewire::actingAs($user) |
| 49 | + ->test(Notifications::class) |
| 50 | + ->assertSee('Notifications') |
| 51 | + ->assertSee('Stay up to date with your account activity.') |
| 52 | + ->assertStatus(200); |
| 53 | + } |
| 54 | + |
| 55 | + // --- Displaying notifications --- |
| 56 | + |
| 57 | + public function test_notifications_display_in_reverse_chronological_order(): void |
| 58 | + { |
| 59 | + $user = User::factory()->create(); |
| 60 | + |
| 61 | + $this->createNotification($user, ['title' => 'First'], now()->subHours(2)); |
| 62 | + $this->createNotification($user, ['title' => 'Second'], now()->subHour()); |
| 63 | + $this->createNotification($user, ['title' => 'Third'], now()); |
| 64 | + |
| 65 | + Livewire::actingAs($user) |
| 66 | + ->test(Notifications::class) |
| 67 | + ->assertSeeInOrder(['Third', 'Second', 'First']); |
| 68 | + } |
| 69 | + |
| 70 | + public function test_empty_state_shown_when_no_notifications(): void |
| 71 | + { |
| 72 | + $user = User::factory()->create(); |
| 73 | + |
| 74 | + Livewire::actingAs($user) |
| 75 | + ->test(Notifications::class) |
| 76 | + ->assertSee('No notifications') |
| 77 | + ->assertSee("You're all caught up!", escape: false); |
| 78 | + } |
| 79 | + |
| 80 | + public function test_notification_title_and_body_are_displayed(): void |
| 81 | + { |
| 82 | + $user = User::factory()->create(); |
| 83 | + |
| 84 | + $this->createNotification($user, [ |
| 85 | + 'title' => 'License Renewed', |
| 86 | + 'body' => 'Your license has been renewed successfully.', |
| 87 | + ]); |
| 88 | + |
| 89 | + Livewire::actingAs($user) |
| 90 | + ->test(Notifications::class) |
| 91 | + ->assertSee('License Renewed') |
| 92 | + ->assertSee('Your license has been renewed successfully.'); |
| 93 | + } |
| 94 | + |
| 95 | + // --- Mark as read --- |
| 96 | + |
| 97 | + public function test_mark_single_notification_as_read(): void |
| 98 | + { |
| 99 | + $user = User::factory()->create(); |
| 100 | + |
| 101 | + $notification = $this->createNotification($user, ['title' => 'Test']); |
| 102 | + |
| 103 | + $this->assertNull($notification->read_at); |
| 104 | + |
| 105 | + Livewire::actingAs($user) |
| 106 | + ->test(Notifications::class) |
| 107 | + ->call('markAsRead', $notification->id); |
| 108 | + |
| 109 | + $this->assertNotNull($notification->fresh()->read_at); |
| 110 | + } |
| 111 | + |
| 112 | + public function test_mark_all_as_read(): void |
| 113 | + { |
| 114 | + $user = User::factory()->create(); |
| 115 | + |
| 116 | + $n1 = $this->createNotification($user, ['title' => 'First']); |
| 117 | + $n2 = $this->createNotification($user, ['title' => 'Second']); |
| 118 | + |
| 119 | + Livewire::actingAs($user) |
| 120 | + ->test(Notifications::class) |
| 121 | + ->call('markAllAsRead'); |
| 122 | + |
| 123 | + $this->assertNotNull($n1->fresh()->read_at); |
| 124 | + $this->assertNotNull($n2->fresh()->read_at); |
| 125 | + } |
| 126 | + |
| 127 | + public function test_mark_all_as_read_button_hidden_when_none_unread(): void |
| 128 | + { |
| 129 | + $user = User::factory()->create(); |
| 130 | + |
| 131 | + $this->createNotification($user, ['title' => 'Read one'], now(), now()); |
| 132 | + |
| 133 | + Livewire::actingAs($user) |
| 134 | + ->test(Notifications::class) |
| 135 | + ->assertDontSee('Mark all as read'); |
| 136 | + } |
| 137 | + |
| 138 | + public function test_mark_all_as_read_button_shown_when_unread_exist(): void |
| 139 | + { |
| 140 | + $user = User::factory()->create(); |
| 141 | + |
| 142 | + $this->createNotification($user, ['title' => 'Unread one']); |
| 143 | + |
| 144 | + Livewire::actingAs($user) |
| 145 | + ->test(Notifications::class) |
| 146 | + ->assertSee('Mark all as read'); |
| 147 | + } |
| 148 | + |
| 149 | + // --- Bell icon in layout --- |
| 150 | + |
| 151 | + public function test_bell_icon_shows_in_dashboard_layout(): void |
| 152 | + { |
| 153 | + $user = User::factory()->create(); |
| 154 | + |
| 155 | + $response = $this->withoutVite()->actingAs($user)->get('/dashboard/settings'); |
| 156 | + |
| 157 | + $response->assertStatus(200); |
| 158 | + $response->assertSee(route('customer.notifications')); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Create a database notification for a user. |
| 163 | + */ |
| 164 | + private function createNotification(User $user, array $data, ?\Carbon\Carbon $createdAt = null, ?\Carbon\Carbon $readAt = null): DatabaseNotification |
| 165 | + { |
| 166 | + return DatabaseNotification::create([ |
| 167 | + 'id' => Str::uuid()->toString(), |
| 168 | + 'type' => 'App\\Notifications\\TestNotification', |
| 169 | + 'notifiable_type' => User::class, |
| 170 | + 'notifiable_id' => $user->id, |
| 171 | + 'data' => $data, |
| 172 | + 'read_at' => $readAt, |
| 173 | + 'created_at' => $createdAt ?? now(), |
| 174 | + 'updated_at' => $createdAt ?? now(), |
| 175 | + ]); |
| 176 | + } |
| 177 | +} |
0 commit comments