Actions
测试
概述
所有本指南内的示例都会使用 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');});
模态框内容
要断言模态的内容,你应该首先挂载 Action(而不是调用它来关闭模态)。然后,可以使用 Livewire 断言例如 assertSee()
来断言模态框包含期望的内容:
use function Pest\Livewire\livewire; it('confirms the target address before sending', function () { $invoice = Invoice::factory()->create(); $recipientEmail = $invoice->company->primaryContact->email; livewire(EditInvoice::class, [ 'invoice' => $invoice, ]) ->mountAction('send') ->assertSee($recipientEmail);});
错误
assertHasNoActionErrors()
用于断言提交表单时,没有发生验证错误。
要断言带有数据的验证错误,请使用 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');});
Edit on GitHubStill need help? Join our Discord community or open a GitHub discussion