Files
reservas/tests/Feature/NotificationCenterTest.php

46 lines
1.5 KiB
PHP

<?php
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Inertia\Testing\AssertableInertia as Assert;
uses(RefreshDatabase::class);
it('apresenta e marca como lidas apenas as notificações do utilizador', function () {
$user = User::factory()->create();
$other = User::factory()->create();
$notificationId = (string) Str::uuid();
$otherNotificationId = (string) Str::uuid();
foreach ([[$notificationId, $user], [$otherNotificationId, $other]] as [$id, $recipient]) {
DB::table('notifications')->insert([
'id' => $id,
'type' => 'test',
'notifiable_type' => $recipient->getMorphClass(),
'notifiable_id' => $recipient->id,
'data' => json_encode(['message' => 'Atualização de teste.', 'url' => '/requisicoes']),
'created_at' => now(),
'updated_at' => now(),
]);
}
$this->actingAs($user)
->get('/notificacoes')
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('notifications/index')
->has('notifications.data', 1)
->where('notifications.data.0.id', $notificationId));
$this->post("/notificacoes/{$notificationId}/ler")
->assertRedirect('/requisicoes');
$this->assertDatabaseMissing('notifications', [
'id' => $notificationId,
'read_at' => null,
]);
$this->post("/notificacoes/{$otherNotificationId}/ler")->assertNotFound();
});