Skip to content

Commit a5d84b7

Browse files
authored
Merge pull request #55 from kirschbaum-development/ISSUE-54
Refactor CommentList component to always utilize getComments method directly
2 parents 059b7d6 + 54f53f5 commit a5d84b7

File tree

8 files changed

+142
-24
lines changed

8 files changed

+142
-24
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
php: 8.4
1313
laravel: 12
1414
testbench: 10
15+
filament: 4
1516
larastan: 3
1617
pint: 1
1718

@@ -57,6 +58,7 @@ jobs:
5758
php: 8.4
5859
laravel: 12
5960
testbench: 10
61+
filament: 4
6062
larastan: 3
6163
pint: 1
6264

@@ -108,10 +110,12 @@ jobs:
108110
include:
109111
- laravel: 11
110112
testbench: 9
113+
filament: 4
111114
larastan: 3
112115
pint: 1
113116
- laravel: 12
114117
testbench: 10
118+
filament: 4
115119
larastan: 3
116120
pint: 1
117121

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ Sometimes you might want to render non-Comments in the list of comments. For exa
540540
```php
541541
use Kirschbaum\Commentions\RenderableComment;
542542
543-
public function getComments(): Collection
543+
public function getComments(?int $limit = null): Collection
544544
{
545545
$statusHistory = $this->statusHistory()->get()->map(fn (StatusHistory $statusHistory) => new RenderableComment(
546546
id: $statusHistory->id,
@@ -551,7 +551,13 @@ public function getComments(): Collection
551551
552552
$comments = $this->comments()->latest()->with('author')->get();
553553
554-
return $statusHistory->merge($comments);
554+
$mergedCollection = $statusHistory->merge($comments);
555+
556+
if ($limit) {
557+
return $mergedCollection->take($limit);
558+
}
559+
560+
return $mergedCollection;
555561
}
556562
```
557563

resources/dist/commentions.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/views/subscription-sidebar.blade.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ class="comm:w-4 comm:h-4 comm:text-gray-700 comm:dark:text-gray-300"
1818
wire:loading.attr="disabled"
1919
color="gray"
2020
size="xs"
21-
class="comm:w-full comm:mb-2"
21+
class="comm:w-full comm:mb-2 comm:inline-flex comm:items-center comm:whitespace-nowrap"
2222
>
23-
<x-filament::icon
24-
icon="heroicon-s-bell-slash"
25-
class="comm:w-3 comm:h-3 comm:mr-1"
26-
/>
27-
Unsubscribe
23+
<span class="comm:inline-flex comm:items-center comm:gap-1 comm:whitespace-nowrap">
24+
<x-filament::icon
25+
icon="heroicon-s-bell-slash"
26+
class="comm:w-3 comm:h-3 comm:flex-shrink-0"
27+
/>
28+
<span>Unsubscribe</span>
29+
</span>
2830
</x-filament::button>
2931
@else
3032
<x-filament::button
@@ -33,13 +35,15 @@ class="comm:w-3 comm:h-3 comm:mr-1"
3335
wire:loading.attr="disabled"
3436
color="gray"
3537
size="xs"
36-
class="comm:w-full comm:mb-2"
38+
class="comm:w-full comm:mb-2 comm:inline-flex comm:items-center comm:whitespace-nowrap"
3739
>
38-
<x-filament::icon
39-
icon="heroicon-o-bell"
40-
class="comm:w-3 comm:h-3 comm:mr-1"
41-
/>
42-
Subscribe
40+
<span class="comm:inline-flex comm:items-center comm:gap-1 comm:whitespace-nowrap">
41+
<x-filament::icon
42+
icon="heroicon-o-bell"
43+
class="comm:w-3 comm:h-3 comm:flex-shrink-0"
44+
/>
45+
<span>Subscribe</span>
46+
</span>
4347
</x-filament::button>
4448
@endif
4549

src/HasComments.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ public function comment(string $body, ?Commenter $author): Comment
2626
return SaveComment::run($this, $author, $body);
2727
}
2828

29-
public function getComments(): Collection
29+
public function getComments(?int $limit = null): Collection
3030
{
31+
if ($limit) {
32+
return $this->commentsQuery()->limit($limit)->get();
33+
}
34+
3135
return $this->commentsQuery()->get();
3236
}
3337

src/Livewire/CommentList.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ public function render()
2727
#[Computed]
2828
public function comments(): Collection
2929
{
30-
$query = $this->record->commentsQuery();
31-
32-
if (! $this->paginate) {
33-
return $query->get();
34-
}
35-
36-
return $query->limit($this->perPage)->get();
30+
return $this->record->getComments($this->paginate ? $this->perPage : null);
3731
}
3832

3933
#[On('comment:saved')]

tests/Livewire/CommentListTest.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
});

tests/TestCase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tests;
44

5+
use BladeUI\Heroicons\BladeHeroiconsServiceProvider;
6+
use BladeUI\Icons\BladeIconsServiceProvider;
57
use Filament\FilamentServiceProvider;
68
use Filament\Support\SupportServiceProvider;
79
use Illuminate\Database\Schema\Blueprint;
@@ -26,10 +28,12 @@ protected function setUp(): void
2628
protected function getPackageProviders($app)
2729
{
2830
return [
29-
LivewireServiceProvider::class,
31+
BladeIconsServiceProvider::class,
32+
BladeHeroiconsServiceProvider::class,
3033
CommentionsServiceProvider::class,
3134
FilamentServiceProvider::class,
3235
SupportServiceProvider::class,
36+
LivewireServiceProvider::class,
3337
];
3438
}
3539

0 commit comments

Comments
 (0)