信息列表
测试
概述
所有本指南内的示例都使用 Pest 编写。要使用 Pest 的 Livewire 插件进行测试,请参考 Pest 文档中的插件安装说明:Pest 的 Livewire 插件。不过,你也可以将其适配到 PHPUnit。
由于信息列表的所有页面都是 Livewire 组件,我们始终都在使用 Livewire 测试辅助函数。如果你之前从未测试过 Livewire 组件,请阅读 Livewire 文档下的这一向导。
Action
你可以将信息列表组件的键名以及 Action 名传递给 callInfolistAction()
方法以调用一个 Action:
use function Pest\Livewire\livewire; it('can send invoices', function () { $invoice = Invoice::factory()->create(); livewire(EditInvoice::class, [ 'invoice' => $invoice, ]) ->callInfolistAction('customer', 'send', infolistName: 'infolist'); 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, ]) ->callInfolistAction('customer', 'send', data: [ 'email' => $email = fake()->email(), ]) ->assertHasNoInfolistActionErrors(); expect($invoice->refresh()) ->isSent()->toBeTrue() ->recipient_email->toBe($email);});
如果你只是要设置 Action 的数据,而不必马上调用,可以使用 setInfolistActionData()
:
use function Pest\Livewire\livewire; it('can send invoices', function () { $invoice = Invoice::factory()->create(); livewire(EditInvoice::class, [ 'invoice' => $invoice, ]) ->mountInfolistAction('customer', 'send') ->setInfolistActionData('customer', 'send', data: [ 'email' => $email = fake()->email(), ])});
执行
使用 assertInfolistActionHalted()
可以断言 Action 是否已被中止:
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, ]) ->callInfolistAction('customer', 'send') ->assertInfolistActionHalted('customer', 'send');});
错误
assertHasNoInfolistActionErrors()
用于断言 Aciton 表单提交时没有出现验证错误。
要断言使用数据时出现验证错误,请使用 assertHasInfolistActionErrors()
,类似于 Livewire 中的 assertHasErrors()
:
use function Pest\Livewire\livewire; it('can validate invoice recipient email', function () { $invoice = Invoice::factory()->create(); livewire(EditInvoice::class, [ 'invoice' => $invoice, ]) ->callInfolistAction('customer', 'send', data: [ 'email' => Str::random(), ]) ->assertHasInfolistActionErrors(['email' => ['email']]);});
使用 assertInfolistActionDataSet()
方法,可以断言 Action 是否预填充了数据:
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, ]) ->mountInfolistAction('customer', 'send') ->assertInfolistActionDataSet([ 'email' => $recipientEmail, ]) ->callMountedInfolistAction() ->assertHasNoInfolistActionErrors(); expect($invoice->refresh()) ->isSent()->toBeTrue() ->recipient_email->toBe($recipientEmail);});
Action 状态
要断言 Action 存在于或者不在信息列表中中,可以使用 assertInfolistActionExists()
或 assertInfolistActionDoesNotExist()
方法:
use function Pest\Livewire\livewire; it('can send but not unsend invoices', function () { $invoice = Invoice::factory()->create(); livewire(EditInvoice::class, [ 'invoice' => $invoice, ]) ->assertInfolistActionExists('customer', 'send') ->assertInfolistActionDoesNotExist('customer', 'unsend');});
要断言 Action 对用户隐藏或者可见,可以使用 assertInfolistActionHidden()
或 assertInfolistActionVisible()
方法:
use function Pest\Livewire\livewire; it('can only print customers', function () { $invoice = Invoice::factory()->create(); livewire(EditInvoice::class, [ 'invoice' => $invoice, ]) ->assertInfolistActionHidden('customer', 'send') ->assertInfolistActionVisible('customer', 'print');});
要断言 Action 是对用户启用还是禁用,可以使用 assertInfolistActionEnabled()
或 assertInfolistActionDisabled()
方法:
use function Pest\Livewire\livewire; it('can only print a customer for a sent invoice', function () { $invoice = Invoice::factory()->create(); livewire(EditInvoice::class, [ 'invoice' => $invoice, ]) ->assertInfolistActionDisabled('customer', 'send') ->assertInfolistActionEnabled('customer', 'print');});
要断言 Action 对某个用户隐藏,可以使用 assertInfolistActionHidden()
方法:
use function Pest\Livewire\livewire; it('can not send invoices', function () { $invoice = Invoice::factory()->create(); livewire(EditInvoice::class, [ 'invoice' => $invoice, ]) ->assertInfolistActionHidden('customer', 'send');});
按钮外观
要断言 Action 的标签正确,可以使用 assertInfolistActionHasLabel()
和 assertInfolistActionDoesNotHaveLabel()
:
use function Pest\Livewire\livewire; it('send action has correct label', function () { $invoice = Invoice::factory()->create(); livewire(EditInvoice::class, [ 'invoice' => $invoice, ]) ->assertInfolistActionHasLabel('customer', 'send', 'Email Invoice') ->assertInfolistActionDoesNotHaveLabel('customer', 'send', 'Send');});
要断言 Action 按钮的图标正确显示,可以使用 assertInfolistActionHasIcon()
或 assertInfolistActionDoesNotHaveIcon()
:
use function Pest\Livewire\livewire; it('when enabled the send button has correct icon', function () { $invoice = Invoice::factory()->create(); livewire(EditInvoice::class, [ 'invoice' => $invoice, ]) ->assertInfolistActionEnabled('customer', 'send') ->assertInfolistActionHasIcon('customer', 'send', 'envelope-open') ->assertInfolistActionDoesNotHaveIcon('customer', 'send', 'envelope');});
要断言 Action 按钮的颜色正确显示,可以使用 assertInfolistActionHasColor()
或 assertInfolistActionDoesNotHaveColor()
:
use function Pest\Livewire\livewire; it('actions display proper colors', function () { $invoice = Invoice::factory()->create(); livewire(EditInvoice::class, [ 'invoice' => $invoice, ]) ->assertInfolistActionHasColor('customer', 'delete', 'danger') ->assertInfolistActionDoesNotHaveColor('customer', 'print', 'danger');});
URL
要断言 Action 的 URL 正确,可以使用 assertInfolistActionHasUrl()
、assertInfolistActionDoesNotHaveUrl()
、assertInfolistActionShouldOpenUrlInNewTab()
和 assertInfolistActionShouldNotOpenUrlInNewTab()
:
use function Pest\Livewire\livewire; it('links to the correct Filament sites', function () { $invoice = Invoice::factory()->create(); livewire(EditInvoice::class, [ 'invoice' => $invoice, ]) ->assertInfolistActionHasUrl('customer', 'filament', 'https://filamentphp.com/') ->assertInfolistActionDoesNotHaveUrl('customer', 'filament', 'https://github.com/filamentphp/filament') ->assertInfolistActionShouldOpenUrlInNewTab('customer', 'filament') ->assertInfolistActionShouldNotOpenUrlInNewTab('customer', 'github');});
Edit on GitHubStill need help? Join our Discord community or open a GitHub discussion