how to test outcome of an action?
I can't seem to find an example of how to test an actions outcome after submitting the form.
I have an action like this:-
And a test that gets so far to populate but I can't see how to now execute the action..
public static function applyConfiguration($action): void
{
$action
->name('bulk-update-lineitem-status')
->icon('heroicon-o-arrows-up-down')
->modalHeading('Change Line Item Status')
->modalDescription(static fn () => new HtmlString("<p class='text-start'>Are you sure you want to change the status of these line items?</p>"))
->schema([
Select::make('status')
->options(LineItemStatus::selectArray())
->native(false)
->preload(),
])
->modalWidth(Width::Medium)
->action(static function (array $data, Collection $records): void {
foreach ($records as $record) {
$record->update($data);
}
});
}
public static function applyConfiguration($action): void
{
$action
->name('bulk-update-lineitem-status')
->icon('heroicon-o-arrows-up-down')
->modalHeading('Change Line Item Status')
->modalDescription(static fn () => new HtmlString("<p class='text-start'>Are you sure you want to change the status of these line items?</p>"))
->schema([
Select::make('status')
->options(LineItemStatus::selectArray())
->native(false)
->preload(),
])
->modalWidth(Width::Medium)
->action(static function (array $data, Collection $records): void {
foreach ($records as $record) {
$record->update($data);
}
});
}
it('can update multiple line item statuses at once', function (): void {
$this->actingAs(\App\Models\Admin::factory()->create());
$order = Order::factory()->create();
$product = Product::factory()->create();
$lineItems = LineItem::factory()->count(3)->create([
'order_id' => $order->id,
'product_id' => $product->id,
'status' => LineItemStatus::PENDING_ASSOCIATE_ASSIGNMENT,
]);
$action = BulkChangeLineItemStatus::make();
// Test that the action can be created and bulk update functionality exists
expect($action)->toBeInstanceOf(BulkChangeLineItemStatus::class)
->and($lineItems)->toHaveCount(3)
->and($lineItems->first()->status)->toBe(LineItemStatus::PENDING_ASSOCIATE_ASSIGNMENT);
//How to now open the action modal and set a new status?
});
it('can update multiple line item statuses at once', function (): void {
$this->actingAs(\App\Models\Admin::factory()->create());
$order = Order::factory()->create();
$product = Product::factory()->create();
$lineItems = LineItem::factory()->count(3)->create([
'order_id' => $order->id,
'product_id' => $product->id,
'status' => LineItemStatus::PENDING_ASSOCIATE_ASSIGNMENT,
]);
$action = BulkChangeLineItemStatus::make();
// Test that the action can be created and bulk update functionality exists
expect($action)->toBeInstanceOf(BulkChangeLineItemStatus::class)
->and($lineItems)->toHaveCount(3)
->and($lineItems->first()->status)->toBe(LineItemStatus::PENDING_ASSOCIATE_ASSIGNMENT);
//How to now open the action modal and set a new status?
});
3 Replies
Not sure if this can help you: https://filamentphp.com/docs/4.x/testing/testing-actions#using-action-class-names-in-tests
maybe i'm an idiot and it was right there..
https://filamentphp.com/docs/4.x/testing/testing-actions#testing-forms-in-action-modals
Although...Can't get fillForm to fill the modal form fields. But using set does work like this;-
So full test example for a bulk action with a modal form
->set('mountedActions.0.data.status'
->set('mountedActions.0.data.status'
$order = Order::factory()->create();
$product = getCoachingProduct();
$lineItems = LineItem::factory()->count(3)->create([
'order_id' => $order->id,
'product_id' => $product->id,
'status' => LineItemStatus::PENDING_ASSOCIATE_ASSIGNMENT,
]);
// Verify initial state
expect($lineItems)->toHaveCount(3)
->and($lineItems->first()->status)->toBe(LineItemStatus::PENDING_ASSOCIATE_ASSIGNMENT);
livewire(ListLineItems::class)
->sortTable('id','desc')
->selectTableRecords($lineItems->pluck('id')->toArray())
->mountAction(TestAction::make('change_status_of_selected')->table()->bulk())
->set('mountedActions.0.data.status',LineItemStatus::IN_PROGRESS->value)
->assertHasNoFormErrors()
->callMountedAction();
// Assert that all line items have been updated to the new status
foreach ($lineItems as $lineItem) {
$lineItem->refresh();
expect($lineItem->status)->toBe(LineItemStatus::IN_PROGRESS);
}
$order = Order::factory()->create();
$product = getCoachingProduct();
$lineItems = LineItem::factory()->count(3)->create([
'order_id' => $order->id,
'product_id' => $product->id,
'status' => LineItemStatus::PENDING_ASSOCIATE_ASSIGNMENT,
]);
// Verify initial state
expect($lineItems)->toHaveCount(3)
->and($lineItems->first()->status)->toBe(LineItemStatus::PENDING_ASSOCIATE_ASSIGNMENT);
livewire(ListLineItems::class)
->sortTable('id','desc')
->selectTableRecords($lineItems->pluck('id')->toArray())
->mountAction(TestAction::make('change_status_of_selected')->table()->bulk())
->set('mountedActions.0.data.status',LineItemStatus::IN_PROGRESS->value)
->assertHasNoFormErrors()
->callMountedAction();
// Assert that all line items have been updated to the new status
foreach ($lineItems as $lineItem) {
$lineItem->refresh();
expect($lineItem->status)->toBe(LineItemStatus::IN_PROGRESS);
}