Skip to content

Commit

Permalink
fix(live): fix testing events
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Sep 13, 2023
1 parent 69e3531 commit bf02e80
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/LiveComponent/src/Test/TestLiveComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
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 @@ -89,7 +90,20 @@ 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()));
}

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

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

0 comments on commit bf02e80

Please sign in to comment.