-
-
Notifications
You must be signed in to change notification settings - Fork 343
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
[Twig] add test helper #821
Conversation
ce5f2db
to
7251aea
Compare
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class RenderedComponent implements \Stringable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this stringable object to wrap the rendered html so we can possibly add something like described in #818 (comment) in the future.
I wasn't sure if changing the type-hint from string -> stringable object as the return type of InteractsWithTwigComponent::renderTwigComponent()
would be considered a BC break. If not, I can change to just return string
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's start with this - easier to remove this later if we end up not needing it or it's a pain for some reason than to go in the other direction.
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
trait InteractsWithTwigComponents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is my preferred name (:wink:) but I have no problem adding the Trait suffix or naming something else that jives with Symfony standards.
My plan is to eventually add an |
bba954a
to
b4f7084
Compare
I've added the ability to use the component class name with these helpers (ie |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @kbond that's super handy! 😁
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
trait InteractsWithTwigComponents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am just wondering why do we use trait here? In my use case, I gonna create a dedicated test class for my component, and I not gonna mixed my components tests with other tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a pattern I've found useful with my other testing libraries. I like using traits to avoid forcing a single type of test.
class MyComponentTest extends KernelTestCase
{
use InteractsWithTwigComponents, ResetDatabase, Factories;
// ...
}
I get the above would still work if we made this trait into and abstract TwigComponentTestCase
. I'm just trying to be consistent: Make a standard kernel test, and inject the test features you need via traits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ho yes using this trait with foundry just rocks! Thanks for all of this 😁
@kbond friendly ping here! What is needed in order to merge it? I feel like it's super close. I already start to use this trait on my project and it's super handy! 😁 |
I feel this is basically ready. I'll rebase this week to ensure all's still good. I'll add a test for an embedded component using html syntax too. I guess I was looking for opinions on the following:
/cc @weaverryan |
I like this. I see what you're saying... but we can't do any better. To me, it feels like you've added a way to unit test a class... but sure, ultimately, someone could call it with invalid arguments or forget to call it at all on page. A higher-level test would be needed for that. |
'name' => $name, | ||
'data' => $data, | ||
]) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is pretty nuts 👍
b4f7084
to
0b27ea9
Compare
Thanks Kevin - this looks awesome! |
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Live] add test helper | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | Continuation of #821 | License | MIT ```php use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\UX\LiveComponent\Test\InteractsWithLiveComponents; class MyComponentTest extends KernelTestCase { use InteractsWithLiveComponents; public function testThisComponent(): void { $testComponent = $this->createLiveComponent('my_component'); $testComponent = $this->createLiveComponent(Component::class); // or use the FQCN (string) $testComponent->render(); // string - initial state html $testComponent->component(); // object - component instance at initial state $testComponent ->call('increase') // call a live action ->call('increase', ['amount' => 2]) // call a live action with arguments ->set('count', 5) // set a live prop ->emit('inceaseEvent') // emit a live event ->emit('inceaseEvent', ['amount' => 2]) // emit a live event with arguments ->refresh() // simply refresh the component ; (string) $testComponent->render(); // string - updated state html $testComponent->component(); // object - component instance at updated state /** `@var` Symfony\Component\HttpFoundation\Response $response */ $response = $testComponent->call('actionThatRedirects')->response(); } } ``` TODO: - [x] handle action responses - [x] `->refresh()` method? - [x] Docs - [x] Changelog Commits ------- faa1518 [Live] add test helper
This works with Live components out of the box (not with slots though as they aren't supported).