-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add features test for category (Can create , Can create with pa…
…rent...)
- Loading branch information
Chri$
committed
Aug 31, 2024
1 parent
3e153a1
commit 7b1914b
Showing
2 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |