create(); $post = Post::factory()->create(); $this->actingAs($user); Livewire::test(Comments::class, ['model' => $post]) ->set('newComment', '

New comment

') ->call('addComment'); Event::assertDispatched(CommentCreated::class, function (CommentCreated $event) use ($post) { return $event->comment->body === '

New comment

' && $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(), 'commenter_id' => $user->getKey(), 'commenter_type' => $user->getMorphClass(), 'body' => '

Original

', ]); $this->actingAs($user); Livewire::test(CommentItem::class, ['comment' => $comment]) ->call('startEdit') ->set('editBody', '

Edited

') ->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(), 'commenter_id' => $user->getKey(), 'commenter_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(), 'commenter_id' => $user->getKey(), 'commenter_type' => $user->getMorphClass(), ]); $this->actingAs($user); Livewire::test(CommentItem::class, ['comment' => $comment]) ->call('startReply') ->set('replyBody', '

Reply text

') ->call('addReply'); Event::assertDispatched(CommentCreated::class, function (CommentCreated $event) use ($comment) { return $event->comment->parent_id === $comment->id && $event->comment->body === '

Reply text

'; }); }); 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', '

Payload test

') ->call('addComment'); Event::assertDispatched(CommentCreated::class, function (CommentCreated $event) use ($post, $user) { return $event->comment instanceof Comment && $event->commentable->id === $post->id && $event->comment->commenter_id === $user->id; }); });