Skip to content

Commit

Permalink
#183: More test written
Browse files Browse the repository at this point in the history
  • Loading branch information
alimranahmed committed Aug 4, 2024
1 parent 0e6088c commit 88d55e3
Show file tree
Hide file tree
Showing 11 changed files with 401 additions and 19 deletions.
3 changes: 2 additions & 1 deletion app/Livewire/Backend/Comment/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Comment;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Arr;
use Livewire\Component;

Expand All @@ -27,7 +28,7 @@ public function render(): View
return view('livewire.backend.comment.edit');
}

public function update(Comment $comment): RedirectResponse
public function update(Comment $comment): RedirectResponse|Redirector
{
$data = $this->validate();

Expand Down
1 change: 0 additions & 1 deletion database/factories/FeedbackFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/
class FeedbackFactory extends Factory
{

protected $model = Feedback::class;

/**
Expand Down
24 changes: 15 additions & 9 deletions tests/Feature/Livewire/Backend/Article/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Tests\Feature\Livewire\Backend\Article;

use App\Livewire\Backend\Article\Form;
use App\Livewire\Backend\Config\Index;
use App\Mail\NotifySubscriberForNewArticle;
use App\Models\Article;
use App\Models\Category;
use App\Models\Reader;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
Expand All @@ -17,7 +17,6 @@

class FormTest extends TestCase
{

public function testRender(): void
{
Livewire::test(Form::class)
Expand All @@ -26,14 +25,14 @@ public function testRender(): void
->assertViewHas('categories');
}

public function test_initializes_correctly_without_article()
public function testInitializesCorrectlyWithoutArticle()
{
Livewire::test(Form::class)
->assertSet('method', 'post')
->assertSet('articleData', []);
}

public function test_initializes_correctly_with_article()
public function testInitializesCorrectlyWithArticle()
{
$article = Article::factory()->create();
Livewire::test(Form::class, ['article' => $article])
Expand All @@ -43,15 +42,15 @@ public function test_initializes_correctly_with_article()
->assertSet('articleData.category_id', $article->category_id);
}

public function test_generates_slug_when_heading_changes()
public function testGeneratesSlugWhenHeadingChanges()
{
Livewire::test(Form::class)
->set('articleData.heading', 'New Article')
->set('articleData.language', 'en')
->assertSet('articleData.slug', Str::slug('New Article'));
}

public function test_validates_article_data_correctly()
public function testValidatesArticleDataCorrectly()
{
Livewire::test(Form::class)
->set('articleData.heading', '')
Expand All @@ -69,11 +68,18 @@ public function test_validates_article_data_correctly()
]);
}

public function test_stores_a_new_article_correctly()
public function testStoresANewArticleCorrectly()
{
Mail::fake();
Auth::loginUsingId(User::factory()->create()->id);

$user = User::factory()->create();
Reader::query()->create([
'user_id' => $user->id,
'notify' => true,
'is_verified' => true,
]);

$category = Category::factory()->create();
$data = [
'heading' => 'Test Article',
Expand All @@ -100,7 +106,7 @@ public function test_stores_a_new_article_correctly()
Mail::assertQueued(NotifySubscriberForNewArticle::class);
}

public function test_updates_an_existing_article_correctly()
public function testUpdatesAnExistingArticleCorrectly()
{
Mail::fake();
Auth::loginUsingId(User::factory()->create()->id);
Expand All @@ -127,7 +133,7 @@ public function test_updates_an_existing_article_correctly()

$this->assertDatabaseHas('articles', [
'id' => $article->id,
'heading' => 'Updated Article'
'heading' => 'Updated Article',
]);
$this->assertDatabaseHas('keywords', ['name' => 'updated']);
$this->assertDatabaseHas('keywords', ['name' => 'article']);
Expand Down
74 changes: 74 additions & 0 deletions tests/Feature/Livewire/Backend/Article/IndexRowTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Tests\Feature\Livewire\Backend\Article;

use App\Livewire\Backend\Article\Index;
use App\Livewire\Backend\Article\IndexRow;
use App\Models\Article;
use App\Models\User;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Livewire\Livewire;
use Spatie\Permission\Models\Role;
use Tests\TestCase;

class IndexRowTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
$user = User::factory()->create();
$author = Role::findOrCreate('author');
$user->assignRole($author);
Auth::loginUsingId($user->id);
}

public function testRender(): void
{
$article = Article::factory()->create();

Livewire::test(IndexRow::class, ['article' => $article])
->assertStatus(Response::HTTP_OK)
->assertViewIs('livewire.backend.article.index-row');
}

public function testTogglePublishCanMakeUnpublish()
{
$article = Article::factory()->create(['is_published' => true]);

Livewire::test(IndexRow::class, ['article' => $article])
->call('togglePublish');

$this->assertDatabaseHas('articles', [
'id' => $article->id,
'is_published' => false,
]);
}

public function testTogglePublishCanMakePublish()
{
$article = Article::factory()->create(['is_published' => false]);

Livewire::test(IndexRow::class, ['article' => $article])
->call('togglePublish');

$this->assertDatabaseHas('articles', [
'id' => $article->id,
'is_published' => true,
]);
}

public function testDestroy()
{
$article = Article::factory()->create(['is_deleted' => false]);

Livewire::test(IndexRow::class, ['article' => $article])
->call('destroy')
->assertDispatchedTo(Index::class, 'articleDeleted');

$this->assertDatabaseHas('articles', [
'id' => $article->id,
'is_deleted' => true,
]);
}
}
9 changes: 4 additions & 5 deletions tests/Feature/Livewire/Backend/Article/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ public function setUp(): void
$user->assignRole($author);
Auth::loginUsingId($user->id);
}

public function testItRendersCorrectly()
{
Auth::loginUsingId(User::factory()->create()->id);

Livewire::test(Index::class)
->assertStatus(200)
->assertViewIs('livewire.backend.article.index');
Expand All @@ -41,7 +40,7 @@ public function testItFiltersArticlesBasedOnCategory()
->set('category', $category->id)
->assertSeeInOrder([$category->name], 'category')
->assertViewHas('articles', function ($articles) use ($category) {
return $articles->every(fn($article) => $article->category_id === $category->id);
return $articles->every(fn ($article) => $article->category_id === $category->id);
});
}

Expand All @@ -57,7 +56,7 @@ public function testItFiltersArticlesBasedOnKeyword()
->set('keyword', $keyword->name)
->assertSeeInOrder([$keyword->name], 'keywords')
->assertViewHas('articles', function ($articles) use ($keyword) {
return $articles->every(fn($article) => $article->keywords->contains($keyword));
return $articles->every(fn ($article) => $article->keywords->contains($keyword));
});
}

Expand All @@ -70,7 +69,7 @@ public function testItFiltersArticlesBasedOnSearchQuery()
Livewire::test(Index::class)
->set('query', $query)
->assertViewHas('articles', function ($articles) use ($query) {
return $articles->every(fn($article) => stripos($article->heading, $query) !== false);
return $articles->every(fn ($article) => stripos($article->heading, $query) !== false);
});
}
}
90 changes: 90 additions & 0 deletions tests/Feature/Livewire/Backend/Category/IndexRowTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Tests\Feature\Livewire\Backend\Category;

use App\Livewire\Backend\Category\IndexRow;
use App\Models\Category;
use Illuminate\Support\Str;
use Livewire\Livewire;
use Tests\TestCase;

class IndexRowTest extends TestCase
{
public function testRender()
{
$category = Category::factory()->create();

Livewire::test(IndexRow::class, ['category' => $category])
->assertViewIs('livewire.backend.category.index-row');
}

public function testToggleActiveCanActivate()
{
$category = Category::factory()->create(['is_active' => false]);

Livewire::test(IndexRow::class, ['category' => $category])
->call('toggleActive')
->assertHasNoErrors();

$this->assertDatabaseHas('categories', [
'id' => $category->id,
'is_active' => true,
]);
}

public function testToggleActiveCanDeactivate()
{
$category = Category::factory()->create(['is_active' => true]);

Livewire::test(IndexRow::class, ['category' => $category])
->call('toggleActive')
->assertHasNoErrors();

$this->assertDatabaseHas('categories', [
'id' => $category->id,
'is_active' => false,
]);
}

public function testDestroy()
{
$category = Category::factory()->create(['is_active' => true]);

Livewire::test(IndexRow::class, ['category' => $category])
->call('destroy', $category)
->assertHasNoErrors();

$this->assertDatabaseMissing('categories', [
'id' => $category->id,
]);
}

public function testStartEditing()
{
$category = Category::factory()->create(['is_active' => true]);

Livewire::test(IndexRow::class, ['category' => $category])
->call('startEditing')
->assertHasNoErrors()
->assertSet('editing', true);
}

public function testUpdate()
{
$category = Category::factory()->create(['is_active' => true]);
Livewire::test(IndexRow::class, ['category' => $category])
->set('editing', true)
->set('categoryData', [
'name' => $name = Str::random(),
'alias' => $alias = Str::random(),
])
->call('update')
->assertSet('editing', false);

$this->assertDatabaseHas('categories', [
'id' => $category->id,
'name' => $name,
'alias' => $alias,
]);
}
}
43 changes: 43 additions & 0 deletions tests/Feature/Livewire/Backend/Category/IndexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Tests\Feature\Livewire\Backend\Category;

use App\Livewire\Backend\Category\Index;
use Illuminate\Http\Response;
use Illuminate\Support\Str;
use Livewire\Livewire;
use Tests\TestCase;

class IndexTest extends TestCase
{
public function testRender(): void
{
Livewire::test(Index::class)
->assertViewIs('livewire.backend.category.index')
->assertViewHas('categories')
->assertStatus(Response::HTTP_OK);
}

public function testStartAdding()
{
Livewire::test(Index::class)
->set('adding', false)
->call('startAdding')
->assertSet('adding', true);
}

public function testStore()
{
Livewire::test(Index::class)
->set('category', [
'name' => $name = Str::random(),
'alias' => $alias = Str::random(),
])
->call('store');

$this->assertDatabaseHas('categories', [
'name' => $name,
'alias' => $alias,
]);
}
}
Loading

0 comments on commit 88d55e3

Please sign in to comment.