|
| 1 | +<?php |
| 2 | + |
| 3 | +use Kirschbaum\Commentions\Comment as CommentModel; |
| 4 | +use Kirschbaum\Commentions\Livewire\CommentList; |
| 5 | +use Kirschbaum\Commentions\RenderableComment; |
| 6 | +use Mockery; |
| 7 | +use Tests\Models\Post; |
| 8 | +use Tests\Models\User; |
| 9 | + |
| 10 | +use function Pest\Livewire\livewire; |
| 11 | + |
| 12 | +test('CommentList calls getComments when not paginating', function () { |
| 13 | + /** @var Post|Mockery\MockInterface $post */ |
| 14 | + $post = Mockery::mock(Post::class)->makePartial(); |
| 15 | + |
| 16 | + $post->shouldReceive('getComments') |
| 17 | + ->once() |
| 18 | + ->andReturn(collect()); |
| 19 | + |
| 20 | + $component = livewire(CommentList::class, [ |
| 21 | + 'record' => $post, |
| 22 | + 'paginate' => false, |
| 23 | + ]); |
| 24 | + |
| 25 | + $component->get('comments'); |
| 26 | +}); |
| 27 | + |
| 28 | +test('CommentList calls getComments when paginating', function () { |
| 29 | + /** @var Post|Mockery\MockInterface $post */ |
| 30 | + $post = Mockery::mock(Post::class)->makePartial(); |
| 31 | + |
| 32 | + $post->shouldReceive('getComments') |
| 33 | + ->once() |
| 34 | + ->andReturn(collect()); |
| 35 | + |
| 36 | + $component = livewire(CommentList::class, [ |
| 37 | + 'record' => $post, |
| 38 | + 'paginate' => true, |
| 39 | + 'perPage' => 5, |
| 40 | + ]); |
| 41 | + |
| 42 | + $component->get('comments'); |
| 43 | +}); |
| 44 | + |
| 45 | +test('CommentList can render non-Comment renderable items', function () { |
| 46 | + /** @var Post|Mockery\MockInterface $post */ |
| 47 | + $post = Mockery::mock(Post::class)->makePartial(); |
| 48 | + |
| 49 | + $items = collect([ |
| 50 | + new RenderableComment(id: 1, authorName: 'System', body: 'System notice 1'), |
| 51 | + new RenderableComment(id: 2, authorName: 'Bot', body: 'Automated message'), |
| 52 | + ]); |
| 53 | + |
| 54 | + $post->shouldReceive('getComments') |
| 55 | + ->once() |
| 56 | + ->andReturn($items); |
| 57 | + |
| 58 | + livewire(CommentList::class, [ |
| 59 | + 'record' => $post, |
| 60 | + 'paginate' => false, |
| 61 | + ]) |
| 62 | + ->assertSee('System') |
| 63 | + ->assertSee('System notice 1') |
| 64 | + ->assertSee('Bot') |
| 65 | + ->assertSee('Automated message'); |
| 66 | +}); |
| 67 | + |
| 68 | +test('CommentList can render both Comment and RenderableComment items', function () { |
| 69 | + /** @var User $user */ |
| 70 | + $user = User::factory()->create(); |
| 71 | + /** @var Post $realPost */ |
| 72 | + $realPost = Post::factory()->create(); |
| 73 | + |
| 74 | + /** @var CommentModel $comment */ |
| 75 | + $comment = CommentModel::factory() |
| 76 | + ->author($user) |
| 77 | + ->commentable($realPost) |
| 78 | + ->create([ |
| 79 | + 'body' => 'Real comment body', |
| 80 | + ]); |
| 81 | + |
| 82 | + $renderable = new RenderableComment(id: 99, authorName: 'System', body: 'System message'); |
| 83 | + |
| 84 | + $items = collect([$comment, $renderable]); |
| 85 | + |
| 86 | + /** @var Post|Mockery\MockInterface $post */ |
| 87 | + $post = Mockery::mock(Post::class)->makePartial(); |
| 88 | + $post->shouldReceive('getComments') |
| 89 | + ->once() |
| 90 | + ->andReturn($items); |
| 91 | + |
| 92 | + livewire(CommentList::class, [ |
| 93 | + 'record' => $post, |
| 94 | + 'paginate' => false, |
| 95 | + ]) |
| 96 | + // From Eloquent Comment |
| 97 | + ->assertSee('Real comment body') |
| 98 | + ->assertSee($user->name) |
| 99 | + // From RenderableComment |
| 100 | + ->assertSee('System') |
| 101 | + ->assertSee('System message'); |
| 102 | +}); |
0 commit comments