ledgerrz/tests/Browser/NotificationTest.php
2026-07-06 13:27:52 +02:00

79 lines
3.3 KiB
PHP

<?php
namespace Tests\Browser;
use App\Models\Dynamic;
use App\Models\Ledger;
use App\Models\Mutation;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
class NotificationTest extends DuskTestCase
{
use DatabaseMigrations;
protected function setUp(): void
{
parent::setUp();
$this->artisan('migrate:fresh', ['--seed' => true]);
}
/**
* A basic browser test example.
*/
public function test_it_shows_notifications_on_mutation_activity(): void
{
$owner = User::factory()->create();
$participant = User::factory()->create();
$dynamic = Dynamic::factory()->create();
$dynamic->chat()->create();
$dynamic->participants()->attach($owner->id, ['role' => 'owner']);
$dynamic->participants()->attach($participant->id, ['role' => 'participant']);
$ledger = Ledger::factory()->create(['dynamic_id' => $dynamic->id]);
$this->browse(function (Browser $ownerBrowser, Browser $participantBrowser) use ($owner, $participant, $dynamic, $ledger) {
$ownerBrowser->loginAs($owner)
->visit(route('dynamics.ledgers.show', [$dynamic, $ledger]))
->waitForText($ledger->name)
->assertSee($ledger->name)
->script([
"window.notifications = [];",
"window.Notification = function(title, options) { window.notifications.push({title, options}); };",
]);
$participantBrowser->loginAs($participant)
->visit(route('dynamics.ledgers.show', [$dynamic, $ledger]))
->waitForText('Add Mutation')
->type('[data-test="description-input"]', 'A new task suggestion')
->type('[data-test="amount-input"]', 10)
->press('[data-test="add-mutation-button"]')
->waitForText('A new task suggestion');
$ownerBrowser->pause(1000);
$lastNotification = $ownerBrowser->script("return window.notifications.pop();")[0] ?? null;
$this->assertNotNull($lastNotification, "Owner did not receive the 'new suggestion' notification.");
$this->assertEquals('New Activity', $lastNotification['title']);
$this->assertStringContainsString('suggested a new entry', $lastNotification['options']['body']);
$mutation = Mutation::where('description', 'A new task suggestion')->first();
$ownerBrowser->press("#mutation-{$mutation->id}-approve")
->waitForText('APPROVED');
$participantBrowser->script([
"window.notifications = [];",
"window.Notification = function(title, options) { window.notifications.push({title, options}); };",
]);
$ownerBrowser->pause(1000);
$participantBrowser->pause(1000);
$lastNotification = $participantBrowser->script("return window.notifications.pop();")[0] ?? null;
$this->assertNotNull($lastNotification, "Participant did not receive the 'approved' notification.");
$this->assertEquals('New Activity', $lastNotification['title']);
$this->assertStringContainsString('APPROVED the suggestion', $lastNotification['options']['body']);
});
}
}