symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
851 stars 313 forks source link

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

Closed rvmourik closed 1 year ago

rvmourik commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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!