Skip to content

Commit 976b53f

Browse files
committed
bug #1113 fix(live): fix testing events (kbond)
This PR was merged into the 2.x branch. Discussion ---------- fix(live): fix testing events | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | Fix #1110 | License | MIT When implementing #823, I made the wrong assumption that events were identical to actions. This fixes that. Commits ------- 3bb139b fix(live): fix testing events
2 parents 6e6c903 + 3bb139b commit 976b53f

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/LiveComponent/src/Test/TestLiveComponent.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\Response;
1616
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1717
use Symfony\Component\Security\Core\User\UserInterface;
18+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
1819
use Symfony\UX\LiveComponent\LiveComponentHydrator;
1920
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory;
2021
use Symfony\UX\TwigComponent\ComponentFactory;
@@ -100,7 +101,24 @@ public function call(string $action, array $arguments = []): self
100101
*/
101102
public function emit(string $event, array $arguments = []): self
102103
{
103-
return $this->call($event, $arguments);
104+
$listeners = AsLiveComponent::liveListeners($this->component());
105+
$actions = [];
106+
107+
foreach ($listeners as $listener) {
108+
if ($listener['event'] === $event) {
109+
$actions[] = ['name' => $listener['action'], 'args' => $arguments];
110+
}
111+
}
112+
113+
if (!$actions) {
114+
throw new \InvalidArgumentException(sprintf('Event "%s" does not exist on component "%s".', $event, $this->metadata->getName()));
115+
}
116+
117+
if (1 === \count($listeners)) {
118+
return $this->call($actions[0]['name'], $arguments);
119+
}
120+
121+
return $this->request(['actions' => $actions], '_batch');
104122
}
105123

106124
public function set(string $prop, mixed $value): self

src/LiveComponent/tests/Fixtures/Component/Component2.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\HttpFoundation\RedirectResponse;
1515
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1616
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
17+
use Symfony\UX\LiveComponent\Attribute\LiveArg;
18+
use Symfony\UX\LiveComponent\Attribute\LiveListener;
1719
use Symfony\UX\LiveComponent\Attribute\PreReRender;
1820
use Symfony\UX\LiveComponent\Attribute\LiveAction;
1921
use Symfony\UX\LiveComponent\Attribute\LiveProp;
@@ -51,6 +53,18 @@ public function redirect(UrlGeneratorInterface $urlGenerator): RedirectResponse
5153
return new RedirectResponse($urlGenerator->generate('homepage'), 302, ['X-Custom-Header' => '1']);
5254
}
5355

56+
#[LiveListener('triggerIncrease')]
57+
public function increaseEvent1(#[LiveArg] int $amount = 1): void
58+
{
59+
$this->count += $amount;
60+
}
61+
62+
#[LiveListener('triggerIncrease')]
63+
public function increaseEvent2(#[LiveArg] int $amount = 1): void
64+
{
65+
$this->count += $amount;
66+
}
67+
5468
#[PreDehydrate]
5569
public function preDehydrateMethod(): void
5670
{

src/LiveComponent/tests/Functional/Test/InteractsWithLiveComponentsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ public function testCanCallLiveActionWithArguments(): void
6363
$this->assertSame(33.3, $testComponent->component()->arg3);
6464
}
6565

66+
public function testCanEmitEvent(): void
67+
{
68+
$testComponent = $this->createLiveComponent('component2');
69+
70+
$this->assertStringContainsString('Count: 1', $testComponent->render());
71+
$this->assertSame(1, $testComponent->component()->count);
72+
73+
$testComponent->emit('triggerIncrease', ['amount' => 2]);
74+
75+
$this->assertStringContainsString('Count: 5', $testComponent->render());
76+
$this->assertSame(5, $testComponent->component()->count);
77+
}
78+
79+
public function testInvalidEventName(): void
80+
{
81+
$testComponent = $this->createLiveComponent('component2');
82+
83+
$this->expectException(\InvalidArgumentException::class);
84+
85+
$testComponent->emit('invalid');
86+
}
87+
6688
public function testCanSetLiveProp(): void
6789
{
6890
$testComponent = $this->createLiveComponent('component_with_writable_props');

0 commit comments

Comments
 (0)