From df389b0f3498bb3512a4e4bfee363a5197d2bc7f Mon Sep 17 00:00:00 2001 From: Al Imran Ahmed Date: Wed, 7 Aug 2024 02:00:44 +0200 Subject: [PATCH] #183: Test coverage increased --- app/Livewire/Backend/Comment/Show.php | 1 - .../Livewire/Backend/Comment/ShowTest.php | 75 +++++++++++++++++++ .../Livewire/Backend/Keyword/IndexTest.php | 40 ++++++++++ .../Livewire/Backend/Subscriber/IndexTest.php | 18 +++++ 4 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/Livewire/Backend/Comment/ShowTest.php create mode 100644 tests/Feature/Livewire/Backend/Keyword/IndexTest.php create mode 100644 tests/Feature/Livewire/Backend/Subscriber/IndexTest.php diff --git a/app/Livewire/Backend/Comment/Show.php b/app/Livewire/Backend/Comment/Show.php index c60a400e..ba060a08 100644 --- a/app/Livewire/Backend/Comment/Show.php +++ b/app/Livewire/Backend/Comment/Show.php @@ -48,6 +48,5 @@ public function submitReply(): void public function delete(Comment $comment): void { $comment->delete(); - $this->comment->refresh(); } } diff --git a/tests/Feature/Livewire/Backend/Comment/ShowTest.php b/tests/Feature/Livewire/Backend/Comment/ShowTest.php new file mode 100644 index 00000000..3e28646f --- /dev/null +++ b/tests/Feature/Livewire/Backend/Comment/ShowTest.php @@ -0,0 +1,75 @@ +user = User::factory()->create(); + Auth::login($this->user); + + $this->article = Article::factory()->create([ + 'user_id' => $this->user->id, + ]); + $this->comment = Comment::factory()->create([ + 'article_id' => $this->article->id, + 'user_id' => $this->user->id, + ]); + } + + public function testRender() + { + Livewire::test(Show::class, ['comment' => $this->comment]) + ->assertOk() + ->assertViewIs('livewire.backend.comment.show'); + } + + public function testSubmitReply() + { + Livewire::test(Show::class, ['comment' => $this->comment]) + ->set('reply', [ + 'content' => $replyContent = $this->faker()->paragraph(), + ]) + ->call('submitReply') + ->assertOk() + ->assertHasNoErrors(); + + $this->assertDatabaseHas('comments', [ + 'parent_comment_id' => $this->comment->id, + 'article_id' => $this->article->id, + 'user_id' => $this->user->id, + 'content' => $replyContent, + ]); + } + + public function testDelete() + { + Livewire::test(Show::class, ['comment' => $this->comment]) + ->call('delete', $this->comment) + ->assertOk() + ->assertHasNoErrors(); + + $this->assertDatabaseMissing('comments', [ + 'id' => $this->comment->id, + ]); + } +} diff --git a/tests/Feature/Livewire/Backend/Keyword/IndexTest.php b/tests/Feature/Livewire/Backend/Keyword/IndexTest.php new file mode 100644 index 00000000..7c0c803f --- /dev/null +++ b/tests/Feature/Livewire/Backend/Keyword/IndexTest.php @@ -0,0 +1,40 @@ +assertOk() + ->assertViewIs('livewire.backend.keyword.index') + ->assertViewHas('keywords'); + } + + public function testDelete() + { + $keyword = Keyword::factory()->create(); + $article = Article::factory()->create(); + $article->keywords()->attach($keyword); + + Livewire::test(Index::class) + ->call('delete', $keyword) + ->assertOk() + ->assertHasNoErrors(); + + $this->assertDatabaseMissing('keywords', [ + 'id' => $keyword->id, + ]); + + $this->assertDatabaseHas('articles', [ + 'id' => $article->id, + ]); + } +} diff --git a/tests/Feature/Livewire/Backend/Subscriber/IndexTest.php b/tests/Feature/Livewire/Backend/Subscriber/IndexTest.php new file mode 100644 index 00000000..38d76798 --- /dev/null +++ b/tests/Feature/Livewire/Backend/Subscriber/IndexTest.php @@ -0,0 +1,18 @@ +assertOk() + ->assertViewIs('livewire.backend.subscriber.index') + ->assertViewHas('subscribers'); + } +}