跳到主要内容
版本:3.0

测试

概述

所有本指南内的示例都会使用 Pest 编写。不过,你可以轻松将其应用到 PHPUnit。

由于所有 Action 都挂载到 Livewire 组件,我们始终都在使用 Livewire 测试辅助函数。如果你之前从未测试过 Livewire 组件,请阅读 Livewire 文档下的这一向导

开始

将 Action 名或者类传入 callAction(),你可以调用 Action:

use function Pest\Livewire\livewire;

it('can send invoices', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->callAction('send');

expect($invoice->refresh())
->isSent()->toBeTrue();
});

要将数据数组传递给 Action,请使用 data 参数:

use function Pest\Livewire\livewire;

it('can send invoices', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->callAction('send', data: [
'email' => $email = fake()->email(),
])
->assertHasNoActionErrors();

expect($invoice->refresh())
->isSent()->toBeTrue()
->recipient_email->toBe($email);
});

如果你只需要设置 Action 的数据,而不用马上调用这些数据,你可以使用 setActionData()

use function Pest\Livewire\livewire;

it('can send invoices', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->mountAction('send')
->setActionData('send', data: [
'email' => $email = fake()->email(),
])
});

执行

要断言 Action 是否停止,你可以使用 assertActionHalted()

use function Pest\Livewire\livewire;

it('stops sending if invoice has no email address', function () {
$invoice = Invoice::factory(['email' => null])->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->callAction('send')
->assertActionHalted('send');
});

错误

assertHasNoActionErrors() 用于断言提交表单时,没有发生验证错误。 assertHasNoActionErrors() is used to assert that no validation errors occurred when submitting the action form.

要断言带有数据的验证错误,请使用 assertHasActionErrors(),类似于 Livewire 中的 assertHasErrors()

use function Pest\Livewire\livewire;

it('can validate invoice recipient email', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->callAction('send', data: [
'email' => Str::random(),
])
->assertHasActionErrors(['email' => ['email']]);
});

要断言 Action 已经预填数据,请使用 assertActionDataSet() 方法:

use function Pest\Livewire\livewire;

it('can send invoices to the primary contact by default', function () {
$invoice = Invoice::factory()->create();
$recipientEmail = $invoice->company->primaryContact->email;

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->mountAction('send')
->assertActionDataSet([
'email' => $recipientEmail,
])
->callMountedAction()
->assertHasNoActionErrors();

expect($invoice->refresh())
->isSent()->toBeTrue()
->recipient_email->toBe($recipientEmail);
});

Action 状态

使用 assertActionExists()assertActionDoesNotExist() 方法,你可以断言 Action 存在或不存在表格中:

use function Pest\Livewire\livewire;

it('can send but not unsend invoices', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->assertActionExists('send')
->assertActionDoesNotExist('unsend');
});

要断言 Action 对用户隐藏或可见,请使用 assertActionHidden()assertActionVisible() 方法:

use function Pest\Livewire\livewire;

it('can only print invoices', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->assertActionHidden('send')
->assertActionVisible('print');
});

要断言 Aciton 对用户启用或者禁用,请使用 assertActionEnabled()assertActionDisabled() 方法:

use function Pest\Livewire\livewire;

it('can only print a sent invoice', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->assertActionDisabled('send')
->assertActionEnabled('print');
});

要断言 Action 是否以正确的顺序存在,请使用 assertActionsExistInOrder()

use function Pest\Livewire\livewire;

it('can have actions in order', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->assertActionsExistInOrder(['send', 'export']);
});

要断言 Action 是否对用户隐藏,请使用 assertActionHidden() 方法:

use function Pest\Livewire\livewire;

it('can not send invoices', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->assertActionHidden('send');
});

Button 外观

要断言 Action 的标签正确,可以使用 assertActionHasLabel()assertActionDoesNotHaveLabel()

use function Pest\Livewire\livewire;

it('send action has correct label', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->assertActionHasLabel('send', 'Email Invoice')
->assertActionDoesNotHaveLabel('send', 'Send');
});

要断言 Action 按钮显示了正确的图标,可以使用 assertActionHasIcon()assertActionDoesNotHaveIcon()

use function Pest\Livewire\livewire;

it('when enabled the send button has correct icon', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->assertActionEnabled('send')
->assertActionHasIcon('send', 'envelope-open')
->assertActionDoesNotHaveIcon('send', 'envelope');
});

要断言 Action 按钮显示了正确的颜色,可以使用 assertActionHasColor()assertActionDoesNotHaveColor()

use function Pest\Livewire\livewire;

it('actions display proper colors', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->assertActionHasColor('delete', 'danger')
->assertActionDoesNotHaveColor('print', 'danger');
});

URL

要断言 Action 拥有正确的 URL,你可以使用 assertActionHasUrl()assertActionDoesNotHaveUrl()assertActionShouldOpenUrlInNewTab()assertActionShouldNotOpenUrlInNewTab()

use function Pest\Livewire\livewire;

it('links to the correct Filament sites', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->assertActionHasUrl('filament', 'https://filamentphp.com/')
->assertActionDoesNotHaveUrl('filament', 'https://github.com/filamentphp/filament')
->assertActionShouldOpenUrlInNewTab('filament')
->assertActionShouldNotOpenUrlInNewTab('github');
});