要用数据填充表单,请将数据传递给 fillForm():
use function Pest\Livewire\livewire;
livewire(CreatePost::class)
->fillForm([
'title' => fake()->sentence(),
// ...
]);如果你在一个 Livewire 组件上有多个模式,你可以指定你想要填充哪个表单,使用
fillForm([...], 'createPostForm')。
要检查表单是否有数据,请使用 assertSchemaStateSet():
use Illuminate\Support\Str;
use function Pest\Livewire\livewire;
it('can automatically generate a slug from the title', function () {
$title = fake()->sentence();
livewire(CreatePost::class)
->fillForm([
'title' => $title,
])
->assertSchemaStateSet([
'slug' => Str::slug($title),
]);
});如果您的 Livewire 组件上有多个模式, 您可以指定要检查哪个模式, 使用
assertSchemaStateSet([...], 'createPostForm').
您可能还会发现将一个函数传递给 assertSchemaStateSet() 方法很有用,这允许您访问表单的 $state 并执行额外的断言:
use Illuminate\Support\Str;
use function Pest\Livewire\livewire;
it('can automatically generate a slug from the title without any spaces', function () {
$title = fake()->sentence();
livewire(CreatePost::class)
->fillForm([
'title' => $title,
])
->assertSchemaStateSet(function (array $state): array {
expect($state['slug'])
->not->toContain(' ');
return [
'slug' => Str::slug($title),
];
});
});你可以从函数返回一个数组,如果你想让 Filament 在函数运行后继续断言架构状态。
使用 assertHasFormErrors() 以确保表单中的数据得到正确验证:
use function Pest\Livewire\livewire;
it('can validate input', function () {
livewire(CreatePost::class)
->fillForm([
'title' => null,
])
->call('create')
->assertHasFormErrors(['title' => 'required']);
});以及 assertHasNoFormErrors() 以确保没有验证错误:
use function Pest\Livewire\livewire;
livewire(CreatePost::class)
->fillForm([
'title' => fake()->sentence(),
// ...
])
->call('create')
->assertHasNoFormErrors();如果你的 Livewire 组件上有多个 schema,你可以将特定表单的名称作为第二个参数传递,例如
assertHasFormErrors(['title' => 'required'], 'createPostForm')或assertHasNoFormErrors([], 'createPostForm').
要检查 Livewire 组件是否具有表单,请使用 assertFormExists():
use function Pest\Livewire\livewire;
it('has a form', function () {
livewire(CreatePost::class)
->assertFormExists();
});如果你的 Livewire 组件上有多个模式,你可以传入特定表单的名称,例如
assertFormExists('createPostForm')。
为确保表单包含指定字段,将字段名传递给assertFormFieldExists():
use function Pest\Livewire\livewire;
it('has a title field', function () {
livewire(CreatePost::class)
->assertFormFieldExists('title');
});您可以将一个函数作为额外参数传递,以断言某个字段通过了给定的“真值测试”。这对于断言某个字段具有特定配置很有用:
use function Pest\Livewire\livewire;
it('has a title field', function () {
livewire(CreatePost::class)
->assertFormFieldExists('title', function (TextInput $field): bool {
return $field->isDisabled();
});
});为了断言表单不包含指定字段,将字段名传递给 assertFormFieldDoesNotExist():
use function Pest\Livewire\livewire;
it('does not have a conditional field', function () {
livewire(CreatePost::class)
->assertFormFieldDoesNotExist('no-such-field');
});如果您的 Livewire 组件上存在多个 schema,您可以指定要检查哪个表单中是否存在该字段,例如
assertFormFieldExists('title', 'createPostForm')。
为确保字段可见,将名称传递给 assertFormFieldVisible():
use function Pest\Livewire\livewire;
test('title is visible', function () {
livewire(CreatePost::class)
->assertFormFieldVisible('title');
});或者为了确保某个字段被隐藏,你可以将该名称传递给 assertFormFieldHidden():
use function Pest\Livewire\livewire;
test('title is hidden', function () {
livewire(CreatePost::class)
->assertFormFieldHidden('title');
});对于
assertFormFieldHidden()和assertFormFieldVisible()你可以将字段所属的特定表单的名称作为第二个参数传入 例如assertFormFieldHidden('title', 'createPostForm')。
为确保某个字段已启用, 将其名称传递给 assertFormFieldEnabled():
use function Pest\Livewire\livewire;
test('title is enabled', function () {
livewire(CreatePost::class)
->assertFormFieldEnabled('title');
});或者为了确保某个字段被禁用你可以将名称传递给 assertFormFieldDisabled():
use function Pest\Livewire\livewire;
test('title is disabled', function () {
livewire(CreatePost::class)
->assertFormFieldDisabled('title');
});对于
assertFormFieldEnabled()和assertFormFieldDisabled()你可以将字段所属的特定表单的名称作为第二个参数传入 就像assertFormFieldEnabled('title', 'createPostForm')。
如果您需要检查特定模式组件而非字段是否存在,您可以使用assertSchemaComponentExists()。由于组件没有名称,此方法使用开发者提供的key():
use Filament\Schemas\Components\Section;
Section::make('Comments')
->key('comments-section')
->schema([
//
])use function Pest\Livewire\livewire;
test('comments section exists', function () {
livewire(EditPost::class)
->assertSchemaComponentExists('comments-section');
});声明某个 schema 不包含给定组件时,请将组件键传递给assertSchemaComponentDoesNotExist():
use function Pest\Livewire\livewire;
it('does not have a conditional component', function () {
livewire(CreatePost::class)
->assertSchemaComponentDoesNotExist('no-such-section');
});要检查组件是否存在并符合给定的真值测试,你可以将一个函数传递给 assertSchemaComponentExists() 的 checkComponentUsing 参数,如果组件通过测试则返回 true,否则返回 false:
use Filament\Schemas\Components\Section;
use function Pest\Livewire\livewire;
test('comments section has heading', function () {
livewire(EditPost::class)
->assertSchemaComponentExists(
'comments-section',
checkComponentUsing: function (Section $component): bool {
return $component->getHeading() === 'Comments';
},
);
});如果你想要更具信息量的测试结果,你可以将断言嵌入到你的真值测试回调中:
use Filament\Schemas\Components\Section;
use Illuminate\Testing\Assert;
use function Pest\Livewire\livewire;
test('comments section is enabled', function () {
livewire(EditPost::class)
->assertSchemaComponentExists(
'comments-section',
checkComponentUsing: function (Section $component): bool {
Assert::assertTrue(
$component->isEnabled(),
'Failed asserting that comments-section is enabled.',
);
return true;
},
);
});在内部,中继器为项目生成 UUID,以便在 Livewire HTML 中更容易地跟踪它们。这意味着当你测试一个带有中继器的表单时,你需要确保表单和测试之间的 UUID 保持一致。这可能很棘手,如果你操作不当,你的测试可能会失败,因为测试期望的是一个 UUID,而不是一个数字键。
然而,由于 Livewire 在测试中不需要跟踪 UUID,您可以禁用 UUID 的生成并用数字键替换它们,通过在测试开始时使用 Repeater::fake() 方法:
use Filament\Forms\Components\Repeater;
use function Pest\Livewire\livewire;
$undoRepeaterFake = Repeater::fake();
livewire(EditPost::class, ['record' => $post])
->assertSchemaStateSet([
'quotes' => [
[
'content' => 'First quote',
],
[
'content' => 'Second quote',
],
],
// ...
]);
$undoRepeaterFake();你可能还会发现,通过将函数传递给 assertSchemaStateSet() 方法来测试重复器中的项目数量会很有用:
use Filament\Forms\Components\Repeater;
use function Pest\Livewire\livewire;
$undoRepeaterFake = Repeater::fake();
livewire(EditPost::class, ['record' => $post])
->assertSchemaStateSet(function (array $state) {
expect($state['quotes'])
->toHaveCount(2);
});
$undoRepeaterFake();为了测试中继器操作是否按预期工作,你可以利用 callFormComponentAction() 方法来调用你的中继器操作,然后执行额外的断言。
与特定中继器项目上的某个操作进行交互时,你需要传入 item 参数,并附带该中继器项目的键。如果你的中继器正在从关系中读取数据,你应该将相关记录的 ID(键)与 record- 前缀组合,以形成中继器项目的键:
use App\Models\Quote;
use Filament\Forms\Components\Repeater;
use function Pest\Livewire\livewire;
$quote = Quote::first();
livewire(EditPost::class, ['record' => $post])
->callAction(TestAction::make('sendQuote')->schemaComponent('quotes')->arguments([
'item' => "record-{$quote->getKey()}",
]))
->assertNotified('Quote sent!');在内部,构建器为项目生成 UUID,以便在 Livewire HTML 中更容易地跟踪它们。这意味着当你使用构建器测试表单时,你需要确保表单和测试之间的 UUID 是一致的。这可能很棘手,如果你没有正确地进行操作,你的测试可能会失败,因为测试期望的是 UUID,而不是数字键。
然而,由于 Livewire 在测试中无需跟踪 UUID,你可以禁用 UUID 生成并将其替换为数字键,在测试开始时使用 Builder::fake() 方法:
use Filament\Forms\Components\Builder;
use function Pest\Livewire\livewire;
$undoBuilderFake = Builder::fake();
livewire(EditPost::class, ['record' => $post])
->assertSchemaStateSet([
'content' => [
[
'type' => 'heading',
'data' => [
'content' => 'Hello, world!',
'level' => 'h1',
],
],
[
'type' => 'paragraph',
'data' => [
'content' => 'This is a test post.',
],
],
],
// ...
]);
$undoBuilderFake();您可能还会发现,通过将函数传递给 assertSchemaStateSet() 方法来访问测试重复器中的项目数量会很有用:
use Filament\Forms\Components\Builder;
use function Pest\Livewire\livewire;
$undoBuilderFake = Builder::fake();
livewire(EditPost::class, ['record' => $post])
->assertSchemaStateSet(function (array $state) {
expect($state['content'])
->toHaveCount(2);
});
$undoBuilderFake();要前往向导的下一步,使用 goToNextWizardStep():
use function Pest\Livewire\livewire;
it('moves to next wizard step', function () {
livewire(CreatePost::class)
->goToNextWizardStep()
->assertHasFormErrors(['title']);
});你还可以通过调用 goToPreviousWizardStep() 返回上一步:
use function Pest\Livewire\livewire;
it('moves to next wizard step', function () {
livewire(CreatePost::class)
->goToPreviousWizardStep()
->assertHasFormErrors(['title']);
});如果你想前往特定步骤,使用 goToWizardStep(),然后 assertWizardCurrentStep 方法可以确保你处于所需的步骤而不会出现来自上一步的验证错误:
use function Pest\Livewire\livewire;
it('moves to the wizards second step', function () {
livewire(CreatePost::class)
->goToWizardStep(2)
->assertWizardCurrentStep(2);
});如果你在一个 Livewire 组件上有多个模式,任何向导测试助手都可以接受一个 schema 参数:
use function Pest\Livewire\livewire;
it('moves to next wizard step only for fooForm', function () {
livewire(CreatePost::class)
->goToNextWizardStep(schema: 'fooForm')
->assertHasFormErrors(['title'], schema: 'fooForm');
});