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:-
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);
}
});
}
And a test that gets so far to populate but I can't see how to now execute the action..
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
acroninja
acroninjaOP2mo ago
Although...Can't get fillForm to fill the modal form fields. But using set does work like this;-
->set('mountedActions.0.data.status'
->set('mountedActions.0.data.status'
So full test example for a bulk action with a modal form
$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);
}

Did you find this page helpful?