mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Add form testing helpers
This commit is contained in:
@@ -2,8 +2,11 @@
|
||||
|
||||
namespace Filament\Forms;
|
||||
|
||||
use Filament\Forms\Testing\TestsForms;
|
||||
use Spatie\LaravelPackageTools\Package;
|
||||
use Spatie\LaravelPackageTools\PackageServiceProvider;
|
||||
use Livewire\Testing\TestableLivewire;
|
||||
|
||||
|
||||
class FormsServiceProvider extends PackageServiceProvider
|
||||
{
|
||||
@@ -39,4 +42,9 @@ class FormsServiceProvider extends PackageServiceProvider
|
||||
|
||||
return array_merge($commands, $aliases);
|
||||
}
|
||||
|
||||
public function packageBooted(): void
|
||||
{
|
||||
TestableLivewire::mixin(new TestsForms());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Forms\Testing;
|
||||
|
||||
use Closure;
|
||||
use Filament\Forms\ComponentContainer;
|
||||
use Filament\Forms\Components\Field;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Illuminate\Testing\Assert;
|
||||
use Livewire\Testing\TestableLivewire;
|
||||
|
||||
/**
|
||||
* @method HasForms instance()
|
||||
*
|
||||
* @mixin TestableLivewire
|
||||
*/
|
||||
class TestsForms
|
||||
{
|
||||
public function assertFormExists(): Closure
|
||||
{
|
||||
return function (string $name = 'form'): static {
|
||||
$livewire = $this->instance();
|
||||
$livewireClass = $livewire::class;
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $livewire->$name;
|
||||
|
||||
Assert::assertInstanceOf(
|
||||
ComponentContainer::class,
|
||||
$form,
|
||||
"Failed asserting that a form with the name [{$name}] exists on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormFieldExists(): Closure
|
||||
{
|
||||
return function (string $fieldName, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormExists($formName);
|
||||
|
||||
$livewire = $this->instance();
|
||||
$livewireClass = $livewire::class;
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $livewire->$formName;
|
||||
|
||||
/** @var ?Field $field */
|
||||
$field = data_get($form->getFlatFields(true), $fieldName, null);
|
||||
|
||||
Assert::assertInstanceOf(
|
||||
Field::class,
|
||||
$field,
|
||||
"Failed asserting that a field with the name [{$fieldName}] exists on the form with the name [${formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormFieldIsDisabled(): Closure
|
||||
{
|
||||
return function (string $fieldName, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormFieldExists($fieldName, $formName);
|
||||
|
||||
$livewire = $this->instance();
|
||||
$livewireClass = $livewire::class;
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $livewire->$formName;
|
||||
|
||||
/** @var Field $field */
|
||||
$field = $form->getFlatFields(true)[$fieldName];
|
||||
|
||||
Assert::assertTrue(
|
||||
$field->isDisabled(),
|
||||
"Failed asserting that a field with the name [{$fieldName}] is disabled on the form named [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormFieldIsEnabled(): Closure
|
||||
{
|
||||
return function (string $fieldName, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormFieldExists($fieldName, $formName);
|
||||
|
||||
$livewire = $this->instance();
|
||||
$livewireClass = $livewire::class;
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $livewire->$formName;
|
||||
|
||||
/** @var Field $field */
|
||||
$field = $form->getFlatFields(true)[$fieldName];
|
||||
|
||||
Assert::assertFalse(
|
||||
$field->isDisabled(),
|
||||
"Failed asserting that a field with the name [{$fieldName}] is enabled on the form named [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormFieldIsHidden(): Closure
|
||||
{
|
||||
return function (string $fieldName, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormFieldExists($fieldName, $formName);
|
||||
|
||||
$livewire = $this->instance();
|
||||
$livewireClass = $livewire::class;
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $livewire->$formName;
|
||||
|
||||
$fields = $form->getFlatFields();
|
||||
|
||||
Assert::assertArrayNotHasKey(
|
||||
$fieldName,
|
||||
$fields,
|
||||
"Failed asserting that a field with the name [{$fieldName}] is hidden on the form named [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormFieldIsVisible(): Closure
|
||||
{
|
||||
return function (string $fieldName, string $formName = 'form'): static {
|
||||
/** @phpstan-ignore-next-line */
|
||||
$this->assertFormFieldExists($fieldName, $formName);
|
||||
|
||||
$livewire = $this->instance();
|
||||
$livewireClass = $livewire::class;
|
||||
|
||||
/** @var ComponentContainer $form */
|
||||
$form = $livewire->$formName;
|
||||
|
||||
$fields = $form->getFlatFields(true);
|
||||
|
||||
$fields = $form->getFlatFields();
|
||||
|
||||
Assert::assertArrayHasKey(
|
||||
$fieldName,
|
||||
$fields,
|
||||
"Failed asserting that a field with the name [{$fieldName}] is hidden on the form named [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Tests\Forms\Fixtures\Livewire;
|
||||
use Filament\Tests\TestCase;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Str;
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
uses(TestCase::class);
|
||||
|
||||
it('has a form with the default name \'form\'', function() {
|
||||
livewire(TestComponentWithForm::class)
|
||||
->assertFormExists();
|
||||
})->only();
|
||||
|
||||
it('can have forms with non-default names', function() {
|
||||
livewire(TestComponentWithMultipleForms::class)
|
||||
->assertFormExists('fooForm')
|
||||
->assertFormExists('barForm');
|
||||
})->only();
|
||||
|
||||
it('has fields', function() {
|
||||
livewire(TestComponentWithForm::class)
|
||||
->assertFormFieldExists('title');
|
||||
})->only();
|
||||
|
||||
it('has fields on multiple forms', function() {
|
||||
livewire(TestComponentWithMultipleForms::class)
|
||||
->assertFormFieldExists('title', 'fooForm')
|
||||
->assertFormFieldExists('title', 'barForm');
|
||||
})->only();
|
||||
|
||||
it('can have disabled fields', function() {
|
||||
livewire(TestComponentWithForm::class)
|
||||
->assertFormFieldIsDisabled('disabled');
|
||||
})->only();
|
||||
|
||||
it('can have disabled fields on multiple forms', function() {
|
||||
livewire(TestComponentWithMultipleForms::class)
|
||||
->assertFormFieldIsDisabled('disabled', 'fooForm')
|
||||
->assertFormFieldIsDisabled('disabled', 'barForm');
|
||||
})->only();
|
||||
|
||||
it('can have enabled fields', function() {
|
||||
livewire(TestComponentWithForm::class)
|
||||
->assertFormFieldIsEnabled('enabled');
|
||||
})->only();
|
||||
|
||||
it('can have enabled fields on multiple forms', function() {
|
||||
livewire(TestComponentWithMultipleForms::class)
|
||||
->assertFormFieldIsEnabled('enabled', 'fooForm')
|
||||
->assertFormFieldIsEnabled('enabled', 'barForm');
|
||||
})->only();
|
||||
|
||||
it('can have hidden fields', function() {
|
||||
livewire(TestComponentWithForm::class)
|
||||
->assertFormFieldIsHidden('hidden');
|
||||
})->only();
|
||||
|
||||
it('can have hidden fields on multiple forms', function() {
|
||||
livewire(TestComponentWithMultipleForms::class)
|
||||
->assertFormFieldIsHidden('hidden', 'fooForm')
|
||||
->assertFormFieldIsHidden('hidden', 'barForm');
|
||||
})->only();
|
||||
|
||||
it('can have visible fields', function() {
|
||||
livewire(TestComponentWithForm::class)
|
||||
->assertFormFieldIsVisible('visible');
|
||||
})->only();
|
||||
|
||||
it('can have visible fields on multiple forms', function() {
|
||||
livewire(TestComponentWithMultipleForms::class)
|
||||
->assertFormFieldIsVisible('visible', 'fooForm')
|
||||
->assertFormFieldIsVisible('visible', 'barForm');
|
||||
})->only();
|
||||
|
||||
class TestComponentWithForm extends Livewire
|
||||
{
|
||||
public function getFormSchema(): array
|
||||
{
|
||||
return [
|
||||
TextInput::make('title'),
|
||||
|
||||
TextInput::make('disabled')
|
||||
->disabled(),
|
||||
|
||||
TextInput::make('enabled'),
|
||||
|
||||
TextInput::make('hidden')
|
||||
->hidden(),
|
||||
|
||||
TextInput::make('visible'),
|
||||
];
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('forms.fixtures.form');
|
||||
}
|
||||
}
|
||||
|
||||
class TestComponentWithMultipleForms extends Livewire
|
||||
{
|
||||
protected function getForms(): array
|
||||
{
|
||||
return [
|
||||
'fooForm' => $this->makeForm()
|
||||
->schema($this->getSchemaForForms()),
|
||||
|
||||
'barForm' => $this->makeForm()
|
||||
->schema($this->getSchemaForForms()),
|
||||
];
|
||||
}
|
||||
|
||||
private function getSchemaForForms(): array
|
||||
{
|
||||
return [
|
||||
TextInput::make('title'),
|
||||
|
||||
TextInput::make('disabled')
|
||||
->disabled(),
|
||||
|
||||
TextInput::make('enabled'),
|
||||
|
||||
TextInput::make('hidden')
|
||||
->hidden(),
|
||||
|
||||
TextInput::make('visible'),
|
||||
];
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('forms.fixtures.form');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user