Skip to content

fix(live): fix testing events #1113

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

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/LiveComponent/src/Test/TestLiveComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\LiveComponentHydrator;
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory;
use Symfony\UX\TwigComponent\ComponentFactory;
Expand Down Expand Up @@ -100,7 +101,24 @@ public function call(string $action, array $arguments = []): self
*/
public function emit(string $event, array $arguments = []): self
{
return $this->call($event, $arguments);
$listeners = AsLiveComponent::liveListeners($this->component());
$actions = [];

foreach ($listeners as $listener) {
if ($listener['event'] === $event) {
$actions[] = ['name' => $listener['action'], 'args' => $arguments];
}
}

if (!$actions) {
throw new \InvalidArgumentException(sprintf('Event "%s" does not exist on component "%s".', $event, $this->metadata->getName()));
}

if (1 === \count($listeners)) {
return $this->call($actions[0]['name'], $arguments);
}

return $this->request(['actions' => $actions], '_batch');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super minor, and it shouldn't make any difference, but if we have just one action, it won't process through batch: it'll just call that one action directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The js makes this distinction?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. The js always tries to use the normal endpoint (either for calling a LiveAction or the "default" action in the case of a re-render). If it finds that it has 2 things queued at once, then it goes through batch. https://github.com/symfony/ux/blob/2.x/src/LiveComponent/assets/src/Backend/RequestBuilder.ts#L71-L79

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll match in the helper.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change has been made.

}

public function set(string $prop, mixed $value): self
Expand Down
14 changes: 14 additions & 0 deletions src/LiveComponent/tests/Fixtures/Component/Component2.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveListener;
use Symfony\UX\LiveComponent\Attribute\PreReRender;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
Expand Down Expand Up @@ -51,6 +53,18 @@ public function redirect(UrlGeneratorInterface $urlGenerator): RedirectResponse
return new RedirectResponse($urlGenerator->generate('homepage'), 302, ['X-Custom-Header' => '1']);
}

#[LiveListener('triggerIncrease')]
public function increaseEvent1(#[LiveArg] int $amount = 1): void
{
$this->count += $amount;
}

#[LiveListener('triggerIncrease')]
public function increaseEvent2(#[LiveArg] int $amount = 1): void
{
$this->count += $amount;
}

#[PreDehydrate]
public function preDehydrateMethod(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ public function testCanCallLiveActionWithArguments(): void
$this->assertSame(33.3, $testComponent->component()->arg3);
}

public function testCanEmitEvent(): void
{
$testComponent = $this->createLiveComponent('component2');

$this->assertStringContainsString('Count: 1', $testComponent->render());
$this->assertSame(1, $testComponent->component()->count);

$testComponent->emit('triggerIncrease', ['amount' => 2]);

$this->assertStringContainsString('Count: 5', $testComponent->render());
$this->assertSame(5, $testComponent->component()->count);
}

public function testInvalidEventName(): void
{
$testComponent = $this->createLiveComponent('component2');

$this->expectException(\InvalidArgumentException::class);

$testComponent->emit('invalid');
}

public function testCanSetLiveProp(): void
{
$testComponent = $this->createLiveComponent('component_with_writable_props');
Expand Down