Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LiveComponent] 2.11 - Testing an emit fails because of the event name being used for the method #1110

Closed
rvmourik opened this issue Sep 13, 2023 · 2 comments · Fixed by #1113

Comments

@rvmourik
Copy link

rvmourik commented Sep 13, 2023

I am implementing the new test helpers in our project and so far all is working well but one thing. Maybe I am misinterpreting the way it should work but the docs state that you can test your component with calling an emit with one of the following two syntaxes:

// call live actions
$testComponent
    ->call('increase')
    ->call('increase', ['amount' => 2]) // call a live action with arguments
;

I have a component with a #[LiveListener] listening to an event, like so:

#[LiveListener('CartUpdated')]
public function update(#[LiveArg] float $balance): void
{
    $this->balance = $balance;
}

So in my assumption I can test the above by calling the following in my test:

$testComponent
    ->emit('CartUpdated', ['balance' => $balance])
;

However, this results in an error message stating the following:
Expected method "CartUpdated" on class "App\Components\CartSummaryMobile"

Looking in the TestLiveComponent class in the UX package it uses the eventName as the method to call on the component it is testing. Otherwise, what would be the point of emitting an event? If it is using the method name I could have used the ->call method for executing a LiveAction I guess?

/**
* @param array<string,mixed> $arguments
*/
public function emit(string $event, array $arguments = []): self
{
    return $this->call($event, $arguments);
}

In my assumption those two do not have to be the same right? If I am incorrect, then please let me know.

Thanks in advance.

Robbert

@kbond
Copy link
Member

kbond commented Sep 13, 2023

Hi @rvmourik, you're right, I thought listeners and actions were identical but didn't see this nuance for events.

I'll work on a fix.

@rvmourik
Copy link
Author

Hi @rvmourik, you're right, I thought listeners and actions were identical but didn't see this nuance for events.

I'll work on a fix.

Thanks for the quick reply, looking forward to it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants