Testing action error

Hello and thanks for anyone that helps me. So I have a list page were i have a custom action
Action::make('import-historical-orders')
->label('Import Historical Orders')
->before(function (Action $action) {
if (Gate::denies('import-historical-orders')) {
$action->failure();
$action->halt();
}
})
->action(fn () => ImportUserHistoricalOrders::dispatch(auth()->user()))
->successNotification($this->importHistoricalOrdersSuccessNotification())
->failureNotification($this->importHistoricalOrdersFailureNotification());
Action::make('import-historical-orders')
->label('Import Historical Orders')
->before(function (Action $action) {
if (Gate::denies('import-historical-orders')) {
$action->failure();
$action->halt();
}
})
->action(fn () => ImportUserHistoricalOrders::dispatch(auth()->user()))
->successNotification($this->importHistoricalOrdersSuccessNotification())
->failureNotification($this->importHistoricalOrdersFailureNotification());
When testing this action i'm getting the error
Failed asserting that an event [open-modal] was fired with parameters: {"id":"oUPpTeYPcvmKfIXk0WEo-action"}
Failed asserting that an event [open-modal] was fired with parameters: {"id":"oUPpTeYPcvmKfIXk0WEo-action"}
The test is the following one
livewire(ListHistoricalOrders::class)
->callAction('import-historical-orders')
->assertActionHalted('import-historical-orders')
livewire(ListHistoricalOrders::class)
->callAction('import-historical-orders')
->assertActionHalted('import-historical-orders')
If i remove the Action $action parameter from the closure on the before method from the action the error doesn't appear anymore. So how can i test an action that have a closure with the action itself?
4 Replies
🎮Days🕹
🎮Days🕹OP9mo ago
Anyone had this error before?
Dennis Koch
Dennis Koch9mo ago
I think this is coming from callAction() which asserts opening the modal which won't happen because of your before(). Try using mountAction()
🎮Days🕹
🎮Days🕹OP9mo ago
@Dennis Koch thanks for the answer but i have tryied it and i have the same behaviour even doing this
Action::make('import-historical-orders')
->label('Import Historical Orders')
->action(function (Action $action) {
if (Gate::denies('import-historical-orders')) {
$action->failure();
$action->halt();
}

ImportUserHistoricalOrders::dispatch(auth()->user());
$action->success();
})
->successNotification($this->importHistoricalOrdersSuccessNotification())
->failureNotification($this->importHistoricalOrdersFailureNotification());
Action::make('import-historical-orders')
->label('Import Historical Orders')
->action(function (Action $action) {
if (Gate::denies('import-historical-orders')) {
$action->failure();
$action->halt();
}

ImportUserHistoricalOrders::dispatch(auth()->user());
$action->success();
})
->successNotification($this->importHistoricalOrdersSuccessNotification())
->failureNotification($this->importHistoricalOrdersFailureNotification());
removing the before and adding the check on the action i have the same behaviour...it's something to do when i pass the Action parametter to the closure. Solved the situation removing the
$action->halt();
$action->halt();
and adding
$action->cancel();
$action->cancel();
and removing the expectation
->assertActionHalted('import-historical-orders')
->assertActionHalted('import-historical-orders')
for an unknown reason for me the halt method is giving this error
gizmojo
gizmojo5mo ago
Same issue here when halting the action and testing for this. @Dennis Koch those assertions prevent it reaching assertActionHalted.

Did you find this page helpful?