测试 Action
Calling an action in a test
You can call an action by passing its name or class to callAction():
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();
});
Testing table actions
To test table actions, you can use a TestAction object with the table() method. This object receives the name of the action you want to test, and replaces the name of the action in any testing method you want to use. For example:
use Filament\Actions\Testing\TestAction;
use function Pest\Livewire\livewire;
$invoice = Invoice::factory()->create();
livewire(ListInvoices::class)
->callAction(TestAction::make('send')->table($invoice));
livewire(ListInvoices::class)
->assertActionVisible(TestAction::make('send')->table($invoice))
livewire(ListInvoices::class)
->assertActionExists(TestAction::make('send')->table($invoice))
Testing table header actions
To test a header action, you can use the table() method without passing in a specific record to test with:
use Filament\Actions\Testing\TestAction;
use function Pest\Livewire\livewire;
livewire(ListInvoices::class)
->callAction(TestAction::make('create')->table());
livewire(ListInvoices::class)
->assertActionVisible(TestAction::make('create')->table())
livewire(ListInvoices::class)
->assertActionExists(TestAction::make('create')->table())
Testing table bulk actions
To test a bulk action, first call selectTableRecords() and pass in any records you want to select. Then, use the TestAction's bulk() method to specify the action you want to test. For example:
use Filament\Actions\Testing\TestAction;
use function Pest\Livewire\livewire;
$invoices = Invoice::factory()->count(3)->create();
livewire(ListInvoices::class)
->selectTableRecords($invoices->pluck('id')->toArray())
->callAction(TestAction::make('send')->table()->bulk());
livewire(ListInvoices::class)
->assertActionVisible(TestAction::make('send')->table()->bulk())
livewire(ListInvoices::class)
->assertActionExists(TestAction::make('send')->table()->bulk())
Testing actions in a schema
If an action belongs to a component in a resource's infolist, for example, if it is in the belowContent() method of an infolist entry, you can use the TestAction object with the schemaComponent() method. This object receives the name of the action you want to test and replaces the name of the action in any testing method you want to use. For example:
use Filament\Actions\Testing\TestAction;
use function Pest\Livewire\livewire;
$invoice = Invoice::factory()->create();
livewire(EditInvoice::class)
->callAction(TestAction::make('send')->schemaComponent('customer_id'));
livewire(EditInvoice::class)
->assertActionVisible(TestAction::make('send')->schemaComponent('customer_id'))
livewire(EditInvoice::class)
->assertActionExists(TestAction::make('send')->schemaComponent('customer_id'))
Testing actions inside another action's schema / form
If an action belongs to a component in another action's schema() (or form()), for example, if it is in the belowContent() method of a form field in an action modal, you can use the TestAction object with the schemaComponent() method. This object receives the name of the action you want to test and replaces the name of the action in any testing method you want to use. You should pass an array of TestAction objects in order, for example:
use Filament\Actions\Testing\TestAction;
use function Pest\Livewire\livewire;
$invoice = Invoice::factory()->create();
livewire(ManageInvoices::class)
->callAction([
TestAction::make('view')->table($invoice),
TestAction::make('send')->schemaComponent('customer.name'),
]);
livewire(ManageInvoices::class)
->assertActionVisible([
TestAction::make('view')->table($invoice),
TestAction::make('send')->schemaComponent('customer.name'),
]);
livewire(ManageInvoices::class)
->assertActionExists([
TestAction::make('view')->table($invoice),
TestAction::make('send')->schemaComponent('customer.name'),
]);