Skip to content

Commit

Permalink
test: Add features test for category (Can create , Can create with pa…
Browse files Browse the repository at this point in the history
…rent...)
  • Loading branch information
Chri$ committed Aug 31, 2024
1 parent 3e153a1 commit 7b1914b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/database/factories/CategoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function definition(): array
'name' => $name = $this->faker->unique()->words(3, true),
'slug' => Str::slug($name),
'description' => $this->faker->realText(),
'is_visible' => $this->faker->boolean(),
'is_enabled' => $this->faker->boolean(),
'created_at' => $this->faker->dateTimeBetween('-1 year', '-6 month'),
'updated_at' => $this->faker->dateTimeBetween('-5 month'),
];
Expand Down
90 changes: 90 additions & 0 deletions tests/src/Admin/Features/CategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

use Shopper\Core\Models\Category;
use Shopper\Core\Repositories\CategoryRepository;
use Shopper\Livewire\Pages;
use Shopper\Livewire\SlideOvers\CategoryForm;
use Shopper\Tests\Admin\Features\TestCase;

use function Pest\Laravel\get;
use function Pest\Livewire\livewire;

uses(TestCase::class);

describe('Category', function (): void {
it('can render categories page', function (): void {
get(route('shopper.categories.index'))
->assertFound();

livewire(Pages\Category\Index::class)
->assertSee(__('shopper::pages/categories.menu'));
});

it('can validate `required` fields on add categorie form', function (): void {
livewire(CategoryForm::class)
->assertFormExists()
->fillForm([])
->call('save')
->assertHasFormErrors(['name' => 'required']);
});

it('can create a categorie', function (): void {
livewire(CategoryForm::class)
->assertFormExists()
->fillForm([
'name' => 'My new Category',
])
->call('save')
->assertHasNoFormErrors()
->assertDispatched('category-save');

expect((new CategoryRepository)->count())->toBe(1);
});

it('will generate a slug when brand slug already exists', function (): void {
Category::factory()->create(['name' => 'Old category', 'slug' => 'my-first-category']);

livewire(CategoryForm::class)
->assertFormExists()
->fillForm([
'name' => 'My first category',
])
->call('save')
->assertDispatched('category-save');

expect((new CategoryRepository)->count())
->toBe(2)
->and((new CategoryRepository)->getById(2)?->slug)
->toBe('my-first-category-1');
});

it('can create category with parent', function (): void {
$parent = Category::factory()->create(['name' => 'Parent']);

livewire(CategoryForm::class)
->assertFormExists()
->fillForm([
'name' => 'My new Category',
'parent_id' => $parent->id,
])
->call('save')
->assertHasNoFormErrors()
->assertDispatched('category-save');
expect((new CategoryRepository)->count())->toBe(2);
});

it('can set parent_id child to null if parent are deleted', function (): void {
$parent = Category::factory()->create(['name' => 'Parent']);
$child = Category::factory()->create(['name' => 'Enfant', 'parent_id' => $parent->id]);

expect($child->parent_id)->toBe($parent->id);

$parent->delete();

$child->refresh();
expect($child->parent_id)->toBeNull();
});

})->group('category');

0 comments on commit 7b1914b

Please sign in to comment.