Skip to content

Commit 58ba8a5

Browse files
committed
add docs
1 parent 04cff46 commit 58ba8a5

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,6 +3033,74 @@ Then specify this new route on your component:
30333033
use DefaultActionTrait;
30343034
}
30353035
3036+
Test Helper
3037+
-----------
3038+
3039+
.. versionadded:: 2.11
3040+
3041+
The test helper was added in LiveComponents 2.11.
3042+
3043+
For testing, you can use the ``InteractsWithLiveComponents`` trait which
3044+
uses Symfony's test client to render and make requests to your components::
3045+
3046+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
3047+
use Symfony\UX\LiveComponent\Test\InteractsWithLiveComponents;
3048+
3049+
class MyComponentTest extends KernelTestCase
3050+
{
3051+
use InteractsWithLiveComponents;
3052+
3053+
public function testCanRenderAndInteract(): void
3054+
{
3055+
$testComponent = $this->createLiveComponent(
3056+
name: 'MyComponent', // can also use FQCN (MyComponent::class)
3057+
data: ['foo' => 'bar'],
3058+
);
3059+
3060+
// render the component html
3061+
$this->assertStringContainsString('Count: 0', $testComponent->render());
3062+
3063+
// call live actions
3064+
$testComponent
3065+
->call('increase')
3066+
->call('increase', ['amount' => 2]) // call a live action with arguments
3067+
;
3068+
3069+
$this->assertStringContainsString('Count: 3', $testComponent->render());
3070+
3071+
// emit live events
3072+
$testComponent
3073+
->emit('increaseEvent')
3074+
->emit('increaseEvent', ['amount' => 2]) // emit a live event with arguments
3075+
;
3076+
3077+
// set live props
3078+
$testComponent
3079+
->set('count', 99)
3080+
;
3081+
3082+
$this->assertStringContainsString('Count: 99', $testComponent->render());
3083+
3084+
// refresh the component
3085+
$testComponent->refresh();
3086+
3087+
// access the component object (in it's current state)
3088+
$component = $testComponent->component(); // MyComponent
3089+
3090+
$this->assertSame(99, $component->count);
3091+
3092+
// test a live action that redirects
3093+
$response = $testComponent->call('redirect')->response(); // Symfony\Component\HttpFoundation\Response
3094+
3095+
$this->assertSame(302, $response->getStatusCode());
3096+
}
3097+
}
3098+
3099+
.. note::
3100+
3101+
The ``InteractsWithLiveComponents`` trait can only be used in tests that extend
3102+
``Symfony\Bundle\FrameworkBundle\Test\KernelTestCase``.
3103+
30363104
Backward Compatibility promise
30373105
------------------------------
30383106

0 commit comments

Comments
 (0)