Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/category test #289

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
mckenziearts marked this conversation as resolved.
Show resolved Hide resolved
});

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();
mckenziearts marked this conversation as resolved.
Show resolved Hide resolved
});

})->group('category');
Loading