Skip to content

Commit f835931

Browse files
committed
Add tests
1 parent 068dff4 commit f835931

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/LiveComponent/src/Test/InteractsWithLiveComponents.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ protected function assertComponentEmitEvent(TestLiveComponent $testLiveComponent
5656
}
5757

5858
foreach ($parameters as $key => $value) {
59-
$this->assertSame($value, $eventData['data'][$key], \sprintf('EventData (%s) is not valid', $key));
59+
$this->assertSame($value, $eventData['data'][$key] ?? null, \sprintf('EventData (%s) is not valid', $key));
6060
}
6161
}
6262

6363
protected function assertComponentNotEmitEvent(TestLiveComponent $testLiveComponent, string $eventName): void
6464
{
65-
$this->assertNull($this->testLiveComponent($testLiveComponent->render(), $eventName), \sprintf('Event %s emitted', $eventName));
65+
$this->assertNull($testLiveComponent->getEmittedEvent($testLiveComponent->render(), $eventName), \sprintf('Event %s emitted', $eventName));
6666
}
6767
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\UX\LiveComponent\Tests\Functional\Test;
1313

14+
use PHPUnit\Framework\AssertionFailedError;
1415
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1516
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
1617
use Symfony\Component\Security\Core\User\InMemoryUser;
@@ -217,4 +218,38 @@ public function testSetLocaleRenderLocalizedComponent(): void
217218
$testComponent->setRouteLocale('de');
218219
$this->assertStringContainsString('Locale: de', $testComponent->render());
219220
}
221+
222+
public function testComponentEmitsExpectedEventWithParameters(): void
223+
{
224+
$testComponent = $this->createLiveComponent('component_with_emit');
225+
226+
$testComponent->call('actionThatEmits');
227+
228+
$this->assertComponentEmitEvent($testComponent, 'event1', [
229+
'foo' => 'bar',
230+
]);
231+
}
232+
233+
public function testComponentDoesNotEmitUnexpectedEvent(): void
234+
{
235+
$testComponent = $this->createLiveComponent('component_with_emit');
236+
237+
$testComponent->call('actionThatEmits');
238+
239+
$this->assertComponentNotEmitEvent($testComponent, 'non_emitted_event');
240+
}
241+
242+
public function testComponentEmitsEventWithWrongParametersFails(): void
243+
{
244+
$testComponent = $this->createLiveComponent('component_with_emit');
245+
246+
$testComponent->call('actionThatEmits');
247+
248+
$this->expectException(AssertionFailedError::class);
249+
$this->expectExceptionMessage('EventData (foo2) is not valid');
250+
$this->assertComponentEmitEvent($testComponent, 'event1', [
251+
'foo' => 'bar',
252+
'foo2' => 'bar2',
253+
]);
254+
}
220255
}

0 commit comments

Comments
 (0)