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', 'test@example.com']);


Mail::assertSent(TeamInvitationMail::class, function($mail) {
return $mail->hasTo('test@example.com');
});

});
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', 'test@example.com']);


Mail::assertSent(TeamInvitationMail::class, function($mail) {
return $mail->hasTo('test@example.com');
});

});
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', 'test@example.com') ->callMountedAction()...
Jump to solution
15 Replies
awcodes
awcodes2mo 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
JJSanders2mo ago
The error is before the Mail facade I think it doesn’t show the form.
awcodes
awcodes2mo ago
Right, but you are trying to call an action named ‘send’ which doesn’t exist.
JJSanders
JJSanders2mo ago
Ok. So 1. How do I trigger the form in my test. 2. How do I send the form?
awcodes
awcodes2mo 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
LeandroFerreira2mo ago
I think
->callAction('nodigGebruikerUit')
->set('mountedActionsData.0.email', 'test@example.com')
->callMountedAction()
->callAction('nodigGebruikerUit')
->set('mountedActionsData.0.email', 'test@example.com')
->callMountedAction()
not sure if fillForm works
JJSanders
JJSanders2mo ago
This works. Thanks so much. Testing is hard.....
LeandroFerreira
LeandroFerreira2mo ago
hum, this will work
->callAction('nodigGebruikerUit', data: ['email' => 'test@example.com'])
->callMountedAction()
->callAction('nodigGebruikerUit', data: ['email' => 'test@example.com'])
->callMountedAction()
JJSanders
JJSanders2mo ago
What is the difference between callAction and callMountedAction?
LeandroFerreira
LeandroFerreira2mo 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
JJSanders2mo ago
I tested it, and it works indeed Thanks soo much
JJSanders
JJSanders2mo 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
LeandroFerreira2mo ago
you sometimes need to dive into the source code to understand how it works..
JJSanders
JJSanders2mo 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