Files
relaticle-comments/tests/Feature/CommentEventsTest.php
manukminasyan 29fcbd8aec feat: initial release of relaticle/comments
Filament comments package with:
- Polymorphic commenting on any Eloquent model
- Threaded replies with configurable depth
- @mentions with autocomplete and user search
- Emoji reactions with toggle and who-reacted tooltips
- File attachments via Livewire uploads
- Reply and mention notifications via Filament notification system
- Thread subscriptions for notification control
- Real-time broadcasting (opt-in Echo) with polling fallback
- Dark mode support
- CommentsAction, CommentsTableAction, CommentsEntry for Filament integration
- 204 tests, 421 assertions
2026-03-26 23:02:56 +04:00

125 lines
3.9 KiB
PHP

<?php
use Illuminate\Support\Facades\Event;
use Livewire\Livewire;
use Relaticle\Comments\Comment;
use Relaticle\Comments\Events\CommentCreated;
use Relaticle\Comments\Events\CommentDeleted;
use Relaticle\Comments\Events\CommentUpdated;
use Relaticle\Comments\Livewire\CommentItem;
use Relaticle\Comments\Livewire\Comments;
use Relaticle\Comments\Tests\Models\Post;
use Relaticle\Comments\Tests\Models\User;
it('fires CommentCreated event when adding a comment', function () {
Event::fake([CommentCreated::class]);
$user = User::factory()->create();
$post = Post::factory()->create();
$this->actingAs($user);
Livewire::test(Comments::class, ['model' => $post])
->set('newComment', '<p>New comment</p>')
->call('addComment');
Event::assertDispatched(CommentCreated::class, function (CommentCreated $event) use ($post) {
return $event->comment->body === '<p>New comment</p>'
&& $event->commentable->id === $post->id;
});
});
it('fires CommentUpdated event when editing a comment', function () {
Event::fake([CommentUpdated::class]);
$user = User::factory()->create();
$post = Post::factory()->create();
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
'body' => '<p>Original</p>',
]);
$this->actingAs($user);
Livewire::test(CommentItem::class, ['comment' => $comment])
->call('startEdit')
->set('editBody', '<p>Edited</p>')
->call('saveEdit');
Event::assertDispatched(CommentUpdated::class, function (CommentUpdated $event) use ($comment) {
return $event->comment->id === $comment->id;
});
});
it('fires CommentDeleted event when deleting a comment', function () {
Event::fake([CommentDeleted::class]);
$user = User::factory()->create();
$post = Post::factory()->create();
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
Livewire::test(CommentItem::class, ['comment' => $comment])
->call('deleteComment');
Event::assertDispatched(CommentDeleted::class, function (CommentDeleted $event) use ($comment) {
return $event->comment->id === $comment->id;
});
});
it('fires CommentCreated event when adding a reply', function () {
Event::fake([CommentCreated::class]);
$user = User::factory()->create();
$post = Post::factory()->create();
$comment = Comment::factory()->create([
'commentable_id' => $post->id,
'commentable_type' => $post->getMorphClass(),
'user_id' => $user->getKey(),
'user_type' => $user->getMorphClass(),
]);
$this->actingAs($user);
Livewire::test(CommentItem::class, ['comment' => $comment])
->call('startReply')
->set('replyBody', '<p>Reply text</p>')
->call('addReply');
Event::assertDispatched(CommentCreated::class, function (CommentCreated $event) use ($comment) {
return $event->comment->parent_id === $comment->id
&& $event->comment->body === '<p>Reply text</p>';
});
});
it('carries correct comment and commentable in event payload', function () {
Event::fake([CommentCreated::class]);
$user = User::factory()->create();
$post = Post::factory()->create();
$this->actingAs($user);
Livewire::test(Comments::class, ['model' => $post])
->set('newComment', '<p>Payload test</p>')
->call('addComment');
Event::assertDispatched(CommentCreated::class, function (CommentCreated $event) use ($post, $user) {
return $event->comment instanceof Comment
&& $event->commentable->id === $post->id
&& $event->comment->user_id === $user->id;
});
});