Testing a form action.

Hey, I have the following action on my ListUsers field:
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
Actions\Action::make('nodigGebruikerUit')
->form([
TextInput::make('email')
->email()
->required()
])
->action(function ($data) {
$invitation = Invitation::create(['email' => $data['email']]);

Mail::to($invitation->email)->send(new TeamInvitationMail($invitation));

Notification::make('invitedSuccess')
->body('De gebruiker is uitgenodigd')
->success()->send();
})
];
}
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
Actions\Action::make('nodigGebruikerUit')
->form([
TextInput::make('email')
->email()
->required()
])
->action(function ($data) {
$invitation = Invitation::create(['email' => $data['email']]);

Mail::to($invitation->email)->send(new TeamInvitationMail($invitation));

Notification::make('invitedSuccess')
->body('De gebruiker is uitgenodigd')
->success()->send();
})
];
}
Now I am trying to write a test:
it('sends an email when the button is pushed', function () {
login(); //don't mind this it's my own helper function
Mail::fake();

livewire(ListUsers::class)
->callAction('nodigGebruikerUit')
->callAction('send', ['email', '[email protected]']);


Mail::assertSent(TeamInvitationMail::class, function($mail) {
return $mail->hasTo('[email protected]');
});

});
it('sends an email when the button is pushed', function () {
login(); //don't mind this it's my own helper function
Mail::fake();

livewire(ListUsers::class)
->callAction('nodigGebruikerUit')
->callAction('send', ['email', '[email protected]']);


Mail::assertSent(TeamInvitationMail::class, function($mail) {
return $mail->hasTo('[email protected]');
});

});
I am getting an error: failed asserting an action with the name [send] exists. I am not sure how to continue. I have a button, when I press that button a form loads and I want to test that when I press that button, en email is sent. Thanks for your help.
Solution:
I think ```php ->callAction('nodigGebruikerUit') ->set('mountedActionsData.0.email', '[email protected]') ->callMountedAction()...
Jump to solution
15 Replies
awcodes
awcodes6mo ago
Send don’t exist on the action. It’s on the Mail facade. Calling the action will trigger the send. You shouldn’t need to manually call it.
JJSanders
JJSandersOP6mo ago
The error is before the Mail facade I think it doesn’t show the form.
awcodes
awcodes6mo ago
Right, but you are trying to call an action named ‘send’ which doesn’t exist.
JJSanders
JJSandersOP6mo ago
Ok. So 1. How do I trigger the form in my test. 2. How do I send the form?
awcodes
awcodes6mo ago
You need to setup the action with the data it needs before calling in. So, you would mountAction() with the data for the form. Then callAction()
Solution
LeandroFerreira
LeandroFerreira6mo ago
I think
->callAction('nodigGebruikerUit')
->set('mountedActionsData.0.email', '[email protected]')
->callMountedAction()
->callAction('nodigGebruikerUit')
->set('mountedActionsData.0.email', '[email protected]')
->callMountedAction()
not sure if fillForm works
JJSanders
JJSandersOP6mo ago
This works. Thanks so much. Testing is hard.....
LeandroFerreira
LeandroFerreira6mo ago
hum, this will work
->callAction('nodigGebruikerUit', data: ['email' => '[email protected]'])
->callMountedAction()
->callAction('nodigGebruikerUit', data: ['email' => '[email protected]'])
->callMountedAction()
JJSanders
JJSandersOP6mo ago
What is the difference between callAction and callMountedAction?
LeandroFerreira
LeandroFerreira6mo ago
callAction will execute the action. If you have a form, this will open the form. So, you need to run callMountedAction to submit it Actually, If you are using the second example passing the data, I think you don't need to run callMountedAction. callAction will submit the form
JJSanders
JJSandersOP6mo ago
I tested it, and it works indeed Thanks soo much
JJSanders
JJSandersOP6mo ago
Thanks I did read that page. I found it hard to understand maybe because it has just examples of the tests but not of the actual code.
LeandroFerreira
LeandroFerreira6mo ago
you sometimes need to dive into the source code to understand how it works..
JJSanders
JJSandersOP6mo ago
I was looking for the callAction method. But I couldn't find it then. Now I am searching again and there it is 🙄
Want results from more Discord servers?
Add your server