|
143 | 143 | test()->assertDatabaseMissing('comments', ['id' => $reply->id]); |
144 | 144 | test()->assertDatabaseMissing('comments', ['id' => $nested->id]); |
145 | 145 | }); |
| 146 | + |
| 147 | +test('a top-level comment renders as a bordered card', function () { |
| 148 | + $user = User::factory()->create(); |
| 149 | + actingAs($user); |
| 150 | + |
| 151 | + $post = Post::factory()->create(); |
| 152 | + $comment = Comment::factory()->author($user)->commentable($post)->create(); |
| 153 | + |
| 154 | + livewire(CommentComponent::class, ['comment' => $comment, 'depth' => 0]) |
| 155 | + ->assertSeeHtml('comm:rounded-lg') |
| 156 | + ->assertSeeHtml('comm:shadow-sm') |
| 157 | + ->assertDontSeeHtml('commentions-thread'); |
| 158 | +}); |
| 159 | + |
| 160 | +test('a reply renders as a flat row with a thread connector', function () { |
| 161 | + $user = User::factory()->create(); |
| 162 | + actingAs($user); |
| 163 | + |
| 164 | + $post = Post::factory()->create(); |
| 165 | + $parent = Comment::factory()->author($user)->commentable($post)->create(); |
| 166 | + $reply = Comment::factory()->author($user)->commentable($post)->create(['parent_id' => $parent->id]); |
| 167 | + |
| 168 | + livewire(CommentComponent::class, ['comment' => $reply, 'depth' => 1]) |
| 169 | + ->assertDontSeeHtml('comm:shadow-sm') |
| 170 | + ->assertSeeHtml('comm:py-2') |
| 171 | + ->assertSeeHtml('comm:w-7') |
| 172 | + ->assertSeeHtml('class="commentions-thread"'); |
| 173 | +}); |
| 174 | + |
| 175 | +test('a comment with replies renders an accessible collapse toggle', function () { |
| 176 | + $user = User::factory()->create(); |
| 177 | + actingAs($user); |
| 178 | + |
| 179 | + $post = Post::factory()->create(); |
| 180 | + $parent = Comment::factory()->author($user)->commentable($post)->create(); |
| 181 | + Comment::factory()->author($user)->commentable($post)->create(['parent_id' => $parent->id]); |
| 182 | + |
| 183 | + livewire(CommentComponent::class, ['comment' => $parent, 'depth' => 0]) |
| 184 | + ->assertSeeHtml(':aria-expanded') |
| 185 | + ->assertSeeHtml('aria-controls="comment-replies-' . $parent->id . '"') |
| 186 | + ->assertSeeHtml('id="comment-replies-' . $parent->id . '"') |
| 187 | + ->assertSeeHtml('role="group"'); |
| 188 | +}); |
| 189 | + |
| 190 | +test('the collapse toggle shows the total descendant reply count', function () { |
| 191 | + $user = User::factory()->create(); |
| 192 | + actingAs($user); |
| 193 | + |
| 194 | + $post = Post::factory()->create(); |
| 195 | + $parent = Comment::factory()->author($user)->commentable($post)->create(); |
| 196 | + $child = Comment::factory()->author($user)->commentable($post)->create(['parent_id' => $parent->id]); |
| 197 | + Comment::factory()->author($user)->commentable($post)->create(['parent_id' => $child->id]); |
| 198 | + |
| 199 | + livewire(CommentComponent::class, ['comment' => $parent, 'depth' => 0]) |
| 200 | + ->assertSee('2 replies'); |
| 201 | +}); |
| 202 | + |
| 203 | +test('a comment with no replies renders no collapse toggle', function () { |
| 204 | + $user = User::factory()->create(); |
| 205 | + actingAs($user); |
| 206 | + |
| 207 | + $post = Post::factory()->create(); |
| 208 | + $comment = Comment::factory()->author($user)->commentable($post)->create(); |
| 209 | + |
| 210 | + livewire(CommentComponent::class, ['comment' => $comment, 'depth' => 0]) |
| 211 | + ->assertDontSeeHtml('aria-controls="comment-replies-'); |
| 212 | +}); |
| 213 | + |
| 214 | +test('replies indent for the first levels then stop', function () { |
| 215 | + config(['commentions.threading.max_depth' => 5]); |
| 216 | + |
| 217 | + $user = User::factory()->create(); |
| 218 | + actingAs($user); |
| 219 | + |
| 220 | + $post = Post::factory()->create(); |
| 221 | + $c0 = Comment::factory()->author($user)->commentable($post)->create(); |
| 222 | + $c1 = Comment::factory()->author($user)->commentable($post)->create(['parent_id' => $c0->id]); |
| 223 | + Comment::factory()->author($user)->commentable($post)->create(['parent_id' => $c1->id]); |
| 224 | + |
| 225 | + // A depth-0 wrapper indents the replies it renders. |
| 226 | + livewire(CommentComponent::class, ['comment' => $c0, 'depth' => 0]) |
| 227 | + ->assertSeeHtml('comm:pl-3'); |
| 228 | + |
| 229 | + // A wrapper at INDENT_CAP_DEPTH stops adding indent. |
| 230 | + livewire(CommentComponent::class, ['comment' => $c1, 'depth' => CommentComponent::INDENT_CAP_DEPTH]) |
| 231 | + ->assertDontSeeHtml('comm:pl-3'); |
| 232 | +}); |
| 233 | + |
| 234 | +test('repliesCount counts every descendant comment', function () { |
| 235 | + $user = User::factory()->create(); |
| 236 | + $post = Post::factory()->create(); |
| 237 | + |
| 238 | + $root = Comment::factory()->author($user)->commentable($post)->create(); |
| 239 | + $a = Comment::factory()->author($user)->commentable($post)->create(['parent_id' => $root->id]); |
| 240 | + Comment::factory()->author($user)->commentable($post)->create(['parent_id' => $a->id]); |
| 241 | + Comment::factory()->author($user)->commentable($post)->create(['parent_id' => $root->id]); |
| 242 | + |
| 243 | + expect($root->fresh()->repliesCount())->toBe(3); |
| 244 | +}); |
0 commit comments